cs101 ยป

# CS101 - Unit 2 Homework

## Question 1 - Udacify

``````# Define a procedure, udacify, that takes as
# input a string, and returns a string that
# is an uppercase 'U' followed by the input string.
# for example, when you enter

# print udacify('dacians')

# the output should be the string 'Udacians'

# Make sure your procedure has a return statement.

def udacify(a):
return "U" + a

# Remove the hash, #, from in front of print to test your code.

print udacify('dacians')
#>>> Udacians

print udacify('turn')
#>>> Uturn

print udacify('boat')
#>>> Uboat
``````

## Question 2 - Proc

In this procedure we call another procedure, test(). It's not necessary to know anything about test() other than it takes a number as its input, and it returns a boolean value of True or False.

``````def proc(a, b):
if test(a):
return b
return a
``````

Which of the below listed procedures has exactly the same behavior as the procedure listed above? In order for two procedures to have exactly the same behavior, they must produce the same output and side effects no matter what the inputs are. The code calling the procedure should not be able to see a difference.

1.) def proc1(x, y): if test(x): return y else: return x

x.) def proc2(a, b): if not test(b): return a else: return b

3.) def proc3(a, b): result = a if test(a): result = b return result

4.) def proc4(a, b): if not test(a): b = 'udacity' else: return b return a

## Question 3 - Median

``````# Define a procedure, median, that takes three
# numbers as its inputs, and returns the median
# of the three numbers.

# Make sure your procedure has a return statement.

def bigger(a,b):
if a > b:
return a
else:
return b

def biggest(a,b,c):
return bigger(a,bigger(b,c))

def median():
if a == biggest (a, b, c):
return bigger (b, c)
else:
if b == biggest (a, b, c):
return bigger (a, c)
else:
return bigger (a, b)

#print median(1,2,3)
#>>> 2

#print median(9,3,6)
#>>> 6

#print median(7,8,7)
#>>> 7
``````

## Question 4 - Blastoff!

``````# Define a procedure, countdown, that takes a
# positive whole number as its input, and prints
# out a countdown from that number to 1,
# followed by Blastoff!
# The procedure should not return anything.
# For this question, you just need to call
# the procedure using the line
# countdown(3)

def countdown():
while (n > 0):
print n
n = n - 1
print 'Blastoff!'

#countdown(3)
#>>> 3
#>>> 2
#>>> 1
#>>> Blastoff!
``````

## Question 5 - Finish

For each loop below, answer if the loop always finishes no matter what the positive integer value is assigned initially to n

Hint 1

Hint 2 : 0 is not a positive integer.

``````# This loop always finishes, because i will eventually reach the value of n
n = any positive integer
i = 0
while i <= n:
i = i + 1

# This loop always finishes, because i will eventually reach
# or surpass the value of n, triggering the break
n = any positive integer
i = 1
while True:
i = i * 2
n = n + 1
if i > n:
break

# This is the Collatz Conjecture, one of the great unsolved
# mathematical puzzles
n = any positive integer
while n != 1:
if n % 2 == 0: # the % means remainder, so this tests if n is even
n = n / 2
else:
n = 3 * n  +  1
``````

## Question 6 - Find Last

``````# Define a procedure, find_last, that takes as input
# two strings, a search string and a target string,
# and returns the last position in the search string
# where the target string appears, or -1 if there
# are no occurences.
#
# Example: find_last('aaaa', 'a') returns 3

# Make sure your procedure has a return statement.

def find_last(search, target):
last_found = -1
position = search.find(target)
while position != -1:
last_found = position
position = search.find(target, position + 1)
return last_found

#print find_last('aaaa', 'a')
#>>> 3

#print find_last('aaaaa', 'aa')
#>>> 3

#print find_last('aaaa', 'b')
#>>> -1

#print find_last("111111111", "1")
#>>> 8

#print find_last("222222222", "")
#>>> 9

#print find_last("", "3")
#>>> -1

#print find_last("", "")
#>>> 0
``````