Python Assignment
Python Assignment
Problem 1
Using both indexing and slicing, only on L, assemble and print a new list that
looks like this:
[0, 2, 3, [5 ,6], 8, 10]
"""
Problem 2
Write code, (using for and a while loop) to figure out how often each element
of F occurs within N and print it out.
IMPORTANT: if use the count() function you will only get half points (because
that's just too easy ...)
[ 6 pts ] loops
"""
F = [4,7,2]
N = [2,3,4,2,5,6,3,2,6,7,3,4]
for f in F:
counter = 0
for n in N:
if f == n:
counter += 1
print f, "occurs", counter, "times"
index = 0
numelems = len(F)
while index < numelems:
index += 1
index2 = 0
numelems2 = len(N)
while index2 < numelems2:
index2 += 1
print F, "occurs in N", index, "times"
"""
Problem 3
Ex 3: Function design
Write a function lenumerate() that takes a string (like s) and returns a list
of 2-item lists
containing each word and it's length:
[['But', 3], ['then', 4], ['of', 2], ... ['are', 3], ['nonmigratory', 12]]
Now write a second version of the lenumerate() function (you can name it
lenumerate2() )
that takes as second argument a boolean (i.e. True or False).
Call this 2. argument flip. The default value for flip shall be False.
False => [['But', 3], ['then', 4], ['of', 2], ... ['are', 3], ['nonmigratory',
12]]
If flip is True, the order of the 2 items shall be flipped around (reversed):
True => [3, 'But'], [4, 'then'], [2, 'of'], ... [3, 'are'], [12,
'nonmigratory']]
Call this new version of the function two times, once so that the default
argument is used
and then again with flip given explicitly as a keyword argument(!) with a
value of True
#???
"""
Problem 4
Ex 4: Dictionary
You have dictionary d with these names (as keys) and phone numbers (as
values).
Ask user for a name, search the dict for this name (as key).
If the name exists as key in the dict, print out its phone numbers e.g. Jim
233-5467
If not, get the number for the name and add both to the dictionary,
then print out the full content.
[ 5 pts ] Dictionary
"""
#raw_input("Copy the output into a text file, then hit any key to continue") #
not part of the code for Ex 4!
"""
Problem 5
"""
L1 = [334,43,75]
print L1, id(L1)
# 1) What happens at the = ?
# Answer: The = creates a list which can be refered to as L1
# 2) What is [334,43,75]?
# Answer: This is a list of integers
L1 = L1 + [ len(L1) ]
print L1
# 3) Syntactically, what is something like + called in Python? (I know it's
called a plus!)
# Answer: .......
L2 = L1
print L1, id(L1), L2, id(L2)
# 5) What relationship between L1 and L2 is created here?
# Answer: .......
L3 = L1[:]
print L3, id(L3), L1, id(L1), L2, id(L2)
# 6) What happens here? (how is this different from L3 = L1?)
# Answer: .......
L2.sort()
print L1, id(L1), L2, id(L2), L3, id(L3)
# 7) Why is L1 now also sorted but L3 is still unsorted?
# Answer: .......
print sorted(L3)
print L3, id(L3)
# 8) How does using sort() differ from sorted()?
# Answer: .......
"""
Problem 6
Problem 6: file conversion: read in, process and print out data
[ 3 pts ] Read the text file into a string and split into lines
[ 5 pts ] Put the records into a data structure
[ 2 pts ] Capitalize the sketch titles
[ 2 pts ] Sort the records alphabetically according to ID
[ 4 pts ] Print the table with a header row
"""
# Task 5: print the sorted table on the console, make sure to line up the
comumns with
# one space in between ID, Date and Title, like shown below. Use string
formatting and programatically
# determine the widest ID and widest Date beforehand (i.e. do not hardcode it
to 8, loop through
# all IDs and find out how many letters the longest ID has!)
# Also, no NOT just use a third party table formatter like prettytable or
tabulate, use standard python only!