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

Leave a Reply

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