Starting from:

$29.99

CS104 Lab 6 Solution


1. Write program that reads in a string on the command line and returns a table of the letters of the alphabet in alphabetical order which occur in the string together with the number of times each letter occurs. Case should be ignored. This should involve writing a function that takes in a string and returns a dictionary with these letters and counts. A sample run of the program would look like:

Enter a sentence >>> The cat in the hat a 2 c 1 e 2
h 3
i 1
n 1
t 4


2. Write a function that takes in two strings. If the strings are anagrams of one another, return True. If not, return False. Two strings are anagrams of one another if the characters of one can be rearranged to make the other.


3. We have the following dictionaries given for English to French, and French to English translation. Write a translator program that asks for an input sentence, and a string denoting whether you want to translate from French to English, or from English to French. Then, prints out the translation.

EtoF = {'bread':'pain', 'wine':'vin', 'with':'avec', 'I':'Je',
'eat':'mange', 'drink':'bois', 'John':'Jean',
'friends':'amis', 'and': 'et', 'of':'du','red':'rouge'}
FtoE = {'pain':'bread', 'vin':'wine', 'avec':'with', 'Je':'I',
'mange':'eat', 'bois':'drink', 'Jean':'John',
'amis':'friends', 'et':'and', 'du':'of', 'rouge':'red'}


4. Write a program that simulates rolling two six-sided dice 1,000 times and displays a table summarizing the total of two dice by expressing the frequency for each total as a percentage of the number of rolls performed. You can also show expected frequencies together with observed frequencies. See output below:

Total Simulated Expected
Percent Percent
2 2.80 2.78
3 5.50 5.56
4 7.30 8.33
5 11.60 11.11
6 13.50 13.89
7 16.30 16.67
8 13.60 13.89
9 10.70 11.11
10 9.30 8.33
11 6.10 5.56
12 3.30 2.78

Design steps can be summarized as below:

Begin by writing a function that simulates rolling a pair of six-sided dice. Your function will not take any parameters. It will return the total that was rolled on two dice as its only result. Then, write a main program that uses your function to simulate rolling two six-sided dice 1,000 times. As your program runs, it should count the number of times that each total occurs. Then it should display a table that summarizes this data by expressing the frequency for each total as a percentage of the number of rolls performed.


More products