Nptel Programming Data Structures And Algorithms Using Python Week 3 Assignment Answer
Course layout
Informal introduction to programmin, algorithms and data structures viagcd
Downloading and installing Python
gcd in Python: variables, operations, control flow – assignments, condition-als, loops, functions
Week 2:
Python: types, expressions, strings, lists, tuples
Python memory model: names, mutable and immutable values
List operations: slices etc
Binary search
Inductive function denitions: numerical and structural induction
Elementary inductive sorting: selection and insertion sort
In-place sorting
Week 3:
Basic algorithmic analysis: input size, asymptotic complexity, O() notation
Arrays vs lists
Merge sort
Quicksort
Stable sorting
Week 4:
Dictionaries
More on Python functions: optional arguments, default values
Passing functions as arguments
Higher order functions on lists: map, lter, list comprehension
Week 5:
Exception handling
Basic input/output
Handling files
String processing
Week 6:
Backtracking: N Queens, recording all solutions
Scope in Python: local, global, nonlocal names
Nested functions
Data structures: stack, queue
Heaps
Week 7:
Abstract datatypes
Classes and objects in Python
“Linked” lists: find, insert, delete
Binary search trees: find, insert, delete
Height-balanced binary search trees
Week 8:
Effcient evaluation of recursive definitions: memoization
Dynamic programming: examples
Other programming languages: C and manual memory management
Other programming paradigms: functional programming
Nptel Programming Data Structures And Algorithms Using Python Week 3 Assignment Answer
Week 3 Programming Assignment
- You may define additional auxiliary functions as needed.
- In all cases you may assume that the value passed to the function is of the expected type, so your function does not have to check for malformed inputs.
- For each function, there are normally some public test cases and some (hidden) private test cases.
- “Compile and run” will evaluate your submission against the public test cases.
- “Submit” will evaluate your submission against the hidden private test cases. There are 12 private test cases, with equal weightage. You will get feedback about which private test cases pass or fail, though you cannot see the actual test cases.
- Ignore warnings about “Presentation errors”.
-
- Define a Python function remdup(l) that takes a nonempty list of integers l and removes all duplicates in l, keeping only the last occurrence of each number. For instance:
>>> remdup([3,1,3,5]) [1, 3, 5] >>> remdup([7,3,-1,-5]) [7, 3, -1, -5] >>> remdup([3,5,7,5,3,7,10]) [5, 3, 7, 10]
- Write a Python function splitsum(l) that takes a nonempty list of integers and returns a list [pos,neg], where pos is the sum of squares all the positive numbers in l and neg is the sum of cubes of all the negative numbers in l.
Here are some examples to show how your function should work.
>>> splitsum([1,3,-5]) [10, -125] >>> splitsum([2,4,6]) [56, 0] >>> splitsum([-19,-7,-6,0]) [0, -7418] >>> splitsum([-1,2,3,-7]) [13, -344]
- A two dimensional matrix can be represented in Python row-wise, as a list of lists: each inner list represents one row of the matrix. For instance, the matrix
1 2 3 4 5 6 7 8 9
would be represented as [[1, 2, 3], [4, 5, 6], [7, 8, 9]].
A horizonatal flip reflects each row. For instance, if we flip the previous matrix horizontally, we get
3 2 1 6 5 4 9 8 7
which would be represented as [[3, 2, 1], [6, 5, 4], [9, 8, 7]].
A vertical flip reflects each column. For instance, if we flip the previous matrix that has already been flipped horizontally, we get
9 8 7 6 5 4 3 2 1
which would be represented as [[9, 8, 7], [6, 5, 4], [3, 2, 1]].
Write a Python function matrixflip(m,d) that takes as input a two dimensional matrix m and a direction d, where d is either 'h' or 'v'. If d == 'h', the function should return the matrix flipped horizontally. If d == 'v', the function should retun the matrix flipped vertically. For any other value of d, the function should return m unchanged. In all cases, the argument m should remain undisturbed by the function.
Here are some examples to show how your function should work. You may assume that the input to the function is always a non-empty matrix.
>>> myl = [[1,2],[3,4]] >>> myl [[1, 2], [3, 4]] >>> matrixflip(myl,'h') [[2, 1], [4, 3]] >>> myl [[1, 2], [3, 4]] >>> matrixflip(myl,'v') [[3, 4], [1, 2]] >>> myl [[1, 2], [3, 4]] >>> matrixflip(matrixflip(myl,'h'),'v') [[4, 3], [2, 1]] >>> myl [[1, 2], [3, 4]] >>> matrixflip(matrixflip(myl,'h'),'v') [[4, 3], [2, 1]] >>> myl [[1, 2], [3, 4]]
- Define a Python function remdup(l) that takes a nonempty list of integers l and removes all duplicates in l, keeping only the last occurrence of each number. For instance:
Input | Output | |
Test Case 1 |
remdup([3,1,3,5]) |
[1, 3, 5] |
Test Case 2 |
remdup([7,3,-1,-5]) |
[7, 3, -1, -5] |
Test Case 3 |
remdup([3,5,7,5,3,7,10]) |
[5, 3, 7, 10] |
Test Case 4 |
splitsum([1,3,-5]) |
[10, -125] |
Test Case 5 |
splitsum([2,4,6]) |
[56, 0] |
Test Case 6 |
splitsum([-19,-7,-6,0]) |
[0, -7418] |
Test Case 7 |
splitsum([-1,2,3,-7]) |
[13, -344] |
Test Case 8 |
matrixflip([[1, 2], [3, 4]],'h') |
[[2, 1], [4, 3]] |
Test Case 9 |
matrixflip([[1, 2], [3, 4]],'v') |
[[3, 4], [1, 2]] |