Largest and Smallest Integers Combinations

Largest and Smallest Integers Combinations

Python Practice Exercise

Given a list of integers separated by a single space, write a Python script that prints out the largest and smallest values that can be obtained by concatenating the integers together. The program should ask the user to enter the integers.

Steps

  1. Take Input:
    • Read the list of integers from the standard input.
  2. Generate Combinations:
    • Generate all possible combinations of the integers by concatenating them together.
  3. Find Largest and Smallest:
    • Identify the largest and smallest values from the generated combinations.
  4. Display Result:

Expected output:

Enter a list of integers separated by a single space: 45 96 21 5
The largest value is: 9654521
The smallest value is: 2145596

Solution


from itertools import permutations

# Step 1: Take Input
input_list = input("Enter a list of integers separated by a single space: ").split()

# Step 2: Generate Combinations
combinations = [''.join(p) for p in permutations(input_list)]

# Step 3: Find Largest and Smallest
largest = max(combinations)
smallest = min(combinations)

# Step 4: Display Result
print(f"The largest value is: {largest}")
print(f"The smallest value is: {smallest}")

Practity
We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for Practice
Register New Account
Shopping cart