Question

In: Computer Science

create a function that sorted tuple by the second values of each tuple. If the values...

create a function that sorted tuple by the second values of each tuple. If the values are the same, sorted by increasing order. Finally returns a list of the first element of each tuple that sorted.

def sort_tup(tup):

#Code here

input : tup1 = [(1, 15), (2, 8), (3, 22), (4, 30), (5, 15)]

output: [4,3,1,5,2]

Solutions

Expert Solution

Code Screenshot :

Executable Code:

#Required function
def sort_tup(tup):
   #Empty list
   result = []
   #Sorting the tuple
   sorted_by_second = sorted(tup,key=lambda tup: tup[1],reverse = True)
   for i in sorted_by_second:
       #Append elements to the list
       result.append(i[0])
   #Return the resultant list
   return result


#Testing and Output
tup1 = [(1, 15), (2, 8), (3, 22), (4, 30), (5, 15)]
print("input:",tup1)
print("output:",sort_tup(tup1))

Sample Output :

Please comment below if you have any queries.
Please do give a thumbs up if you liked the answer thanks :)


Related Solutions

Define a function named posterize. This function expects an image and a tuple of RGB values...
Define a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function developed in Chapter 7 and shown below, but it uses passed in RGB values instead of black. def blackAndWhite(image): """Converts the argument image to black and white.""" blackPixel = (0, 0, 0) whitePixel = (255, 255, 255) for y in range(image.getHeight()): for x in range(image.getWidth()): (r, g, b) = image.getPixel(x, y) average =...
ARDUINO Create a function that will ask the user to enter the values for each of...
ARDUINO Create a function that will ask the user to enter the values for each of the three colors (red, green and blue) between 0 and 255. The function should check that the user does not enter a number bigger than 255 or smaller than 0. If this happens, the function should keep asking for a valid number.
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many...
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many times each person party in the day. For example def party_freq(dicto:dict) -> [(str,{(int,int,int): int})]: #code here input dict1 ={'fire1': {(2000,5,20,480) : ('Aaron', 25, 300, ( 0, 300)), (2000,5,20,720) : ('Baily', 45, 1500, (1500,500)), (2000,5,21,490) : ('Aaron', 35, 500, (1300,500)) }, 'fire2': {(2000,5,20,810) : ('Baily', 45, 1400, (600,1600)), (2000,5,20,930) : ('Baily', 43, 1800, ( 0, 0)) }} output [('Aaron', {(2000,5,20): 1, (2000,5,21): 1}), ('Baily', {(2000,5,20):...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple in the #list will have two items: a string and an integer. The #string will represent the name of a movie, and the integer #will represent that movie's total ticket sales (in millions #of dollars). # #The function should return the movie from the list that #had the most sales. Return only the movie name, not the #full tuple. #Below are some lines of...
In R, Create a function that replaces the negative values in a numeric matrix with a...
In R, Create a function that replaces the negative values in a numeric matrix with a random integer between 1 and 10. At the same time, it counts the negative values and prints their number. Apply the function to a 4 by 5 matrix of random continuous uniform values ranging from -10 to 10.
1. Create a program that generates n values in range [low, high]. • Create a function...
1. Create a program that generates n values in range [low, high]. • Create a function generate_vals which takes as input an array of double values and three integers representing the size of the array, low value of range, and high value of range, respectively. The function should use rand() to generate the requested number of values. • Prompt the user to enter the size of their array as shown below. It cannot exceed a maximum size of 128. If...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values in the dictionary. For example: dict1 = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':4}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} dict2 = {300:{'s': 300}, 400:{'s': 100, 'g': 100, 'p': 100}, 500: {'s': 50 ,'m': 400, 'p':30, 'i': 50}, 600: {'s': 40, 'i': 400}, 700: {'m': 100, 'p': 50}} def dict_sum(db): should give output...
Create a database to hold a collection of numbers to be searched and sorted: 1) Create...
Create a database to hold a collection of numbers to be searched and sorted: 1) Create a main class and a database class 2) Collect 5 random numbers from the user in the main class and store in an array. (not in ascending or descending order) 3) Create an instance of the database class in your main class and store the numbers array in the database using its constructor. 4) In the Database class, create a method that performs a...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
You will overload a function named printArray. The function will assign values to each element and...
You will overload a function named printArray. The function will assign values to each element and print out a single dimension array of integers and an array of characters. The main body of the program will call each function. In C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT