Hey Folks, We returned with a new Assignment Joy Of Computing Using Python Week 6 Programming Assignment. Copy the code carefully to avoid any indentation errors. They why to wait, let see the answers.Â
Join Telegram:Â Â CLICK HEREÂ for more updates and answers.
Also Read: Nptel Joy of COmputing Week 7 Quiz Answers
NPTEL Joy Of Computing Using Python Week 6 Programming Assignment 2024
Q1. 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:
Le Suppose Letter 1 is a and Letter 2 is a. The distance between letters 1 and 2 is 0. Similarly, if the letter 1 is A and the letter 2 is C. Now, the distance between them is 2.
Program Code:
def distance(word1, word2): """ This function calculates the distance between two words. Args:   word1: The first word.   word2: The second word. Returns:   The distance between the two words. """ 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){codeBox}
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.
Program Code:Â
def ancestry(P, present, past): # Base case: present is the past person, return the list containing only present if present == past:  return([present]) # Check if present is in the dictionary. If not, return None if present not in P:  return(None) # Recursive call with current person's father as present and past remaining the same ancestral_chains = ancestry(P, P[present], past) # If a valid chain is found, prepend present and return if ancestral_chains:  return([present] + ancestral_chains) # No valid chain found, return None return(None) {codeBox}
Q3.   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.
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.
Program Code:Â
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){codeBox}
Conclusion:Â
In the next article you will get week 7. follow
Also Read:Â Nptel Joy of COmputing Week 7 Quiz Answers