The Joy of Computing Using Python Week 8 Solutions 2024

ABOUT THE COURSE :


Disclaimer: The answers to these questions are provided for educational and informational purposes only. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself.

A fun filled whirlwind tour of 30 hrs, covering everything you need to know to fall in love with the most sought after skill of the 21st century. The course brings programming to your desk with anecdotes, analogies and illustrious examples. Turning abstractions to insights and engineering to art, the course focuses primarily to inspire the learner's mind to think logically and arrive at a solution programmatically. As part of the course, you will be learning how to practice and culture the art of programming with Python as a language. At the end of the course, we introduce some of the current advances in computing to motivate the enthusiastic learner to pursue further directions.

Summary
Course Type Elective
Duration 12 weeks
Category Computer Science and Engineering
Start Date 22 Jan 2024
End Date 12 Apr 2024
Exam Registration Ends 16 Feb 2024
Exam Date 21 Apr 2024 IST

Assignment Solutions :
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 :
  1. Average assignment score = 25% of average of best 8 assignments out of the total 12 assignments given in the course.
  2. Exam score = 75% of the proctored certification exam score out of 100
  3. Final score = Average assignment score + Exam score
YOU WILL BE ELIGIBLE FOR A CERTIFICATE ONLY IF AVERAGE ASSIGNMENT SCORE >=10/25 AND EXAM SCORE >= 30/75. If one of the 2 criteria is not met,you will not get the certificate even if the Final score >= 40/100.

Join With Us On :

Join Now


Previous Year Questions Now to Boost Your Exam Performance

LOADS OF LOGIC