|
1 | 1 | # NPTEL-Programming-Data-Structures-and-Algorithms-using-Python-Week-4-Practice-Programming-Assignment
|
2 | 2 | NPTEL Programming, Data Structures and Algorithms using Python Week 4 Practice Programming Assignment
|
| 3 | + |
| 4 | +# Week 4 Practice Programming Assignment |
| 5 | +1. We represent scores of batsmen across a sequence of matches in a two level dictionary as follows: |
| 6 | +```python |
| 7 | +{'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91} |
| 8 | +``` |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +The input will be such that there are never any ties for highest total score. |
| 15 | + |
| 16 | +For instance: |
| 17 | +```python |
| 18 | +>>> orangecap({'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91}}) |
| 19 | +('player3', 100) |
| 20 | + |
| 21 | +>>> orangecap({'test1':{'Ashwin':84, 'Kohli':120}, 'test2':{'ashwin':59, 'Pujara':42}}) |
| 22 | +('Kohli', 120) |
| 23 | +``` |
| 24 | + |
| 25 | +2. Let us consider polynomials in a single variable x with integer coefficients: for instance, 3x<sup>4</sup> - 17x<sup>2</sup> - 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. |
| 26 | + |
| 27 | +We have the following constraints to guarantee that each polynomial has a unique representation: |
| 28 | +* Terms are sorted in descending order of exponent |
| 29 | +* No term has a zero cofficient |
| 30 | +* No two terms have the same exponent |
| 31 | +* Exponents are always nonnegative |
| 32 | + |
| 33 | +For example, the polynomial introduced earlier is represented as |
| 34 | +```python |
| 35 | +[(3,4),(-17,2),(-3,1),(5,0)] |
| 36 | +``` |
| 37 | +The zero polynomial, 0, is represented as the empty list [], since it has no terms with nonzero coefficients. |
| 38 | + |
| 39 | +Write Python functions for the following operations: |
| 40 | +```python |
| 41 | +addpoly(p1,p2) |
| 42 | +multpoly(p1,p2) |
| 43 | +``` |
| 44 | +that add and multiply two polynomials, respectively. |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +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. |
| 49 | + |
| 50 | +You may also want to convert the list representation to a dictionary representation and manipulate the dictionary representation, and then convert back. |
| 51 | + |
| 52 | +Some examples: |
| 53 | +```python |
| 54 | +>>> addpoly([(4,3),(3,0)],[(-4,3),(2,1)]) |
| 55 | +[(2, 1),(3, 0)] |
| 56 | +``` |
| 57 | +Explanation: (4x<sup>3</sup> + 3) + (-4x<sup>3</sup> + 2x) = 2x + 3 |
| 58 | +```python |
| 59 | +>>> addpoly([(2,1)],[(-2,1)]) |
| 60 | +[] |
| 61 | +``` |
| 62 | +Explanation: 2x + (-2x) = 0 |
| 63 | +```python |
| 64 | +>>> multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)]) |
| 65 | +[(1, 3),(-1, 0)] |
| 66 | +``` |
| 67 | +Explanation: (x - 1) * (x<sup>2</sup> + x + 1) = x<sup>3</sup> - 1 |
| 68 | + |
| 69 | +# Sample Test Cases |
| 70 | +| | Input | Output | |
| 71 | +|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------| |
| 72 | +| Test Case 1 | orangecap({'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91}}) | ('player3', 100) | |
| 73 | +| Test Case 2 | orangecap({'test1':{'Ashwin':84, 'Kohli':120}, 'test2':{'ashwin':59, 'Pujara':42}}) | ('Kohli', 120) | |
| 74 | +| Test Case 3 | 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) | |
| 75 | +| Test Case 4 | orangecap({'match1':{'player1':57, 'player2':38}}) | ('player1', 57) | |
| 76 | +| Test Case 5 | multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)]) | [(1, 3), (-1, 0)] | |
| 77 | +| Test Case 6 | multpoly([(2,1)],[(-2,1)]) | [(-4, 2)] | |
| 78 | +| Test Case 7 | multpoly([(4,3),(3,0)],[(-4,3),(2,1)]) | [(-16, 6), (8, 4), (-12, 3), (6, 1)] | |
| 79 | +| Test Case 8 | addpoly([(4,3),(3,0)],[(-4,3),(2,1)]) | [(2, 1), (3, 0)] | |
| 80 | +| Test Case 9 | addpoly([(2,1)],[(-2,1)]) | [] | |
| 81 | +| Test Case 10 | addpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)]) | [(1, 2), (2, 1)] | |
| 82 | +| Test Case 11 | orangecap({'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91}}) | ('player3', 100) | |
| 83 | +| Test Case 12 | orangecap({'test1':{'Ashwin':84, 'Kohli':120}, 'test2':{'ashwin':59, 'Pujara':42}}) | ('Kohli', 120) | |
| 84 | +| Test Case 13 | 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) | |
| 85 | +| Test Case 14 | orangecap({'match1':{'player1':57, 'player2':38}}) | ('player1', 57) | |
| 86 | +| Test Case 15 | multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)]) | [(1, 3), (-1, 0)] | |
| 87 | +| Test Case 16 | multpoly([(2,1)],[(-2,1)]) | [(-4, 2)] | |
| 88 | +| Test Case 17 | multpoly([(4,3),(3,0)],[(-4,3),(2,1)]) | [(-16, 6), (8, 4), (-12, 3), (6, 1)] | |
| 89 | +| Test Case 18 | addpoly([(4,3),(3,0)],[(-4,3),(2,1)]) | [(2, 1), (3, 0)] | |
| 90 | +| Test Case 19 | addpoly([(2,1)],[(-2,1)]) | [] | |
| 91 | +| Test Case 20 | addpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)]) | [(1, 2), (2, 1)] | |
0 commit comments