Lecture 38: Practice!

posted Apr 10, 2013, 6:01 AM by Samuel Konstantinovich   [ updated Apr 10, 2013, 7:06 AM ]
Goals:

Practice problems.

Go over file labs.
Regarding labs:
  If you have problems opening, or reading information from a file, refer to lecture 36a.

Complete these problems on looseleaf:
1. What prints when the code is run?
H=[1,3,9,5,2]
for x in H:
    x=x**2
print H

2. What prints when the code is run? 
Y=[4,0,3,1,2]
for i in range(len(Y)-1):
    Y.append(Y.pop(0))
print Y

3. What prints when this code is run?
G=[2,3,1,0,4]
for i in range(len(G)):
    G[G[i]]=i
print G

4. What will print when you run this code?
def f(x):
   y = 2 * x + 1
   print y
z = f(3)
print z

5a. What is contained in y?
x=[]
for i in range(1,11,2):
    x+=[i]
y=x[1:len(x)-1]

5b. Using y from above, what is printed?
for j in y:
    print j*chr(ord('a')+j)


6. Copy the code, and write the corrections to the right of the code: (syntax errors only)
def miste(r,e)
    x=[]
    while r < e
        x.append(r)
        r=r+1
    y=[]
    1=i
    while i<=len(x):
        x[-i]*=e
        if i%2=0:
            y=y+i
        i.append(1)
    print x,y

7.Challenge:
x = open('a.txt','r')
y = open('b.txt','r')
z = open('c.txt','w')
g = x.read().split()
h = y.read().split()
x.close()
y.close()
for y in range(len(g)):
    if y%3==0 and y>0:
        z.write('\n')
    z.write(g[y]*int(h[y])+" ")
z.close()

'''a.txt contains:
Think what will 
happen when you
run the code
'''
'''b.txt contains:
1 4 5
2 5 3
8 7 1 
'''

Comments