0% found this document useful (0 votes)
132 views6 pages

Assignments NPTEL

The document contains Python code definitions for various functions: intreverse to reverse an integer, matched to check balanced parentheses, factors and isprime for prime number checks, sumprimes to sum prime numbers, and other functions for tasks like checking expanding sequences, accordion sequences, and rotating a 2D array. It also includes code for tracking tennis player statistics by parsing match results.

Uploaded by

avinash kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views6 pages

Assignments NPTEL

The document contains Python code definitions for various functions: intreverse to reverse an integer, matched to check balanced parentheses, factors and isprime for prime number checks, sumprimes to sum prime numbers, and other functions for tasks like checking expanding sequences, accordion sequences, and rotating a 2D array. It also includes code for tracking tennis player statistics by parsing match results.

Uploaded by

avinash kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

WEEK 2 ASSIGNMENT

def intreverse(n):

ans = 0

while n > 0:

(d,n) = (n%10,n//10)

ans = 10*ans + d

return(ans)

def matched(s):

nested = 0

for i in range(0,len(s)):

if s[i] == "(":

nested = nested+1

elif s[i] == ")":

nested = nested-1

if nested < 0:

return(False)

return(nested == 0)

def factors(n):

factorlist = []

for i in range(1,n+1):

if n%i == 0:

factorlist = factorlist + [i]

return(factorlist)

def isprime(n):

return(factors(n) == [1,n])

def sumprimes(l):

sum = 0

for i in range(0,len(l)):

if isprime(l[i]):

sum = sum+l[i]

return(sum)
WEEK 3 ASSIGNMENT

def expanding(l):

for i in range(0,len(l)-2):

if (abs(l[i+2]-l[i+1])>abs(l[i+1]-l[i])):

Answer=True

else:

Answer=False

return Answer

return Answer

def accordian(l):

di=[]

res=[]

for x,y in zip(l[:],l[1:]):

d1=abs(y-x)

di.append(d1)

#print(di)

for i,j in zip(di[:],di[1:]):

if i<j:

flag=1

elif i==j:

flag=2

else:

flag=0

res.append(flag)

#print(flag)

for s in res:

if s==2:

return False

break

return all(m!=n for m,n in zip(res,res[1:]))

def rotate(A):

N = len(A[0])

for i in range(N // 2):


for j in range(i, N - i - 1):

temp = A[i][j]

A[i][j] = A[N - 1 - j][i]

A[N - 1 - j][i] = A[N - 1 - i][N - 1 - j]

A[N - 1 - i][N - 1 - j] = A[j][N - 1 - i]

A[j][N - 1 - i] = temp

for i in range(N):

print([A[i]],end="")

WEEK 4 ASSIGNMENT

def frequency(l):

unique_l = list(set(l))

freq_list = [l.count(x) for x in unique_l]

min_freq_list = [unique_l[x] for x in range(len(freq_list)) if freq_list[x] ==


min(freq_list)]

max_freq_list = [unique_l[x] for x in range(len(freq_list)) if freq_list[x] ==


max(freq_list)]

min_freq_list.sort()

max_freq_list.sort()

return (min_freq_list, max_freq_list)

def onehop(lst):

data = lst

data.sort(key=lambda tup: tup[0])

ans = []

for ele in lst:

x, y = ele

for ele1 in lst:

if ele != ele1:

xx, yy = ele1

if y == xx and x != yy and (x,yy) not in ans:

ans.append((x, yy))

ans = sorted(ans, key=lambda tup: (tup[0], tup[1]))

return ans

import ast
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 == "frequency":

arg = parse(farg)

print(frequency(arg))

if fname == "onehop":

arg = parse(farg)

print(onehop(arg))

WEEK 5 ASSIGNMENT

# Statistics will be stored as a dictionary

# Each key is a player name, each value is a list of 6 integers representing

# Best of 5 set matches won,

# Best of 3 set matches won,

# Sets won

# Games won

# Sets lost (store as negative number for comparison)

# Games lost (store as negative number for comparison)

stats = {}

# Read a line of input

line = input()

while line:

# Keep track of sets/games won and lost in this match

# with respect to winner of the match


(wsets,lsets,wgames,lgames) = (0,0,0,0)

# Extract winner, loser and string of setscores

(winner,loser,setscores) = line.strip().split(':',2)

# Extract sequence of sets from setscores

sets = setscores.split(',')

for set in sets:

# Process each set

(winstr,losestr) = set.split('-')

win = int(winstr)

lose = int(losestr)

wgames = wgames + win

lgames = lgames + lose

if win > lose:

wsets = wsets + 1

else:

lsets = lsets + 1

# Update statistics for each of the players

for player in [winner,loser]:

try:

stats[player]

except KeyError:

stats[player] = [0,0,0,0,0,0]

if wsets >= 3:

stats[winner][0] = stats[winner][0] + 1

else:

stats[winner][1] = stats[winner][1] + 1

stats[winner][2] = stats[winner][2] + wsets

stats[winner][3] = stats[winner][3] + wgames

stats[winner][4] = stats[winner][4] - lsets


stats[winner][5] = stats[winner][5] - lgames

stats[loser][2] = stats[loser][2] + lsets

stats[loser][3] = stats[loser][3] + lgames

stats[loser][4] = stats[loser][4] - wsets

stats[loser][5] = stats[loser][5] - wgames

line = input()

# Collect each player's stats as a tuple, name last

statlist = [(stat[0],stat[1],stat[2],stat[3],stat[4],stat[5],name) for name in stats.keys()


for stat in [stats[name]]]

# Sort the statistics in descending order

# Losing games are stored negatively for sorting correctly

statlist.sort(reverse = True)

# Print

for entry in statlist:

print(entry[6],entry[0],entry[1],entry[2],entry[3], -entry[4], -entry[5])

You might also like