Feb-20-2017, 05:45 AM
Hello,
I am trying to pick up some projects online to practice coding and so far I have faced a challenge.
If we take a look at the US keypad on a standard keyboard, it has ten keys for the digits 0 through 9. Each key 2 through 9 has three letters of the alphabet, but the letters Q and Z are missing. The keys 0 and 1 have no letters. I am trying to construct a mnemonic word for a telephone number. It might choose letters purely at random. Suppose that the program must construct a word for 686–2377. The possible letters for 6 are MNO, so it might choose M first. The possible letters for 8 are TUV, so it might choose V next, etc. Continuing in this way, it might construct MVNBDPR.
However, I want to create a word that is "pronounce-able", meaning it looks like an english word even though it has no meaning in any dictionary on earth. For example, suppose that the program must still construct a word for 686–2377. The possible letters for 6 are MNO, so it might choose M. The possible letters for 8 are TUV, but since U is more likely to follow M than T or V, it might choose U. Continuing in this way, choosing each letter based on how likely it is to follow the previous letter, the program might construct MUNCERS. This isn’t an English word, but it looks like one, so it’s easier to remember than the number.
Here are my thoughts.
1. I created two classes: one is called Random which is used to generate a random number based on Park-Miller Algorithm and then use that number to generate a random character out of a string. My second class is called Mnenomic. Its methods will generate mnemonic words for telephone numbers.
2. In the Mnemonic, I will have a method called make:
make(self, number)
Here number is a telephone number, made up of digit characters. The method make tries to construct a mnemonic word from number. Let’s say the word it constructs is in the local variable mnemonic.
It starts by getting the first digit from number. Then it gets a string of letters for that digit, using letters. It picks one of the letters from the string using choose. This letter is the first letter of mnemonic.
After that, make gets the remaining digits from number, one at a time. Each time it gets a digit, it gets the last letter from mnemonic. It also gets a string of letters from follow that can follow the last letter. It temporarily removes from that string all letters which do not appear on the same key for digit, using letters. If the resulting string is empty, then make can’t continue, so it gives up and returns'', the empty string. However, if it is not empty, then make picks one of its letters using choose and concatenates that letter to the end of mnemonic.
The method make continues in this way until it has seen all the digits from number; then it returns the string in mnemonic.
Here is my code for the make function:
I am trying to pick up some projects online to practice coding and so far I have faced a challenge.
If we take a look at the US keypad on a standard keyboard, it has ten keys for the digits 0 through 9. Each key 2 through 9 has three letters of the alphabet, but the letters Q and Z are missing. The keys 0 and 1 have no letters. I am trying to construct a mnemonic word for a telephone number. It might choose letters purely at random. Suppose that the program must construct a word for 686–2377. The possible letters for 6 are MNO, so it might choose M first. The possible letters for 8 are TUV, so it might choose V next, etc. Continuing in this way, it might construct MVNBDPR.
However, I want to create a word that is "pronounce-able", meaning it looks like an english word even though it has no meaning in any dictionary on earth. For example, suppose that the program must still construct a word for 686–2377. The possible letters for 6 are MNO, so it might choose M. The possible letters for 8 are TUV, but since U is more likely to follow M than T or V, it might choose U. Continuing in this way, choosing each letter based on how likely it is to follow the previous letter, the program might construct MUNCERS. This isn’t an English word, but it looks like one, so it’s easier to remember than the number.
Here are my thoughts.
1. I created two classes: one is called Random which is used to generate a random number based on Park-Miller Algorithm and then use that number to generate a random character out of a string. My second class is called Mnenomic. Its methods will generate mnemonic words for telephone numbers.
2. In the Mnemonic, I will have a method called make:
make(self, number)
Here number is a telephone number, made up of digit characters. The method make tries to construct a mnemonic word from number. Let’s say the word it constructs is in the local variable mnemonic.
It starts by getting the first digit from number. Then it gets a string of letters for that digit, using letters. It picks one of the letters from the string using choose. This letter is the first letter of mnemonic.
After that, make gets the remaining digits from number, one at a time. Each time it gets a digit, it gets the last letter from mnemonic. It also gets a string of letters from follow that can follow the last letter. It temporarily removes from that string all letters which do not appear on the same key for digit, using letters. If the resulting string is empty, then make can’t continue, so it gives up and returns'', the empty string. However, if it is not empty, then make picks one of its letters using choose and concatenates that letter to the end of mnemonic.
The method make continues in this way until it has seen all the digits from number; then it returns the string in mnemonic.
Here is my code for the make function:
def make(self,number): self.num = number self.s1='' self.s2='' self.s3='' i = 0 self.s1+=self.random.choose(self.letters[int(self.num[0])]) while i < len(self.num): # self.s1 +=self.random.choose(self.letters[int(self.num)]) self.s2 += self.follow[self.s1[-1]] i=+1 j = 0 while j < len(self.s2): found_char = True if self.s2[j]==self.s1: self.s3+=self.s2[j] found_char = True elif found_char == False: return '' j+=1 self.s1 += self.random.choose(self.letters[self.s3]) return self.s1When I ran my code, I got an error message:
Error:Traceback (most recent call last):
File "/Users/Hieung92/Google Drive/Notability/CSCI 1913 Lab/nguy2952_Project1.py", line 313, in <module>
print('"' + m.make('6862377') + '"') # "ounadrr"
File "/Users/Hieung92/Google Drive/Notability/CSCI 1913 Lab/nguy2952_Project1.py", line 37, in make
if self.s2[j]==self.s1[i]:
IndexError: string index out of range
Any tip will be greatly appreciated. Thank you.