# Programming Data Structures And Algorithms Using Python Week 4 Programming Assignment | NPTEL

## Programming Data Structures And Algorithms Using Python Week 4 Programming Assignment | NPTEL

In this article i am gone to share NPTEL Course Programming Data Structures And Algorithms Using Python Week 4 Programming Assignment with you..

#### Week 4 Programming 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) #Sorting all tuples in ascending order via first element

ans = []                   # Creating a blank List

for ele in lst:            # Looping through all elements

x, y = ele             # Storing Tuple value in x and y

for ele1 in lst:       # For finding next hop for all destinations

if ele != ele1:    # To check if it’s not the same element in loop

xx, yy = ele1  # Storing another tuple’s value in xx, yy

if y == xx and x != yy and (x,yy) not in ans: #checking conditions for adding to ans.

# y == xx to check if second element of first tuple is equal to first element of second tuple.

# Only then Next Hop is possible

# x != y, so that we don’t get tuples like (2,2), (3,3) etc.

# (x,yy) not in ans, is to ensure that one next hope isn’t repeated twice.

ans.append((x, yy)) #Adding tuple to ans if all conditions satisfied.

ans = sorted(ans, key=lambda tup: (tup, tup))  #Sorting all tuples in ascending order via first element and second element.

#ans.sort(key=lambda tup: tup)

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))