Please scroll down for latest Programs 👇
1. Programming Assignment | Week 11
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.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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
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.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
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.