How to Write a Recursive Solution for Phonepad Mnemonics in Java?

  • Thread starter kantrcr
  • Start date
In summary, the programmer is asked to create a function that takes a prefix and suffix and produces a list of phone numbers. The solution uses a different prototype, but the programmer can't figure out how to write a recursive solution given the required prototype.
  • #1
kantrcr
1
0
I have a programming (Java) lab asking for a function of the prototype:

Vector phoneNumbers(String suffix, String prefix)

And I'm suppose to use recursion to fill the final returned Vector with every possible word letter combonation the digits contained in suffix could be. Prefix is a string I'm suppose to place before each combonation generated of suffix, and the concatenation of these two should go into a vector.

I've written a solution that uses a much different prototype, but I cannot figure out how I can write a recursive solution given this required prototype.

Code:
public static void ListMnemonics(String suffix, String prefix, String mnem){
      if(mnem.length() == suffix.length())
      {
             System.out.println(prefix + mnem);
              return;
      }

      String letters = GetLettersCorrespondingToNumber((int)suffix.charAt(mnem.length()) - (int)'0');
      
      for(int i=0; i<letters.length(); i++)
      {
             ListMnemonics(suffix, prefix, mnem + letters.charAt(i));
     }
}public static String GetLettersCorrespondingToNumber (int num){
char [][]arr=new char[10][3];

arr[0][0]='a';
arr[0][1]='b';
arr[0][2]='c';
arr[1][0]='d';
arr[1][1]='e';
arr[1][2]='f';
arr[2][0]='g';
arr[2][1]='h';
arr[2][2]='i';
arr[3][0]='g';
arr[3][1]='h';
arr[3][2]='i';
arr[4][0]='j';
arr[4][1]='k';
arr[4][2]='l';
arr[5][0]='m';
arr[5][1]='n';
arr[5][2]='o';
arr[6][0]='p';
arr[6][1]='q';
arr[6][2]='r';
arr[7][0]='s';
arr[7][1]='t';
arr[7][2]='u';
arr[8][0]='v';
arr[8][1]='w';
arr[8][2]='x';
arr[9][0]='y';
arr[9][1]='z';
arr[9][2]='+';
String str=new String(""+arr[num][0]+arr[num][1]+arr[num][2]);
return str;
}

I can think of a way to do this in a language like C or C++ where i could just have a static Vector, but Java doesn't have this so I can't think of a way to keep a previous copy of my Vector after each recursive call.
 
Last edited:
Technology news on Phys.org
  • #2
Here is a solution that uses the required prototype:public static Vector<String> phoneNumbers(String suffix, String prefix){ Vector<String> result = new Vector<String>(); if (suffix.length() == 0) { result.add(prefix); return result; } String digits = GetLettersCorrespondingToNumber((int)suffix.charAt(0) - (int)'0'); for (int i=0; i<digits.length(); i++) { result.addAll(phoneNumbers(suffix.substring(1), prefix + digits.charAt(i))); } return result;}
 
  • #3


I would suggest using a different approach to solve this problem. Instead of trying to use recursion with the given prototype, you could create a new function that takes in the same parameters (suffix, prefix) and returns a vector of all possible combinations. This new function can then be called recursively within your existing function to build the vector. This way, you can still use recursion to generate all possible combinations, but without having to modify the given prototype.

Additionally, you could also consider using a data structure like a stack or queue to store the generated combinations instead of a vector. This would allow you to keep track of the previous combinations and build upon them in each recursive call.

Overall, it is important to approach problems like this with a flexible and creative mindset. While the given prototype may seem limiting, there are often alternative ways to solve the problem. As a scientist, it is important to think critically and adapt to find the best solution.
 

What is Recursive Phonepad Mneumonics?

Recursive Phonepad Mneumonics is a method used to generate all possible combinations of words that can be formed using the letters associated with the numbers on a phone keypad. It is commonly used for creating memorable phone numbers and passwords.

How does Recursive Phonepad Mneumonics work?

The method starts with a phone number input by the user. Each number in the phone number is associated with a set of letters on the phone keypad. The method then recursively generates all possible combinations of words that can be formed using these letters. These combinations are then checked against a dictionary to see if they are real words. If they are, they are added to the list of possible mnemonics for the phone number.

What is the benefit of using Recursive Phonepad Mneumonics?

Recursive Phonepad Mneumonics can be used to create easy-to-remember phone numbers or passwords that are also secure. It allows for a large number of possible mnemonics to be generated, increasing the chances of finding a memorable one. It also eliminates the need to use a random number or letter generator, which can be difficult to remember.

Are there any limitations to Recursive Phonepad Mneumonics?

One limitation of Recursive Phonepad Mneumonics is that it can only generate words that can be formed using the letters on a phone keypad. This means that some words may not be possible to create, resulting in fewer options for mnemonics. Additionally, the longer the input phone number, the longer it may take to generate all possible combinations.

How can Recursive Phonepad Mneumonics be useful for security purposes?

Recursive Phonepad Mneumonics can be useful for creating secure passwords by generating a large pool of possible combinations that are difficult to guess. It also allows for the creation of memorable and unique passwords, reducing the risk of using the same password for multiple accounts. Additionally, it eliminates the need to store passwords in a database, which can be vulnerable to hackers.

Similar threads

  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
17
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top