Dear students,
Submit the postwork-2 to T&P cell on or before 15th March 2015. We will not consider any submission after this date. Please submit your own work. Avoid copying from other groups, may lead to cancellation of both the teams’ from participation in this webinar series.
Post Work 2 – Questions:
1.
Usually programs loop until the user indicates they
want to stop. Write a function named “stay()” that takes no arguments. The
function should print the message “continue (yes/no)?” and wait for the user to
respond. If the user types “Yes”, “y” or “Y”, then return true, otherwise
return false.
2. Write a python program to
print Pascal’s triangle.
3. Write a python program to
generate following outputs.
(a) 1 (b) 1
2 2 1 2
3 3 3 1 2 3
4 4 4 4 1 2 3 4
5 5 5 5 5 1 2 3 4 5
4. What does a function return
if it has no return statement in it?
5.
What is the output? And why?
a.
s = “India”
def
display():
display() # function call
b.
s = “India”
def
display():
s=”Infosys”
display() # function call
print(s)
c.
s = “India”
def display():
s=”Infosys”
print(s)
display() # function call
print(s)
d.
s = “India”
def
display():
global s
s=”Infosys”
display() # function call
print(s)
e.
s = “India”
def
display():
s=”Infosys”
def inside():
print(s)
inside() #inner function call
display() # function call
print(s)
6.
Write a python program to convert a decimal number
into hexadecimal number.
7.
If a is a number and n is a positive integer, the
quantity an can obviously be computed by multiplying n times. A much
faster algorithm uses the following observations. If n is zero, an
is 1. If n is even, an is the same as (a * a)n/2. If n is
odd, an is the same as a * an-1. Using these
observations, write a recursive function for computing an.
8.
Try executing the following program. Can you explain
what is going on?
def outer(x):
def inner(y):
return x + y
return inner
x = outer(3)
print x(4)
9.
Find the output and give reasons
a.
def display(a, b=4, c=5):
print(a,b,c)
display(1,2) #function call
b.
def display(a,b,c=5):
print(a,b,c)
display(1,
c=3, b=2) #function call
c.
def display(a, *args)
print(a,
args)
display(4,
5, 7) #function call
d.
def display(a,b,c=3,d=4) : print(a,b,c,d)
display(1,
*(5,6))
e.
Display odd numbers between 0 to 100 using for and
range
10. A palindrome is a string that reads the same both
forwards and backwards, for example the word “rotor” or the sentence “rats live
on no evil star”. Testing for the palindrome property is easy if you think
recursively. If a string has zero or one character, it is a palindrome. If it
has more than two characters, test to see if the first character matches the
final character. If not, then the word is not a palindrome. Otherwise, strip
off the first and final characters, and recursively perform the palindrome
test. Write a recursive function using this approach.
Bonus Problem:
Try to generate a python program to solve the towers of Hanoi problem. In
this problem there are three poles labeled A, B and C. Initially a nmber of
disks of decreasing size are all on pole A. As you aware, the goal of the puzzle
is to move all disks from pole A to pole B without ever moving a disk onto another
disk with smaller size. The third pole can be used as a temporary during this
process.
If you try to solve this problem in a conventional fashion you will
quickly find yourself frustrated trying to determine the first move. Should you
move the littlest disk from A to B, or from A to C? But the recursive version
is very simple. Rewrite the problem as a call on Hanoi(n, x, y, z) where n is
the size of the stack, and x, y and z are strings representing the starting
pole (initially ‘A’), the target pole (initially ‘B’) and the temporary pole
(initially ‘C’). The task can be thought of recursively as follows. If n is 1,
then move one disk from pole x to pole y. Otherwise, recursively call Hanoi to
move n-1 disks from x to z, using y as the temporary. Then move one disk from x
to y, which leaves pole x empty. Finally move n-1 disks from pole z to pole y,
using x as the temporary. Write the towers of Hanoi as a recursive function, printing
out a message each time a disk is moved from one pole to another.