Programming Assignment :
1. Programming Assignment | Week 6
The
distance between two letters in the English alphabet is one more than the
number of letters between them. Alternatively, it can be defined as the number
of steps needed to move from the alphabetically smaller letter to the larger
letter. This is always a non-negative integer. For example:

The distance between two words is defined as follows:
- It is -1, if the words are of unequal lengths.
- If the word-lengths are equal, it is the sum of the distances between letters at corresponding positions in the words. For example:
dword(dog,cat)=dletter(d,c)+dletter(o,a)+dletter(g,t)=1+14+13=28
Write a function named distance that accepts two words as arguments and returns the distance between them.
You do not have to accept
input from the user or print output to the console. You just have to write the
function definition.
def distance(word1, word2):
if len(word1) != len(word2):
return(-1)
dis = 0
for i in range(len(word1)):
dis += abs(ord(word1[i]) - ord(word2[i]))
return(dis)
2. Programming Assignment | Week 6
1.
P is
a dictionary of father-son relationships that has the following structure: for
any key in the dictionary, its corresponding value is the
father of key. As an example:
P = {
'Jahangir': 'Akbar',
'Akbar': 'Humayun',
'Humayun': 'Babur'
}
If 'Jahangir' is
the key, then 'Akbar', his father, is the
value. This is true of every key in the dictionary.
Write a recursive
function named ancestry that accepts the
following arguments:
- P: a dictionary of
relationships
- present: name of a person,
string
- past: name of a person,
string
It should return the
sequence of ancestors of the person named present,
traced back up to the person named past.
For example, ancestry(P, 'Jahangir', 'Babur') should
return the list:
L = ['Jahangir', 'Akbar',
'Humayun', 'Babur']
In more Pythonic
terms, L[i] is the father of L[i - 1],
for 1≤ i < len(L), with the condition that L[0] should
be present and L[-1] should
be past.
(1) You can assume that
no two persons in the dictionary have the same name. However, a given person
could either appear as a key or
as a value in the dictionary.
(2) A given person could
appear multiple times as one of the values of the dictionary. For example, in
test-case-2, Prasanna has two sons, Mohan and Krishna, and hence appears twice
(as a value).
(3) You do not have to
accept input from the user or print output to the console. You just have to
write the function definition.
def ancestry(P, present, past):
if present == past:
return([present])
if present not in P:
return(None)
ancestral_chains = ancestry(P, P[present], past)
if ancestral_chains:
return([present] + ancestral_chains)
return(None)
3. Programming Assignment | Week 6
1.
Fibonacci
Fibonacci is a young
resident of the Italian city of Pisa. He spends a lot of time visiting the
Leaning Tower of Pisa, one of the iconic buildings in the city, that is
situated close to his home. During all his visits to the tower, he plays a
strange game while climbing the marble steps of the tower.
The Game
Fibonacci likes to climb
the steps either one at a time, two at a time, or three at a time. This adds
variety to the otherwise monotonous task of climbing. He wants to find the
total number of ways in which he can climb n steps, if the
order of his individual steps matters. Your task is to help Fibonacci compute
this number.
For example, if he wishes
to climb three steps, in the case of n=3, he could do it in four
different ways:
- (1,1,1) (1,1,1): do it in three
moves, one step at a time
- (1,2) (1,2): do it in two moves,
first take a single step, then a double step
- (2,1) (2,1): do it in two moves,
first take a double step, then a single step
- (3): do it in just one move, directly
leaping to the third step
To take another example,
if n=5, then some of the sequences could be:
(1,3,1), (1,1,3),
(3,1,1), (2,1,1,1), (1,2,1,1), (2,1,2)
Each sequence is one of
the ways of climbing five steps. The point to note here is that each element of
a sequence can only be 1, 2, or 3.
Write a recursive
function named steps that accepts a
positive integer n as the argument. It should return the total
number of ways in which Fibonacci can ascend n steps. Note
that the order of his steps is important.
You do not have to accept
input from the user or print output to the console. You just have to write the
function definition.
def steps(n):
if n <= 0:
return 0
elif n == 1:
return 1
elif n == 2:
return 2
elif n == 3:
return 4
one_steps = steps(n-1)
two_steps = steps(n-2)
three_steps = steps(n-3)
return(one_steps + two_steps + three_steps)
CRITERIA TO GET A CERTIFICATE :
- Average assignment score = 25% of average of best 8 assignments out of the
total 12 assignments given in the course.
- Exam score = 75% of the proctored certification exam score out of 100
- Final score = Average assignment score + Exam score
Follow Us