Write a program to output the indexes of the elements in tuple format. | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

Write a program to output the indexes of the elements in tuple format.

When elements are subtracted, the value of the subtraction should be 30. Sample Example. Input type: list Input: [15, 45, 75, 95] Output type: tuple Output: (1,0) (2,1) Explanation: 45 is at index 1. 15 is at index 0. 45-15 =30. Hence tuple (1, 0) is obtained.Similarly, 75 is at index 2. 45 is at index 1.. 75-45=30. Hence tuple (2,1) is obtained.

8th Nov 2022, 2:01 AM
G Sree Vani
3 Réponses
+ 8
G Sree Vani , here are some thoughts about how to solve the task: > we need to create pairs of numbers coming from the input list. > as a tool to do so, we can use the python modul *itertools*, and the function can be permutations() or combinations(). in our case i suggest to use combinations(). > combinations() takes 2 arguments: an iterable like our list, and an int value that describes the number of values to take from the list. the result of using this function will be an iterator. > we can use this iterator now in a loop to check if the subtraction of the 2 numbers results in the target value of 30. if this condition is true, calculate the index position of the current values, and print it as tuple. > otherwise the next iteration cycle will be done.
8th Nov 2022, 2:59 PM
Lothar
Lothar - avatar
+ 3
a= 1,2 print(a)
8th Nov 2022, 6:43 AM
Oma Falk
Oma Falk - avatar
+ 2
https://code.sololearn.com/clNoMOks9fyQ/?ref=app maybe product is a good choice too.
8th Nov 2022, 8:43 PM
Oma Falk
Oma Falk - avatar