Homework:
Given a 7 digit phone number input, determine the word possibilities. Input will not use 0 or 1 since they have no letters associated so the phone number input cannot contain either digit; only digits 2 - 9 are permitted in the phone number sequence.
I know one method (algorithm). Are there others ? Is mine correct ? A better way with STL ?
Here is one way: The number of possible words is equal to 3^7 or 2187. I can build an array called word of strings with each element being a 7 char string. There would be 2187 elements in the array. 2187 / 3 = 729. If the first digit in the phone number were 5, (using a c-string in this model) elements word1[0] thru word728[0] = J while word729[0] thru word1457[0] = K and from word1458[0] to 2187[0] = L .
the second element in the c-string will be assumed to be in accord with the digit 2. So going down the list, I will repeat it 243 times for each of the 3 letters. There will be 9 repitition blocks of each letter, whereas in the first element there were 3 repetition blocks across the entire 2187 words.
In the third element, word1[2] etc, there will be 27 repitition blocks so each of the three letters will be written consecutively 2187 /27 = 81 times. If the digit were 3 then the letter D would be written 81 times, then E would be recoreded in the 3rd element 81 times and then F would be written 81 times. This procedure would loop 9 times (3 x 9 = 27 repitition blocks total) to fill the 2187 words with a third element possibility.
In a 7 digit phone number where there are 3 alpha characters >>
--------------------------
----------
----------
----------
----------
----------
-
count c 1 2 3 4 5 6 7
element e 0 1 2 3 4 5 6
loops 3^e 1 3 9 27 81 243 729
writes 2187/ (3^c) 729 243 81 27 9 3 1
--------------------------
----------
----------
----------
----------
----------
--
In the last sequence, each letter would be written once and the the set of three would again be repeated: 729 loops in all to fill out the array of 2187 strings.
Finally, the array could be sorted by alpha.
There must be a more eloqent means.
Considering permutations of 24 elements taken 7 at a time won't apply here since only 3 letters are used to fill each of 7 digits.
Please advise. I am new to STL so there may be something of use there that I am unaware of beyond the power algorithm and the variety of sort options there. Thank you.