Roko Smuđa
Roko Smuđa

Reputation: 11

Creating a number from smallest digits of other numbers

I'm looking for a way to create a number which is composed from the smallest digits of other numbers. It starts with an input which directs how many numbers there will be. After that the program needs to take input from those numbers and find the smallest digit. In the end it should create a new number which is made from all the smallest numbers one next to another. I figured out how to find the smallest digit, but don't know what to do next.

Example:

4 #first number

#4 numbers since the first input equals 4 (each one in its own line)
123456
345345
2423262
644243

1323 #number made from the smallest digits of the 4 numbers above
num=int(input())
smallest=num%10
while num > 0:
    reminder = num % 10
    if smallest > reminder:
        smallest = reminder
    num =int(num / 10)
print("The Smallest Digit is ", smallest) 

Upvotes: 0

Views: 785

Answers (3)

Joe Ferndz
Joe Ferndz

Reputation: 8508

You can do this without converting the numbers into integers.

Print smallest number from the small digits in each line

n = int(input())
y = [min(input()) for _ in range(n)] 
y.sort()
print (''.join(y))

Input:

4
123456
345345
2423262
644243

Output will be:

1223

This will give you the smallest digit of the smallest digits you found.

If you just want to print the number in the same order, you can exclude the y.sort() line.

n = int(input())
y = [min(input()) for _ in range(n)] 
print (''.join(y))

Input:

4
123456
345345
2423262
644243

Output:

1322

Upvotes: 1

Rasputin
Rasputin

Reputation: 171

Wouldn't it just be this (below)? I omitted your limitation that num cannot be 0. I didn't understand the purpose of that.

num=int(input())
listOfSmallestDigits = []
i = 0
while i < num:
     smallest=num%10
     remainder = num % 10
     if smallest > reminder:
        smallest = reminder
     print("The Smallest Digit is ", smallest)
     listOfSmallestDigits.append(smallest)

# Create a String Variable to Collect All the New Smallest Digits in a String
newDigit = ""
for digit in listOfSmallestDigits:
  newDigit = newDigit + digit

print("The final number is: {}".format(newDigit)) 

Also, if you want to use the newDigit as a number later on, just use this:

newDigit = int(newDigit)

Upvotes: -1

John Skeen
John Skeen

Reputation: 223

If I've understood the question correctly, I believe something like this is what you're after:

def smallest_digit(num):
    smallest = num % 10
    while num > 0:
        reminder = num % 10
        if smallest > reminder:
            smallest = reminder
        num = int(num / 10)
    return smallest

small_num = ""
number_of_inputs = int(input("Number of inputs: "))

for i in range(number_of_inputs):
    small_num += str(smallest_digit(int(input())))

First, the user is asked for the number of numbers they wish to input.

Then, the for loop will ask the user for an input number for the correct number of times that the user wanted.

This input is passed straight to the function smallest_digit (this is using your code from above to find the smallest digit in a given number).

The function returns the smallest digit, which is then added to a string called small_num.

Upvotes: 1

Related Questions