0% found this document useful (0 votes)
522 views

Python Assignment

This document contains 6 problems related to data processing and Python programming concepts. It includes tasks involving reading a text file into a data structure, processing the data by capitalizing titles and sorting records, and printing the records in a formatted table.

Uploaded by

Ravi Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
522 views

Python Assignment

This document contains 6 problems related to data processing and Python programming concepts. It includes tasks involving reading a text file into a data structure, processing the data by capitalizing titles and sorting records, and printing the records in a formatted table.

Uploaded by

Ravi Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

"""

Problem 1

HCI 574 2016 midterm

Ex 1: Index and slicing

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]

[ 3 pts ] Index and slicing


"""
L = [0, [], [1,2,3,4], [[5],[6,7]], [8,9,10]]
tmp = L[0] # Gets first number (0) in list
tmp1 = L[2] # Gets second two numbers (2,3) in list
tmp2 = tmp1[1:2][0] # Gets the number 2
tmp3 = tmp1[2] # Gets the number 3
tmp4 = L[-1] # Gets last 3 numbers in list (8,9,10)
tmp5 = L[3] # Gets the 3rd set of numbers in list ([[5], [6, 7]]
tmp6 = tmp5[0][0] # Gets the number 5 out of brackets
tmp7 = L[3][1][0] # Gets the number 6 out of brackets
newL = [tmp, tmp2, tmp3, [tmp6, tmp7], tmp4[0], tmp4[2]] # Gets entire list
print "newL is", newL # should show this: [0, 2, 3, [5 ,6], 8, 10]

"""
Problem 2

Ex 2: For and while loops

Write code, (using for and a while loop) to figure out how often each element
of F occurs within N and print it out.

This should print:


4 occurs in N 2 times
7 occurs in N 1 times
2 occurs in N 3 times

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"

print "F is", F


print "N is", N

"""
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]]

[ 3 pts ] Function design - simple

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.

If flip is False the list described above will be returned:

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

[ 3 pts ] with 2. arg


"""

# define the function first ...


def lenumerate(s):
'''
lenumerate(s) return s as a list of 2-element lists: [[word, length],
[word, length], ...]
'''
# convert string s into a list of 2-element-lists

# ... then call the function


s = "But then of course African swallows are nonmigratory"
l = lenumerate(s)
e = s.split()
print l

# 2. version of lenumerate(), takes a second arg flip (True/False)


#def lenumerate2(????): # <- you still need to define the two arguments here!
#'''
#lenumerate2() returns a list of 2-element lists: [[word, length], [word,
length], ...]
#a second agument flip (True/False, defaults to False) determines if the
order 2-element list
#is flipped. With flip given as True, the length is first and the word is
second:
#False: [word, length], [word, length], ...]
#True: [[length, word], [length, word], ...]
#'''

#???

# ... then call the 2. version of the function


s = "But then of course African swallows are nonmigratory"
#l = lenumerate2(???) # <- call so that the default arg is used, i.e. output
will NOT be flipped
print l
#l_flipped = lenumerate2(????) # set flip to True via keyword argument, so
the output is flipped
#print l_flipped

"""
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
"""

d = {'Jim':'233-5467', 'Bob':'643-7894', 'Anne':'478-4392', 'Jill':'952-4532'}


print d
name = "Jim"# or "Bob" or "Chris" - start with hardcoding the name, then get
input from user
username = raw_input( "Please enter name: ")

#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

Ex 5: Objects, Assignment, reference, function call, argument, operators, in-


place change

Answer the following questions in English in the spaces provided below.

[ 4 pts ] python questions

"""
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: .......

# 4) What is len() and what role has the L1 in len(L1)?


# 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 1: Reading the file line by line


f = open('database.txt', 'r')
full_text = f.read()
print repr(full_text)
lines = full_text.splitlines()
for l in lines:
print l

# Task 2: Collect each record in a data structure

# Task 3: Convert all Titles to a titlecased (Capitalized) version

# Task 4: sort the records alphabetically according to ID

# 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!

#ID Date Title


#d45j5jkd 28 December 1969 The Royal Philharmonic Orchestra Goes To The
Bathroom
#f983 22 December 1970 Royal Episode 13 (Or: The Queen Will Be Watching)
print "Done"

You might also like