SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
0.lists
Exercise:
Programming Excercises
Question:24 | ISBN:978013274719 | Edition: 6

Question

(Math: combinations) Write a program that prompts the user to enter 10 integers and displays all the combinations of picking two numbers from the 10.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program Code:

from itertools import combinations

# Prompt the user to enter 10 integers separated by spaces
num = input("Enter 10 integer numbers: ")

# Convert the input string to a list of integers
numbers = [int(num) for num in num.split()]

# Get all combinations of picking two numbers from the list
combinationsNums = list(combinations(numbers, 2))

# Display the combinations
print("All combinations of picking two numbers from the list:")
for combination in combinationsNums:
    print(combination)

 

Executed Output:

Enter 10 integer numbers: 2 4 6 8 10 5 10 15 20 1
All combinations of picking two numbers from the list:
(2, 4)
(2, 6)
(2, 8)
(2, 10)
(2, 5)
(2, 10)
(2, 15)
(2, 20)
(2, 1)
(4, 6)
(4, 8)
(4, 10)
(4, 5)
(4, 10)
(4, 15)
(4, 20)
(4, 1)
(6, 8)
(6, 10)
(6, 5)
(6, 10)
(6, 15)
(6, 20)
(6, 1)
(8, 10)
(8, 5)
(8, 10)
(8, 15)
(8, 20)
(8, 1)
(10, 5)
(10, 10)
(10, 15)
(10, 20)
(10, 1)
(5, 10)
(5, 15)
(5, 20)
(5, 1)
(10, 15)
(10, 20)
(10, 1)
(15, 20)
(15, 1)
(20, 1)

 

 

0 0

Discussions

Post the discussion to improve the above solution.