Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

python - 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) 
question from:https://stackoverflow.com/questions/65946597/creating-a-number-from-smallest-digits-of-other-numbers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...