Home > Python >Python Exercise: Doubles

Doubles

You will be given randomly generated lists of 2–15 unique positive integers and asked to determine how many items in each list are twice some other item in the same list. This program should be able to scan the lists and output the correct answer for each one. For example, given the list

1 4 3 2 9 7 18 22

your program should answer 3, as 2 is twice 1, 4 is twice 2, and 18 is twice 9.
Input
The input will be one list of numbers separated by comma. Each list will contain from 2 to 15 unique positive integers. No integer will be larger than 99.
Output
The output will consist of the input list => a count of the items that are double some other item. For example:

1 4 3 2 9 7 18 22 => 3

You need login to comment

Solution submitted by Dzeng at June 18, 2020, 7:49 p.m.
def doubles (set1): set2 = set() for i in set1: set2.add(i * 2) output = set1.intersection(set2) print("The doubles are:", output, "and there are:", len(output), "of them.") doubles({1,2,3,4})

You need login to submit your solution