Help in converting a certain recurtion

  • Thread starter transgalactic
  • Start date
In summary, the conversation is discussing the development of a method to generate all possible letter combinations for a given phone number, in order to make it more memorable. The method would use recursion and involve printing out subsets of a given string. The conversation includes a theoretical idea of using nested "for" loops to generate combinations for different lengths of numbers.
  • #1
transgalactic
1,395
0
here is a question and i have a very similar code but i need to convert it
some how in order to fit this question

question:
In order to make their phone numbers more memorable, service providers like to find numbers that spell out some word (called a mnemonic) appropriate to their business that makes that phone number easier to remember. For example, the phone number for a recorded time-of-day message in some localities is 637-8687 (NERVOUS). Imagine that you have just been hired by a local telephone company to write a method listMnemonics that will generate all possible letter combinations that correspond to a given number, represented as a string of digits. For example, if you call
recursiveObject.listMnemonics("723")
your method shall generate and print out (using recursion) the following 27 possible letter combinations that correspond to that prefix:
PAD PBD PCD RAD RBD RCD SAD SBD SCD
PAE PBE PCE RAE RBE RCE SAE SBE SCE
PAF PBF PCF RAF RBF RCF SAF SBF SCF

my efforts in solving it:
i know that in order to solve this question is need to use a recursive methos that
prints out all the subsets of a given string letters with a given resolt string size

i have built this code
Code:
ublic class subsets {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String word="abcd";
		
		
subsets(word.length(),3,"",word);
	}
	public static void phone(String phone){
		
	}
	public static void subsets(int n,int k,String also,String word){
		if (n==0){
			if (also.length()==k){
			System.out.println(also);
			}
		}
		else{
			subsets(n-1,k,also+word.charAt(n-1),word);
			subsets(n-1,k,also,word);
			
				
			
		}
	}

}
 
Last edited:
Technology news on Phys.org
  • #2
i had a theoretical idea the we put an inner counter for each cell
and then count them like numbers
001
002
003
but for example in order to have a combination for a number with length 4
we need to have 4 "for" loops one inside another

for 5 digit number we need to have 5 "for" loop one inside of the other

how do i solve its for every given length
 
  • #3


To convert your code to fit this question, you can modify your subsets method to take in the phone number string and generate all possible letter combinations for that specific number. You can do this by creating a new method called listMnemonics that will call your subsets method with the appropriate parameters. This new method should also have a base case to check if the given number is empty or not. If it is empty, then you can print out the resulting mnemonic. If it is not empty, then you can call your subsets method with the appropriate parameters to generate the letter combinations.

Additionally, you can create a hashmap that maps each digit to its corresponding letters (e.g. 2 - ABC, 3 - DEF, etc.) to make it easier to generate the letter combinations. This way, you can simply access the letters for each digit in the subsets method and add them to the resulting string.

Overall, the key is to understand the problem and modify your existing code to fit its specific requirements. Good luck!
 

FAQ: Help in converting a certain recurtion

How can I convert a recursive function into an iterative one?

To convert a recursive function into an iterative one, you can use a loop structure, such as a for loop or a while loop, to mimic the recursive calls. You will also need to use a stack or queue data structure to keep track of the function calls and their corresponding parameters.

Why would I want to convert a recursive function into an iterative one?

Converting a recursive function into an iterative one can improve the efficiency and speed of the code. Recursion can be memory-intensive, and using an iterative approach can save memory and make the code more efficient.

What are the advantages of using a recursive function?

Recursive functions can be easier to read and understand compared to iterative code. They also allow for a more concise and elegant solution to some problems. Additionally, some problems may be inherently recursive in nature, making it a more natural approach to solving them.

Can all recursive functions be converted into iterative ones?

No, not all recursive functions can be converted into iterative ones. Some problems may only have a recursive solution, and attempting to convert it into an iterative one may result in a more complex and inefficient code. It ultimately depends on the specific problem and the logic of the function.

Are there any specific steps to follow when converting a recursive function into an iterative one?

There is no set formula for converting a recursive function into an iterative one. It will depend on the specific function and the problem it is solving. However, some general steps to follow include identifying the base case and recursive calls, creating a loop structure, and using a stack or queue data structure to keep track of function calls and parameters.

Similar threads

Replies
3
Views
3K
Replies
2
Views
3K
Replies
11
Views
5K
Replies
12
Views
2K
Replies
10
Views
3K
Replies
7
Views
2K
Back
Top