# Programming Data Structure And Algorithms Using Python | Week 3

**Session: JULY-DEC 2023**

**Course Name: Programming Data Structure And Algorithms Using Python**

**Course Link: Click Here**

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

**Programming Data Structure And Algorithms Using Python Programming Assignment Question**

**Question 1Write a function expanding(l) that takes as input a list of integer l and returns True if the absolute difference between each adjacent pair of elements strictly increases.Here are some examples of how your function should work.**

Answer:

```
def expanding(l):
if len(l) <= 2:
return(True)
diff = abs(l[1]-l[0])
return(diff < abs(l[2]-l[1]) and expanding(l[1:]))
def expanding_loop(l):
if len(l) <= 2:
return(True)
olddiff = abs(l[1]-l[0])
for i3 in range(2,len(l)):
newdiff = abs(l[i3]-l[i3-1])
if newdiff <= olddiff:
return(False)
olddiff = newdiff
return(True)
def sumsquare(l):
(odd_sum,even_sum)=(0,0)
for aa in l:
if aa%2!=0:
odd_sum+=aa*aa
else:
even_sum+=aa*aa
return([odd_sum,even_sum])
def transpose(m):
ans1=list()
for ip in range(len(m[0])):
a=list()
for j in range(len(m)):
a.append(m[j][ip])
ans1.append(a)
return(ans1)
```

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

**These are Programming Data Structure And Algorithms Assignment 3 Answers**More Weeks of Programming Data Structure And Algorithms Using Python: Click here

More Nptel Courses: Click here

**Session: JAN-APR 2023**

**Course Name: Programming Data Structure And Algorithms Using Python**

**Course Link: Click Here**

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

**These are Programming Data Structure And Algorithms Assignment 3 Answers****Question 1**

Write three Python functions as specified below. Paste the text for all three functions together into the submission window. Your function will be called automatically with various inputs and should return values as specified. Do not write commands to read any input or print any output.

You may define additional auxiliary functions as needed.

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

**These are Programming Data Structure And Algorithms Assignment 3 Answers**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”.

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

**These are Programming Data Structure And Algorithms Assignment 3 Answers**1. 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:

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

3. 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 would be represented as [[1, 2, 3], [4, 5, 6], [7, 8, 9]].

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

A horizonatal flip reflects each row. For instance, if we flip the previous matrix horizontally, we get 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 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.

**Solution:**

```
####
def remdup(l):
if len(l) <= 1:
return(l)
if l[0] in l[1:]:
return(remdup(l[1:]))
else:
return([l[0]] + remdup(l[1:]))
####
def splitsum(l):
pos = 0
neg = 0
for x in l:
if x > 0:
pos = pos + x**2
if x < 0:
neg = neg + x**3
return([pos,neg])
####
def matrixflip(l,d):
outl = []
for row in l:
outl.append(row[:])
if d == 'h':
for row in outl:
row.reverse()
elif d == 'v':
outl.reverse()
return(outl)
####
import ast
def tolist(inp):
inp = "["+inp+"]"
inp = ast.literal_eval(inp)
return (inp[0],inp[1])
def parse(inp):
inp = ast.literal_eval(inp)
return (inp)
fncall = input()
lparen = fncall.find("(")
rparen = fncall.rfind(")")
fname = fncall[:lparen]
farg = fncall[lparen+1:rparen]
if fname == "remdup":
arg = parse(farg)
print(remdup(arg))
elif fname == "splitsum":
arg = parse(farg)
print(splitsum(arg))
elif fname == "matrixflip":
(arg1,arg2) = parse(farg)
savearg1 = []
for row in arg1:
savearg1.append(row[:])
myans = matrixflip(arg1,arg2)
if savearg1 == arg1:
print(myans)
else:
print("Illegal side effect")
else:
print("Function", fname, "unknown")
```

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

More Weeks of Programming Data Structure And Algorithms: Click Here

More Nptel courses: https://progiez.com/nptel

**Session: JULY-DEC 2022**

Course Name: Programming Data Structure And Algorithms Using Python

Link of Course: Click Here

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

**These are Programming Data Structure And Algorithms Assignment 3 Answers**Write three Python functions as specified below. Paste the text for all three functions together into the submission window. Your function will be called automatically with various inputs and should return values as specified. Do not write commands to read any input or print any output.

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

- 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 15 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”.

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

Question 1

Write a function contracting(l) that takes as input a list of integer l and returns True if the absolute difference between each adjacent pair of elements strictly decreases.Here are some examples of how your function should work.

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

Question 2

In a list of integers l, the neighbours of l[i] are l[i-1] and l[i+1]. l[i] is a *hill* if it is strictly greater than its neighbours and a *valley* if it is strictly less than its neighbours. Write a function counthv(l) that takes as input a list of integers l and returns a list [hc,vc] where hc is the number of hills in l and vc is the number of valleys in l. Here are some examples to show how your function should work.

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

Question 3

A square n×n matrix of integers can be written in Python as a list with n elements, where each element is in turn a list of n integers, representing a row of the matrix. For instance, the matrix would be represented as [[1,2,3], [4,5,6], [7,8,9]].

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

Write a function leftrotate(m) that takes a list representation m of a square matrix as input, and returns the matrix obtained by rotating the original matrix counterclockwize by 90 degrees. For instance, if we rotate the matrix above, we get

Your function should *not* modify the argument m provided to the function rotate().

Here are some examples of how your function should work.

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

# Solution:

```
####
def remdup(l):
if len(l) <= 1:
return(l)
if l[0] in l[1:]:
return(remdup(l[1:]))
else:
return([l[0]] + remdup(l[1:]))
####
def splitsum(l):
pos = 0
neg = 0
for x in l:
if x > 0:
pos = pos + x**2
if x < 0:
neg = neg + x**3
return([pos,neg])
####
def matrixflip(l,d):
outl = []
for row in l:
outl.append(row[:])
if d == 'h':
for row in outl:
row.reverse()
elif d == 'v':
outl.reverse()
return(outl)
####
import ast
def tolist(inp):
inp = "["+inp+"]"
inp = ast.literal_eval(inp)
return (inp[0],inp[1])
def parse(inp):
inp = ast.literal_eval(inp)
return (inp)
fncall = input()
lparen = fncall.find("(")
rparen = fncall.rfind(")")
fname = fncall[:lparen]
farg = fncall[lparen+1:rparen]
if fname == "remdup":
arg = parse(farg)
print(remdup(arg))
elif fname == "splitsum":
arg = parse(farg)
print(splitsum(arg))
elif fname == "matrixflip":
(arg1,arg2) = parse(farg)
savearg1 = []
for row in arg1:
savearg1.append(row[:])
myans = matrixflip(arg1,arg2)
if savearg1 == arg1:
print(myans)
else:
print("Illegal side effect")
else:
print("Function", fname, "unknown")
```

**These are Programming Data Structure And Algorithms Assignment 3 Answers**

More NPTEL Solutions: https://progiez.com/nptel

The content uploaded on this website is for reference purposes only. Please do it yourself first. |