Lecture 30 + HW:

posted Mar 18, 2013, 5:54 AM by Samuel Konstantinovich   [ updated Mar 18, 2013, 1:39 PM ]
Homework is do-now bulletpoint 3 (in red text)

Announcements/Reminders:
 -Apologies: Your quizes are not ready yet. (In case your parents ask, this note is on the website to show them) 
 -If you aren't doing as well as you would like to in class, I want to remind you that you have access to online tutorials/references, tutoring, office hours, challenge problems, etc. 

Do now:
Log In while you do this on paper:
(Note: any time you are asked to do something on paper, that is a good way to figure out how well you would do on a test.)
1. Write a function: makeBarGraph(L) that takes a list of non-negative integers and returns a string of text that looks like a bar graph in the format below. (The index prints on the left side, and the number of equal signs is the value at that index.)

makeBarGraph([4,2,3]) returns: '0:====\n1:==\n2:===\n'  when you print it it looks like this:
0:====
1:==
2:===

makeBarGraph([3,0,1,2]) returns: '0:===\n1:\n2:=\n3:==\n' when you print it it looks like this:
0:===
1:
2:=
3:==

Phase II: On the computer: 
2. Test the function you wrote on paper.

HOMEWORK:
3. Make the bar graph vertically: (easier upside down, slightly harder rightside up)
makeBarGraph([3,0,1,2])  would give you:
0123
= ==
=  =
=
makeBarGraph([4,2,3])  would give you:
012
===
===
= =
=

New Stuff:
I. Explicitly go over nested loops

What would this output: (trace it on paper)
print foo1(4,5)
print foo1(6,4)

def foo1(a,b):
    i=0
    ans=''
    while i<a:
        j=0
        while j<b:
            ans+=str(i*j)
            j+=1
        i+=1
        ans+='\n'
    return ans

#Foo1 and Foo2 are equivalent
#Foo2 uses a second function instead of a loop, but it is the same result.
def bar(i,b):
    ans=''
    j=0
    while j<b:
        ans+=str(i*j)+'\t'
        j+=1
    ans+='\n'
    return ans

def foo2(a,b):
    i=0
    ans=''
    while i<a:
        ans+=bar(i,b)
        i+=1
    return ans

II. Go Over HW for periods 7/10

III. Range Command
Test these commands and try to predict what would happen.
range(10)
range(12)
#can you predict range(3) ?

range(2,10)
range(4,15)
#can you predict range(5,7)?

range(5,15,2)
range(5,15,3)

#can you predict range(2,13,2) ?
Comments