Homework 4

1. right_justify

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def right_justify(name):
    print name.rjust(70)

if __name__ == "__main__":
    right_justify("yourfirstname")

print 'this is a cll to the right_justify module'


    

2. do_wtice

Solution:

1
2
3
4
5
6
7
8
def do_twice(f,dude):
      f(dude)
      f(dude)
      
def print_twice(dude):
      print dude

do_twice(print_twice,"spam")

3. is_sorted

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def is_sorted(kobe):
    if isinstance(kobe,list)==True:
        if sorted(kobe)==kobe:
            print 'True'
        else:
            print 'False'
    else:
        print 'get a life, give me a list bro'
     
if __name__ == "__main__":
    is_sorted([1,2,3,4])
    is_sorted(['b','a'])
 
print 'this is a call to the is_sorted module!'

4. cumsum

Solution:

1
2
3
4
5
6
7
8
def cumsum(kobe):
    if isinstance(kobe,list):
        dd=[0]*len(kobe)
        for i in range(0,len(kobe)):
            dd[i]=sum(kobe[0:(i+1)])
        print dd
    else:
        print "list please"