Question

In: Computer Science

•• P11.1 Phone numbers and PIN codes can be easier to remember when you find words...

•• P11.1 Phone numbers and PIN codes can be easier to remember when you find words that
spell out the number on a standard phone keypad. For example, instead of remembering
the combination 2633, you can just think of CODE.
Write a recursive function that, given a number, yields all possible spellings (which
may or may not be real words).
•• P11.2 Continue Exercise P11.1, checking the words against the /usr/share/dict/words file on
your computer, or the words.txt file in the companion code for this book. For a given
number, return only actual words.
••• P11.3 With a longer number, you may need more than one word to remember it on a
phone pad. For example, 846-386-2633 is TIME TO CODE. Using your work from
Exercise P11.2, write a program that, given any number, lists all word sequences that
spell the number on a phone pad.
••• P11.4 Change the permutations function of Section 11.4 (which computed all permutations
at once) to a PermutationIterator (which computes them one at a time).
class PermutationIterator
{
public:
PermutationIterator(string s) { . . . }
string next_permutation() { . . . }
bool has_more_permutations() { . . . }
};
Here is how you would print out all permutations of the string "eat":
PermutationIterator iter("eat");
while (iter.has_more_permutations())
{
cout << iter.next_permutation() << endl;
}
Now we need a way to iterate through the permutations recursively. Consider the
string "eat". As before, we’ll generate all permutations that start with the letter 'e',

then those that start with 'a', and finally those that start with 't'. How do we generate
the permutations that start with 'e'? Make another PermutationIterator object
(called tail_iterator) that iterates through the permutations of the substring "at". In
the next_permutation function, simply ask tail_iterator what its next permutation is,
and then add the 'e' at the front. However, there is one special case. When the tail
iterator runs out of permutations, all permutations that start with the current letter
have been enumerated. Then
• Increment the current position.
• Compute the tail string that contains all letters except for the current one.
• Make a new permutation iterator for the tail string.
You are done when the current position has reached the end of the string.
••• P11.5 The following program generates all permutations of the numbers 0, 1, 2, ... , n – 1,
without using recursion.
#include <iostream>
#include <vector>
using namespace std;
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
void reverse(vector<int>& a, int i, int j)
{
while (i < j)
{
swap(a[i], a[j]); i++; j--;
}
}
bool next_permutation(vector<int>& a)
{
for (int i = a.size() - 1; i > 0; i--)
{
if (a[i - 1] < a[i])
{
int j = a.size() - 1;
while (a[i - 1] > a[j]) { j--; }
swap(a[i - 1], a[j]);
reverse(a, i, a.size() - 1);
return true;
}
}
return false;
}
void print(const vector<int>& a)
{
for (int i = 0; i < a.size(); i++)
{
cout << a[i] << " ";
}
cout << endl;

}
int main()
{
const int n = 4;
vector<int> a(n);
for (int i = 0; i < a.size(); i++) { a[i] = i; }
print(a);
while (next_permutation(a)) { print(a); }
return 0;
}
The algorithm uses the fact that the set to be permuted consists of distinct numbers.
Thus, you cannot use the same algorithm to compute the permutations of the
characters in a string. You can, however, use this technique to get all permutations of
the character positions and then compute a string whose ith character
is s[a[i]]. Use
this approach to reimplement the generate_permutations function without recursion.
•• P11.6 Refine the is_palindrome function to work with arbitrary strings, by ignoring nonletter
characters and the distinction between upper- and lowercase letters. For
example, if the input string is
"Madam, I’m Adam!"
then you’d first strip off the last character because it isn’t a letter, and recursively
check whether the shorter string
"Madam, I’m Adam"
is a palindrome.

Solutions

Expert Solution

P11.1 :

CODE:

=====================================================================================

#include <bits/stdc++.h>

using namespace std;
//taking the souce characters for each button
const char source[10][5] = {"", "", "ABC", "DEF", "GHI", "JKL","MNO", "PQRS", "TUV", "WXYZ"};


//recursive function "digits"
void digits(int m[], int x, char codes[], int y)
{
    int n;
    if (x == y)
    {
  //priting "," after every string
        cout<<codes<<", ";
        return;
    }
//using for loop
    for (n=0; n<strlen(source[m[x]]); n++)
    {
        codes[x] = source[m[x]][n];
//calling function recursively
        digits(m, x+1, codes, y);
        if(m[x] == 0 || m[x] == 1)
        {
            return;
        }
    }
}
//function "check" to call the function
void check(int m[], int y)
{
    char codes[y+1];
    codes[y] ='\0';
    digits(m, 0, codes, y);
}

int main(void)
{
//input code to print
    int m[] = {2,6,3,3};
    int y = sizeof(m)/sizeof(m[0]);
    check(m, y);
    return 0;
}

=======================================================================================


Related Solutions

To make telephone numbers easier to remember, some companies use letters to show their telephone number....
To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letters. Instructions Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone...
JAVA PROGRAMMING project 1: Businesses want phone numbers that are easy to remember, and one that...
JAVA PROGRAMMING project 1: Businesses want phone numbers that are easy to remember, and one that can be tied to a phone number are even better. Given the “standard international keypad”, write anaysis and design to determine the phone number based on a user-supplied 7-letter phrase. 1 2 ABC 3 DEF 4 GHI 5 JKL 6 MNO 7 PQRS 8 TUV 9 WXYZ * 0 # Test cases: ● BadDogs ● GoodCat ● Glasses ● EatGood ------------------------------------------------------------------------------------------------------------------------ I ONLY NEED...
Answer the following questions. Remember that you can only enter numbers in the boxes given and...
Answer the following questions. Remember that you can only enter numbers in the boxes given and that decimal places are separated with a dot (not with a comma). Do not leave empty boxes, otherwise partial marks will not be given. Also, enter at least 4 decimals in your answers whenever possible. Suppose you have the following AD and AS equations: ?=82−0.8?Y=82−0.8π (AD equation) ?=−7+1.8?Y=−7+1.8π (Short Run AS equation) ?=49.15Y=49.15 trillions (Long Run output) a) The real GDP in equilibrium the...
What do you find challenging about facing a job interview? How can you make it easier...
What do you find challenging about facing a job interview? How can you make it easier for yourself?
“Don’t believe those phone numbers when you hear 4.9 and 5 percent unemployment. The number’s probably...
“Don’t believe those phone numbers when you hear 4.9 and 5 percent unemployment. The number’s probably 28, 29, as high as 35. In fact, I even heard recently 42 percent.” Donald Trump, 2/9/16, in a victory speech after winning the New Hampshire primary “There are all sorts of ways the government plays with statistics. For example, have you noticed lately the unemployment rate has gone down? You would think ‘Oh, this is a good thing. My government is telling me...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." In C, Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume the length...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C language to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters....
You will write a program that prompts the user to enter a 7-digit phone numbers, and...
You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run: unixlab% java MapNumbers Enter name of dictionary file: words10683 Enter a test word (3 letters): cat Test word maps to 228 Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273 Options for first 3 digits: act cat bat Options...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT