100% Guaranteed Results


Exercises – Write Python functions as specified below. Paste the text for all functions together into the submission window. Solved
$ 24.99
Category:

Description

5/5 – (1 vote)

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.
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’:{‘Ashwin’:84, ‘Kohli’:120}, ‘test2’:{‘ashwin’:59, ‘Pujara’:42}}) (‘Kohli’, 120)

2). Let us consider polynomials in a single variable x with integer coefficients: for instance, 3×4 – 17×2 – 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.

Correspondingly, the outputs from these functions should also obey the same constraints.

Hint: You are not restricted to writing just the two functions asked for. 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.

Some examples:

>>> addpoly([(4,3),(3,0)],[(-4,3),(2,1)])
[(2, 1),(3, 0)]
Explanation: (4×3 + 3) + (-4×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) * (x2 + x + 1) = x3 – 1

Sample Test Cases
Input
Output

Test Case 1
orangecap({‘match1’:{‘player1’:57, ‘player2’:38}, ‘match2’:{‘player3’:9, ‘player1’:42}, ‘match3’:{‘player2’:41, ‘player4’:63, ‘player3’:91}})
(‘player3’, 100)

Test Case 2
orangecap({‘test1’:{‘Ashwin’:84, ‘Kohli’:120}, ‘test2’:{‘ashwin’:59, ‘Pujara’:42}}) (‘Kohli’, 120)

Test Case 3
addpoly([(4,3),(3,0)],[(-4,3),(2,1)])
[(2, 1), (3, 0)]

Test Case 4
addpoly([(2,1)],[(-2,1)])
[]

Test Case 5
multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)])
[(1, 3), (-1, 0)]

Test Case 6
multpoly([(2,1)],[(-2,1)])
[(-4, 2)]

Test Case 7
orangecap({‘match1’:{‘player1’:57, ‘player2’:38}, ‘match2’:{‘player3’:9, ‘player1’:42}, ‘match3’:{‘player2’:41, ‘player4’:63, ‘player3’:91}})
(‘player3’, 100)

Test Case 8
orangecap({‘test1’:{‘Ashwin’:84, ‘Kohli’:120}, ‘test2’:{‘ashwin’:59, ‘Pujara’:42}}) (‘Kohli’, 120)

Test Case 9
orangecap({‘match1’:{‘player1’:57, ‘player2’:38}, ‘match2’:{‘player3’:9, ‘player1’:42},
‘match3’:{‘player2’:41, ‘player4’:63, ‘player3′:91},’test1’:{‘Ashwin’:84, ‘Kohli’:120}, ‘test2’:{‘ashwin’:59, ‘Pujara’:42}})
(‘Kohli’, 120)

Test Case 10
orangecap({‘match1’:{‘player1’:57, ‘player2’:38}})
(‘player1’, 57)

Test Case 11
addpoly([(4,3),(3,0)],[(-4,3),(2,1)])
[(2, 1), (3, 0)]

Test Case 12
addpoly([(2,1)],[(-2,1)])
[]

Test Case 13
addpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)])
[(1, 2), (2, 1)]

Test Case 14
multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)])
[(1, 3), (-1, 0)]

Test Case 15
multpoly([(2,1)],[(-2,1)])
[(-4, 2)]

Test Case 16
multpoly([(4,3),(3,0)],[(-4,3),(2,1)]) [(-16, 6), (8, 4), (-12, 3), (6, 1)]

Reviews

There are no reviews yet.

Be the first to review “Exercises – Write Python functions as specified below. Paste the text for all functions together into the submission window. Solved”

Your email address will not be published. Required fields are marked *

Related products