Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL The Joy of Computing using Python Jan-2024 Week 11 | Swayam

NPTEL The Joy of Computing using Python Jan-2024 Week 11 | Swayam

 


  Please scroll down for latest Programs ðŸ‘‡ 



1. Programming Assignment | Week 11

Due on 2024-04-11, 23:59 IST

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 argument. 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 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.


You do not have to accept input from the user or print output to the console. You just have to write the definition of all three functions. Each test case will correspond to one function call.

 

Your last recorded submission was on 2024-03-31, 10:27 IST
Select the Language for this assignment. 
1
def factors(n):
2
    """
3
    Compute the set of factors of n
4
            
5
    Argument:
6
        n: integer
7
    Return:
8
        factors_of_n: set
9
    """
10
    factorset = set()
11
    for i in range(1,n+1):
12
      if n%i == 0:
13
        factorset.add(i)
14
    return(factorset)
15
16
def common_factors(a, b):
17
    """
18
    Compute the set of common factors of a and b
19
20
    Arguments:
21
        a, b: integers
22
    Return:
23
        factors_common: set
24
    """
25
    return(factors(a).intersection(factors(b)))
26
27
def factors_upto(n):
28
    """
29
    Get the factors up to n 
30
    
31
    Argument:
32
        n: integer
33
    Return:
34
        result: dict (keys: integers, values: sets)
35
    """
36
    D = {}
37
    for i in range(1,n+1):
38
      D[i] = factors(i)
39
    return(D)
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
factors(10)
{1, 2, 10, 5}
{1, 2, 10, 5}\n
Passed after ignoring Presentation Error
Test Case 2
common_factors(10, 5)
{1, 5}
{1, 5}\n
Passed after ignoring Presentation Error
Test Case 3
factors_upto(4)
{1: {1}, 2: {1, 2}, 3: {1, 3}, 4: {1, 2, 4}}
{1: {1}, 2: {1, 2}, 3: {1, 3}, 4: {1, 2, 4}}\n
Passed after ignoring Presentation Error



2. Programming Assignment | Week 11

Due on 2024-04-11, 23:59 IST

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.

Your last recorded submission was on 2024-03-31, 13:12 IST
Select the Language for this assignment. 
1
l = input().split(',')
2
k = int(input())
3
for i in range(1,k+1):
4
  last = l.pop()
5
  l.insert(0, last)
6
print(",".join([str(i) for i in l]), end = '')
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1,2,3,4,5
3
3,4,5,1,2
3,4,5,1,2
Passed
Test Case 2
1,2,3,4,5,6
10
3,4,5,6,1,2
3,4,5,6,1,2
Passed



3. Programming Assignment | Week 11

Due on 2024-04-11, 23:59 IST

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 the x-coordinate of Pn​, the point at which the nth arm of the spiral ends.


You do not have to accept input from the user or print the output to the console. You just have to write the function definition.

Your last recorded submission was on 2024-03-31, 13:07 IST
Select the Language for this assignment. 
1
~~~THERE IS SOME INVISIBLE CODE HERE~~~
2
def spiral_iterative(left, right, n):
3
    """
4
    An iterative function to compute the x-coordinate of the nth arm of the spiral
5
6
    Arguments:
7
        left: integer
8
        right: integer
9
        n: integer
10
    Return:
11
        result: float
12
    """
13
    for i in range(1,n):
14
      center = (left + right)/2
15
      if i % 2 :
16
        left = center
17
      else:
18
        right = center
19
    return center
20
    
21
    
22
def spiral_recursive(left, right, n):
23
    """
24
    An recursive function to compute the x-coordinate of the nth arm of the spiral
25
26
    Arguments:
27
        left: integer
28
        right: integer
29
        n: integer
30
    Return:
31
        result: float
32
    """
33
    if n == 2:
34
      return ( left + right )/2
35
    elif n % 2 == 0:
36
      return (spiral_recursive(( left + right )/2, right, n-1))
37
    elif n % 2 == 1:
38
      return (spiral_recursive(left, ( left + right )/2, n-1))
39
    
40
0
~~~THERE IS SOME INVISIBLE CODE HERE~~~
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
spiral_recursive
0,1,4
0.625
0.625\n
Passed after ignoring Presentation Error
Test Case 2
spiral_iterative
0,1,100
0.667
0.667\n
Passed after ignoring Presentation Error





























No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.