Hey, Folks. This article is with NPTEL Joy of Computing Using Python Week 11 Programming Assignment Answers. Copy the code carefully to avoid any indentation errors. Let See the answers with no excitement.
Join Telegram:Â Â CLICK HEREÂ for more updates and answers.Â
Also Read: NPTEL Joy Of Computing Using Python Week 11 Quiz Assignment AnswersÂ
NPTEL Joy Of Python Using Python Week 11 Programming Assignment AnswersÂ
Q1. Write the following functions:
(1) factors: accept a positive integer n as argument. Return the set of all factors of n.
(2) common_factors: accept two positive integers a and b as arguments. Return the set of common factors of the two numbers. This function must make use of factors.
(3) factors_upto: accept a positive integer n as an argument. Return a dict D, whose keys are integers and values are set. Each integer in the range [1,n], endpoints inclusive, is a key of D. The value corresponding to a key, is the set of all factors of key. This function must make use of factors.
The idea we are trying to bring out here is to make use of pre-defined functions whenever needed.Â
Program Code:Â
def factors(n):  """  Accepts a positive integer n as an argument.  Returns the set of all factors of n.  """  factor_set = set()  for ips in range(1, n + 1):    if n % ips == 0:      factor_set.add(ips)  return factor_setdef common_factors(a, b):  """  Return the set of common factors of a and b.  """  factors_a = factors(a)  factors_b = factors(b)  return factors_a.intersection(factors_b)def factors_upto(n):  """  Return a dict D, whose keys are integers and values are sets.  Each integer in the range [1,n], endpoints inclusive, is a key of D.  The value corresponding to a key is the set of all factors of the key.  """  factors_dict = {}  for i in range(1, n + 1):    factors_dict[i] = factors(i)  return (factors_dict) {codeBox}
Q2. A clockwise rotation of a list consists of taking the last element and moving it to the beginning of the list. For instance, if we rotate the list [1, 2, 3, 4, 5], we get [5, 1, 2, 3, 4]. If we rotate it again, we get [4, 5, 1, 2, 3]. Your task is to perform k rotations of a list.
The first line of input contains a non-empty sequence of comma-separated integers. The second line of input is a positive integer k. Perform k rotations of the list and print it as a sequence of comma-separated integers.
Program Code:Â
def rotate_clockwise(lst, k):  npk = len(lst)  k %= npk  rotated_list = lst[-k:] + lst[:-k]  return rotated_listinput_list = input().split(",") Âk = int(input())Ârotated_result = rotate_clockwise(input_list, k)print(",".join(rotated_result)) {codeBox}Â
Q3. Consider a spiral of semicircles. We start at a point P0​ on the x-axis with coordinates (l,0). The first arm of the spiral ends at P1​ with coordinates (r,0). The second arm of the spiral starts at P1​ and ends at the center of the first arm, P2​. The third arm starts from P2​ and ends at P3​ which happens to be the center of the second arm. And finally, the fourth arm starts at P3​ and ends at P4​, the center of the third arm.
Write two functions named spiral_iterative and spiral_recursive, each of which accepts three arguments:
left: x-coordinate of the point P0​
right: x-coordinate of the point P1​
n: the number of arms in the spiral
Both functions should return the x-coordinate of Pn, the point at which the nth arm of the spiral ends.
Program Code:Â
def spiral_iterative(left, right, n):  for ipl in range(1,n):   center = (left + right)/2   if ipl % 2 :    left = center   else:    right = center  return center  Âdef spiral_recursive(left, right, n):  if n == 1:    return right  else:    center = (left + right) / 2    return spiral_recursive(right, center, n - 1) {codeBox}Â