Monday, February 24, 2020

Week-5 Assignment Solution Jan-March 2020 NPTEL


Week-5 Assignment Solution (Last Date : 05/03/2020)

The library at the Hogwarts School of Witchcraft and Wizardry has computerized its book issuing process. The relevant information is provided as text from standard input in three parts: information about books, information about borrowers and information about checkouts. Each part has a specific line format, described below.
  1. Information about books
    Line format: Accession Number~Title
  2. Information about borrowers
    Line format: Username~Full Name
  3. Information about checkouts
    Line format: Username~Accession Number~Due Date
    Note: Due Date is in YYYY-MM-DD format.
You may assume that the data is internally consistent. For every checkout, there is a corresponding username and accession number in the input data, and no book is simultaneously checked out by two people.
Each section of the input starts with a line containing a single keyword. The first section begins with a line containing Books. The second section begins with a line containing Borrowers. The third section begins with a line containing Checkouts. The end of the input is marked by a line containing EndOfInput.
Write a Python program to read the data as described above and print out details about books that have been checked out. Each line should describe to one currently issued book in the following format:
Due Date~Full Name~Accession Number~Title
Your output should be sorted in increasing order of due date. For books due on the same date, sort in increasing order of full name. If the due date and full name are both the same, sort based on the accession number, and, finally, the title of the book.
Here is a sample input and its corresponding output.
Sample Input
Books
APM-001~Advanced Potion-Making
GWG-001~Gadding With Ghouls
APM-002~Advanced Potion-Making
DMT-001~Defensive Magical Theory
DMT-003~Defensive Magical Theory
GWG-002~Gadding With Ghouls
DMT-002~Defensive Magical Theory
Borrowers
SLY2301~Hannah Abbott
SLY2302~Euan Abercrombie
SLY2303~Stewart Ackerley
SLY2304~Bertram Aubrey
SLY2305~Avery
SLY2306~Malcolm Baddock
SLY2307~Marcus Belby
SLY2308~Katie Bell
SLY2309~Sirius Orion Black
Checkouts
SLY2304~DMT-002~2019-03-27
SLY2301~GWG-001~2019-03-27
SLY2308~APM-002~2019-03-14
SLY2303~DMT-001~2019-04-03
SLY2301~GWG-002~2019-04-03
EndOfInput
Sample Output
2019-03-14~Katie Bell~APM-002~Advanced Potion-Making
2019-03-27~Bertram Aubrey~DMT-002~Defensive Magical Theory
2019-03-27~Hannah Abbott~GWG-001~Gadding With Ghouls
2019-04-03~Hannah Abbott~GWG-002~Gadding With Ghouls
2019-04-03~Stewart Ackerley~DMT-001~Defensive Magical Theory 

Solution:


# Dictionary to map accession number to title
books = {}
# Dictionary to map username to fullname
borrowers = {}
# List to store checkout data: accumulate, sort and print
checkouts = [] 

# Find the start of Books data
nextline = input().strip()
while nextline.find("Books") < 0:
    nextline = input().strip()

# Read upto start of Borrowers data
# Skip the line with "Books"
nextline = input().strip()
while nextline.find("Borrowers") < 0:
    (accession_number,title) = nextline.split('~')
    books[accession_number] = title
    nextline = input().strip()

# Read upto Checkout data
# Skip the line with "Borrowers"
nextline = input().strip()
while nextline.find("Checkouts") < 0:
    (username,fullname) = nextline.split('~')
    borrowers[username] = fullname
    nextline = input().strip()

# Process Checkouts
# Skip the line with "Checkouts"
nextline = input().strip()
while nextline.find("EndOfInput") < 0:
    (username,accession_number,due_date) = nextline.split('~')
    checkoutline = due_date+"~"+borrowers[username]+"~"+accession_number+"~"+books[accession_number]
    checkouts.append(checkoutline)
    nextline = input().strip()

# Print the output from checkouts
for checkoutline in sorted(checkouts):
    print(checkoutline)










Monday, February 17, 2020

Week-4 Assignment Solution Jan-March 2020 NPTEL

Week-4 Assignment Solution (Last Date : 27/02/2020)


  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}})
    ('Ashwin', 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                    
Solution:

def orangecap(d):
  total = {}
  for k in d.keys():
    for n in d[k].keys():
      if n in total.keys():
        total[n] = total[n] + d[k][n]
      else:
        total[n] = d[k][n]

  maxtotal = -1
  for n in total.keys():
    if total[n] > maxtotal:
      maxname = n
      maxtotal = total[n]

  return(maxname,maxtotal)

def listtodict(poly):
  dpoly = {}
  for term in poly:
    coeff = term[0]
    exp = term[1]
    dpoly[exp] = coeff
  return(dpoly)

def dicttolist(dpoly):
  lpoly = []
  for exp in sorted(dpoly.keys()):
    lpoly.append((dpoly[exp],exp))
  lpoly.reverse()
  return(lpoly)

def dpolyadd (dpoly1,dpoly2):
  sumpoly = {}
  for exp in dpoly1.keys():
    sumpoly[exp] = dpoly1[exp]

  for exp in dpoly2.keys():
    if exp in sumpoly.keys():
      sumpoly[exp] = sumpoly[exp] + dpoly2[exp]
    else:
      sumpoly[exp] = dpoly2[exp]

  return(sumpoly)

def dpolymult (dpoly1,dpoly2):
  multpoly = {}
  for exp1 in dpoly1.keys():
    for exp2 in dpoly2.keys():
      newexp = exp1 + exp2
      newcoeff = dpoly1[exp1] * dpoly2[exp2]
      if newexp in multpoly.keys():
        multpoly[newexp] = multpoly[newexp] + newcoeff
      else:
        multpoly[newexp] = newcoeff
  return(multpoly)


def addpoly(p1,p2):
  d1 = listtodict(p1)
  d2 = listtodict(p2)
  res = dpolyadd(d1,d2)
  return(dicttolist(cleanup(res)))

def multpoly(p1,p2):
  d1 = listtodict(p1)
  d2 = listtodict(p2)
  res = dpolymult(d1,d2)
  return(dicttolist(cleanup(res)))

def cleanup(dpoly):
  dpolyclean = {}
  for exp in dpoly.keys():
    if dpoly[exp] != 0:
      dpolyclean[exp] = dpoly[exp]

  return(dpolyclean)

#Copy the Below Code as it is and Paste it in the terminal

Week-4 Quiz Solution Jan-March 2020 NPTEL

Week-4 Quiz Solution (Last Date : 27/02/2020)



Solution : 258


Solution: [(2, 4, 5), (3, 3, 5), (3, 4, 5), (3, 4, 6)]


Solution : D


Solution : C

Tuesday, February 11, 2020

Week-3 Assignment Solution Jan-March 2020

Week-3 Assignment Solution (Last Date : 20/02/2020)


def remdup(l):
  final = []
  for i in l:
    if i not in final:
      final.append(i)
  return final


def sumsquare(l):
  submit = [0,0]
  for i in l:
    if i%2 == 0:
      submit[1] += i**2
    else:
      submit[0] += i**2
  return submit



def transpose(M):
  s = []
  for i in range(len(M[0])):
    s.append([j[i] for j in M])
  return s

Note: Copy the Code and Paste as it is without making any changes into it. If you have any queries Please do comment Below and Share among your friends.

Week-2 Assignment Solution Jan-March 2020

Week-2 Assignment Solution (Last Date : 15/02/2020)


def threesquares(N) :
    i = 0
    while (i * i<= N) :
        j= i
        while (j * j <= N) :
            k = j 
            while (k * k <= N) :
                if (i * i + j * j + k * k  == N) :
                    return True
                k = k + 1
            j = j + 1
        i = i + 1
    return False


def repfree(s):
    a = set(s)
    return len(a) == len(s)


def hillvalley(l):
    if l == []:
        return False
    stop = -1
    i = 1
    flag = True
    if l[0] > l[1]:
        i = 1
        for i in range(i, len(l)):
            if l[i-1] < l[i]:
                stop = i
                break
        if stop == -1:
            return False
        i = stop
        for i in range(i, len(l)):
            if l[i-1] > l[i]:
                flag = False
                break
        return flag
    elif l[0] < l[1]:
        for i in range(1, len(l)):
            if l[i-1] > l[i]:
                stop = i
                break
        if stop == -1:
            return False
        for i in range(i, len(l)):
            if l[i-1] < l[i]:
                flag = False
                break
        return flag
    else:
        return False

Note: Copy the Code and Paste as it is without making any changes into it. If you have any queries Please do comment Below and Share among your friends.


Week-2 Quiz Solution Jan-March 2020

Week-2 Quiz Solution (Last Date : 12/02/2020)

  1. 7
  2. a
  3. "tarantula"
  4. [44,71,12,8,23,17,16]

Note: Make Sure you Enter the Answers in Similar format without any changes. 

Week-1 Quiz Solution Jan-March 2020

Week-1 Quiz Solution (Last Date : 12/02/2020)



  1.  6
  2. 35
  3. 4
  4. d