CSCI 141 Computational Problem Solving Homework 3

CSCI 141 Computational Problem Solving
Homework 3
1. Given the list
a = [3, 1, 4, 1, 6]
write segments of Python code that produce each of the following outputs:
a. 3
1
4
1
6
a = [3,1,4,1,6]
for n in a:
print(n)
b. 6
1
4
1
3
for n in a[::-1]:
print(n)
c. [6, 1, 4, 1, 3]
print(a[::-1])
d. (3, 1, 4, 1, 6)
print(a[::-1])
e. [True, True, False, True, False]
print([True if val%2 == 1 else False for val in a])
2. Given the list
a = ['possum loaf', 'potted cat', 'monkey pot pie', 'puppy pate']
what will be printed by each of the following statements?
a. print(a[0:1])
print(a[0:2])
print(a[0:3])
['possum loaf']
['possum loaf', 'potted cat']
['possum loaf', 'potted cat', 'monkey pot pie']
b. print(a[1:])
print(a[:2])
['potted cat', 'monkey pot pie', 'puppy pate']
['possum loaf', 'potted cat']
c. print(a[0][2])
print(a[1][0])
print(a[0][0:6])
s
p
possom
d. print(a[:])
print(a[::2])
['possum loaf', 'potted cat', 'monkey pot pie', 'puppy pate']
['possum loaf', 'monkey pot pie']
3. Show the contents of list a after the following code completes:
a = []
for k in range(2, 6):
a.append([i for i in range(1,k)])
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
4. What will happen if you execute the following code?
b = (3, 1, 4, 1, 6)
b[0] = 42
A TypeError will be generated. Tuples are immutable. You
cannot change the value of a position within a tuple.
5. What are the values of a and c at the end of the following segment of code?
a = [3, 1, 4, 1, 6]
c = [a, 2, 7, 8]
c[0][2] = 42
[3, 1, 42, 1, 6]
[[3, 1, 42, 1, 6], 2, 7, 8]
6. What are the values of a and c at the end of the following segment of code?
a = [3, 1, 4, 1, 6]
c = [a, 2, 7, 8]
c[0] = [19]
[3, 1, 4, 1, 6]
[[19], 2, 7, 8]
7. Provide a list comprehension to fill a list named bob with all the odd numbers
between 1 and 1000 inclusive.
bob = [i for i in range(1,1001) if (i % 2 == 1)]
8. What is the output of the following segment of code?
a = list('ice cream')
print(a)
['i', 'c', 'e', ' ', 'c', 'r', 'e', 'a', 'm']
9. What is the output of the following segment of code?
capital = {}
capital['VA'] = 'Richmond'
capital['WA'] = 'Olympia'
capital['MA'] = 'Boston'
capital['CA'] = 'Sacramento'
print(capital)
for foo in capital:
print(foo)
for foo in capital:
print(capital[foo])
for foo in capital:
print(foo[1])
for foo in capital:
print(capital[foo][0])
{'VA': 'Richmond', 'MA': 'Boston', 'WA': 'Olympia', 'CA': 'Sacramento'}
VA
MA
WA
CA
Richmond
Boston
Olympia
Sacramento
A
A
A
A
R
B
O
S
10. Describe the differences between lists and dictionaries.
To access items in a list, you use a numeric index. Nonnegative indices indicate positions starting at 0 from the
left end of the list or -1 from the right end. Dictionaries
do not support numeric indexing, because they associate
specified keys with values. Instead of accessing the fifth
position, you might access the element mapped from the
key "awesome_key". Keys can be anything you specify,
as long as they are immutable (and hashable, but that is
out of scope). You can iterate over both structures, but
the order of the items in a dictionary may change as
items are inserted.
11. Imagine an English to Pig Latin dictionary as a Python dictionary in the
following fashion. Note that this is an example only. Assume that you
have a complete dictionary.
{"yes": "esyay", "no": "ixnay", "dog": "ogday",
"cat": "atcay", "possum": "ossumpay"}
Write a function called translate() that takes a list of English words and
returns a list with their translation into Pig Latin.
d e f
t r a n s l a t e ( e n g l i s h ) :
r e s u l t
f o r
=
[ ]
w o r d
i n
e n g l i s h :
r e s u l t . a p p e n d ( \
t r a n s _ d i c t [ w o r d ] )
r e t u r n
r e s u l t
12. Write a function called foo() that takes a string as its input, computes the
number of times each character in the string appears, and returns the
result as a dictionary named freq whose keys are the characters in the
string and whose values are the number of times each character appears.
The first and last lines of the function have been provided for you.
d e f
f o o ( s ) :
f r e q
f o r
i f
=
{ }
c h a r
c h a r
i n
i n
l i s t ( s ) :
f r e q :
f r e q [ c h a r ]
+ =
1
e l s e :
f r e q [ c h a r ]
r e t u r n
f r e q
=
1