Programming, Data Structures And Algorithms Using Python Week 3 Programming Assignment | NPTEL | Answer with Explanation
"Discover the Excitement of Computing with Python | Week 3 Programming Assignment - Get ready to enhance your programming skills and deepen your understanding of the Python language with this week 3 Assignment on 'Programming, Data Structures And Algorithms Using Python
'. Test your knowledge and boost your confidence as a Python programmer today!"
Week 3: Basic algorithmic analysis: input size, asymptotic complexity, O() notation,
Arrays vs lists,
Merge sort,
Quicksort,
Stable sorting
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]
Answer
def remdup(l): seen = set() result = [] for x in reversed(l): if x not in seen: result.append(x) seen.add(x) result.reverse() return result
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]
Answer
def splitsum(l): pos = 0 neg = 0 for i in l: if i > 0: pos += i**2 else: neg += i**3 return [pos, neg]
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]]
Answer
def matrixflip(m, d): if d == 'h': return [row[::-1] for row in m] elif d == 'v': return m[::-1] else: return m
Disclaimer:
"This page contains Porgramming Assignment answers related to The Joy of Computing using Python . The answers to these questions are provided for educational and informational purposes only.These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself."
0 Comments