Home

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

Programming, Data Structures And Algorithms Using Python | Week 4 Programming Assignment | Jan-2022 | NPTEL

Programming, Data Structures And Algorithms Using Python

Week 4 Programming Assignment

Due on 2022-02-24, 23:59 IST


Write Python functions as specified below. Paste the text for all functions together into the submission window.

  • 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 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 and report a score on 100. There are 10 private testcases in all, each 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".

  1. We represent scores of batsmen across a sequence of matches in a two level dictionary as follows:

    {'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91}
    

    Each match is identified by a string, as is each player. The scores are all integers. The names associated with the matches are not fixed (here they are 'match1''match2''match3'), nor are the names of the players. A player need not have a score recorded in all matches.

    Define a Python function orangecap(d) that reads a dictionary d of this form and identifies the player with the highest total score. Your function should return a pair (playername,topscore) where playername is a string, the name of the player with the highest score, and topscore is an integer, the total score of playername.

    The input will be such that there are never any ties for highest total score.

    For instance:

    >>> orangecap({'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91}})
    ('player3', 100)
    
    >>> orangecap({'test1':{'Pant':84, 'Kohli':120}, 'test2':{'Pant':59, 'Gill':42}})
    ('Pant', 143)
    
  2. Let us consider polynomials in a single variable x with integer coefficients. For instance:

    3x4 - 17x2 - 3x + 5
    

    Each term of the polynomial can be represented as a pair of integers (coefficient,exponent). The polynomial itself is then a list of such pairs.

    We have the following constraints to guarantee that each polynomial has a unique representation:

    • Terms are sorted in descending order of exponent
    • No term has a zero cofficient
    • No two terms have the same exponent
    • Exponents are always nonnegative

    For example, the polynomial introduced earlier is represented as:

    [(3,4),(-17,2),(-3,1),(5,0)]
    

    The zero polynomial, 0, is represented as the empty list [], since it has no terms with nonzero coefficients.

    Write Python functions for the following operations:

    addpoly(p1,p2)
    multpoly(p1,p2)
    

    that add and multiply two polynomials, respectively.

    You may assume that the inputs to these functions follow the representation given above. Correspondingly, the outputs from these functions should also obey the same constraints.

    You can write auxiliary functions to "clean up" polynomials – e.g., remove zero coefficient terms, combine like terms, sort by exponent etc. Build a library of functions that can be combined to achieve the desired format.

    You may also want to convert the list representation to a dictionary representation and manipulate the dictionary representation, and then convert back.

    Some examples:

      
       >>> addpoly([(4,3),(3,0)],[(-4,3),(2,1)])
       [(2, 1),(3, 0)]
    
       Explanation: (4x^3 + 3) + (-4x^3 + 2x) = 2x + 3
    
       >>> addpoly([(2,1)],[(-2,1)])
       []
    
       Explanation: 2x + (-2x) = 0
    
       >>> multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)])
       [(1, 3),(-1, 0)]
    
       Explanation: (x - 1) * (x^2 + x + 1) = x^3 - 1

Select the Language for this assignment. 

Just scroll your mouse wheel over this code or swipe on this code snippet to scroll down to see the remaining code.

1
def orangecap(d):
	total={}
	l=[]
	for p in d.values():
		for ply in p.keys():
			if ply not in l:
				l=l+[ply]
	for n in l:
		total[n]=0
		for match in d.keys():
			if n in d[match].keys():
				total[n]=total[n]+d[match][n]
	topscore=0
	for player in total.keys():
			if total[player] > topscore:
				playername=player
				topscore=total[player]
	return (playername,topscore)
  

def csort(l):
  for start in range(len(l)): 
    minpos = start
    for i in range(start,len(l)):
      if l[i][1] > l[minpos][1]:
        minpos = i
    (l[start],l[minpos]) = (l[minpos],l[start])
  
  
def remove(l):
  for j in (l):
    if j[0] == 0 :
      l.remove(j)
  return (l)

  
def addpoly(p1,p2):
	l=[]
	for i in range(len(p1)):
		for j in range(len(p2)):
			if p1[i][1] == p2[j][1]:
				l=l+[(p1[i][0]+p2[j][0], p1[i][1])]
				break
		else:
			l=l+[(p1[i][0], p1[i][1])]
	for i in range(len(p2)):
		flag=1
		for j in range(len(l)):
			if p2[i][1] == l[j][1]:
				flag=0
				break
		if flag:
			l=l+[(p2[i][0], p2[i][1])]
	remove(l)
	csort(l)
	return (l)
  

def remdup(l):
  p=[]
  for x in l:
    if(x not in p):
      p=p+[x]
  return(p)


def multpoly(p1,p2):
	l=[]
	for i in range(len(p1)):
		for j in range(len(p2)):
				l=l+[(p1[i][0]*p2[j][0], p1[i][1]+p2[j][1])]
	csort(l)
	list2=[]
	for j in l:
		sum=0
		for k in l:
			if j[1] == k[1]:
				sum+=k[0]
		list2+=[(sum,j[1])]
	for j in list2:
		if j[0] == 0 :
			while j in list2:
				list2.remove(j)
	l=remdup(list2)
	return(l)
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
orangecap({'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91}})
('player3', 100)\n
('player3', 100)\n
Passed
Test Case 2
orangecap({'test1':{'Pant':84, 'Kohli':120}, 'test2':{'Pant':59, 'Gill':42}})
('Pant', 143)\n
('Pant', 143)\n
Passed
Test Case 3
addpoly([(4,3),(3,0)],[(-4,3),(2,1)])
[(2, 1), (3, 0)]\n
[(2, 1), (3, 0)]\n
Passed
Test Case 4
addpoly([(2,1)],[(-2,1)])
[]\n
[]\n
Passed
Test Case 5
multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)])
[(1, 3), (-1, 0)]\n
[(1, 3), (-1, 0)]\n
Passed


Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed
Test Case 6
Passed
Test Case 7
Passed
Test Case 8
Passed
Test Case 9
Passed
Test Case 10
Passed


Please subscribe to our YouTube Channel :  Swayam Solver

This will help the creator to continue making quality content...

Have a great day !

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.