Question :-

  1. Write a Python function frequency(l) that takes as input a list of integers and returns a pair of the form (minfreqlist,maxfreqlist) where
    • minfreqlist is a list of numbers with minimum frequency in l, sorted in ascending order
    • maxfreqlist is a list of numbers with maximum frequency in l, sorted in ascending

    For instance

    >>> frequency([13,12,11,13,14,13,7,11,13,14,12])
    ([7], [13])
    
    >>> frequency([13,12,11,13,14,13,7,11,13,14,12,14,14])
    ([7], [13, 14])
    
    >>> frequency([13,12,11,13,14,13,7,11,13,14,12,14,14,7])
    ([7, 11, 12], [13, 14])
  2.  airline has assigned each city that it serves a unique numeric code. It has collected information about all the direct flights it operates, represented as a list of pairs of the form (i,j), where i is the code of the starting city and j is the code of the destination.

    It now wants to compute all pairs of cities connected by one intermediate hope — city i is connected to city j by one intermediate hop if there are direct flights of the form (i,k) and (k,j) for some other city k. The airline is only interested in one hop flights between different cities — pairs of the form (i,i) are not useful.

    Write a Python function onehop(l) that takes as input a list of pairs representing direct flights, as described above, and returns a list of all pairs (i,j), where i != j, such that i and j are connected by one hop. Note that it may already be the case that there is a direct flight from i to j. So long as there is an intermediate k with a flight from i to k and from k to j, the list returned by the function should include (i,j). The input list may be in any order. The pairs in the output list should be in lexicographic (dictionary) order. Each pair should be listed exactly once.

    For instance

     

    >>> onehop([(2,3),(1,2)])
    [(1, 3)]
    
    >>> onehop([(2,3),(1,2),(3,1),(1,3),(3,2),(2,4),(4,1)])
    [(1, 2), (1, 3), (1, 4), (2, 1), (3, 2), (3, 4), (4, 2), (4, 3)]
    
    >>> onehop([(1,2),(3,4),(5,6)])
    []

Solution :-

Download Python File :- Download

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]) #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[0], tup[1]))  #Sorting all tuples in ascending order via first element and second element.

    #ans.sort(key=lambda tup: tup[0])
    return ans

Can be tested with the following code (Test Cases) :-

## = Answer of that test case.

print(frequency([13,12,11,13,14,13,7,11,13,14,12]))
## ([7], [13])

print(frequency([13,12,11,13,14,13,7,11,13,14,12,14,14]))
## ([7], [13, 14])

print(frequency([13,12,11,13,14,13,7,11,13,14,12,14,14,7]))
## ([7, 11, 12], [13, 14])


print(onehop([(2,3),(1,2)]))
## [(1, 3)]

print(onehop([(2,3),(1,2),(3,1),(1,3),(3,2),(2,4),(4,1)]))
## [(1, 2), (1, 3), (1, 4), (2, 1), (3, 2), (3, 4), (4, 2), (4, 3)]

print(onehop([(1,2),(3,4),(5,6)]))
## []
Categories: NPTEL solution

22 Comments

krish · August 26, 2017 at 8:11 AM

Working fine… Thanks a lot guys

    Mit Patel · September 5, 2017 at 7:33 PM

    Thanks Krish . Keep visiting 😛

Anonymous · August 28, 2017 at 4:35 PM

Hello,
Please post week 5 assignment in programming and data structures in python, it’s confusing.
thankyou.

Karan · August 29, 2017 at 5:00 AM

Please provide solutions for week 5

kumar · August 30, 2017 at 2:07 PM

Hello
post the week 5 assignment for datastructures in python

kumar · August 30, 2017 at 2:08 PM

Hello
post the week5 assignment in datastructures in python

Titir · September 1, 2017 at 10:22 AM

Please post week 5 assignment

Raghu · September 1, 2017 at 2:26 PM

Please post week 5 programming assignment solution link

    Raghu · September 1, 2017 at 2:26 PM

    Python week 5 assignment solution required

Unknown · September 1, 2017 at 3:23 PM

Can you upload Python week 5 programming assignment solution ?

secret identity · September 1, 2017 at 3:35 PM

week 5 programming assignment solution please

Mayuresh · September 2, 2017 at 4:45 AM

sir please upload week 5 programming assignment
Today is the last day.please sir

Mayuresh · September 2, 2017 at 4:49 AM

sir please upload it…

shiv · September 2, 2017 at 5:51 AM

sir plz upload week 5 programming assignment

Saurab.B · September 2, 2017 at 6:05 AM

yeah please upload week 5 assignment please…..

    Mit Patel · September 5, 2017 at 7:29 PM

    It has been posted.

Hank · September 2, 2017 at 9:54 AM

Sir please upload solutions for week 5

sam · September 2, 2017 at 10:47 AM

plz upload week 5 assingnment answer

Mayuresh · September 2, 2017 at 12:08 PM

Sir today is last day for it please upload it.

suresh · September 2, 2017 at 1:03 PM

sir plz upload week 5 programming assignment

Thejaswini · September 2, 2017 at 2:25 PM

sir please upload assignment 5

lakith · September 2, 2017 at 2:34 PM

sir, please upload week 5 assignment

Leave a Reply

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