简体   繁体   中英

Python - Get second smallest number without using array

I want to print the second smallest number without using any array but it doesn't work. This is what I've done so far.

numbers = 5
print('Enter your numbers\n')

for x in range(0, numbers):
    use = input('Enter #' + str(x + 1) + ': ')
    if x == 0:
        small = int(use)
        second = small
    if int(use) < small:
        small = int(use)
        if small > second:
            second = int(use)

print('\nSecond smallest number is ' + str(second))

code:

numbers = 5
min = 2**32
second_min = 2**32
print('Enter your numbers\n')

for x in range(numbers):
    use = input('Enter #' + str(x + 1) + ': ')
    if int(use) < min:
        min = int(use)
    elif int(use) >= min and int(use) < second_min:
        second_min = int(use)

print('\nSecond smallest number is ' + str(second_min))

result:

Enter your numbers

Enter #1: 5
Enter #2: 6
Enter #3: 7
Enter #4: 8
Enter #5: 9

Second smallest number is 6
numbers = 5
print('Enter your numbers\n')

small = float('inf')
second = float('inf')

for x in range(0, numbers):
    use = int(input('Enter #' + str(x + 1) + ': '))
    if use < small:
       small = use
    if use > small and use < second:
        second = use

print('\nSecond smallest number is ' + str(second))

PS float('inf') is most biggest number

This works without lists and for number in any order.

numbers = 5
print('Enter your numbers\n')
#Instead of using lists, we'll use variables to store the smallest numbers. We'll give them the initial value of 0.
smallest = 0
secondsmallest = 0
    
for x in range(0, numbers):
    #We get the user input and convert it to int.
    use = int(input('Enter #' + str(x + 1) + ': '))
    #If the smallest variable hasn't been touched, i.e., it's the first time the for loop is run, we assign the current input to be the smallest number.
    if smallest == 0:
        smallest = use
    #Else if the current input is smaller than the current smallest number, then it should be the new smallest number.
    elif use < smallest:
        smallest = use
    #Else if the current input is greater than the smallest and the secondsmallest number still is untouched then the current input should be the secondsmallest.
    elif use > smallest and secondsmallest == 0:
        secondsmallest = use
    #Else if the current input is less than the secondsmallest, then it should be the new secondsmallest.
    elif use < secondsmallest:
        secondsmallest = use
        
print(secondsmallest)

Code:

nums = [1, 2, 9, 11, 13]
for counter,num in enumerate(nums):
 
    if counter == 0:
        smallest = num
    if counter == 1:
        second = num
    if counter > 1:
        if second == smallest:
            second = num
        if num < second:
            second = num
        if second < smallest:
            smallest, second = second, smallest
                     
print(second)
    

Here is the algorithm of my approach:

  1. First initialise the smallest and second smallest numbers to be the first two inputs. That is, Smallest=First_Input and second_smallest = Second_Input . These lines do that:
    if counter == 0:
        smallest = num
    if counter == 1:
        second = num
  1. Next See the third number. If the third number is smaller than the second_smallest number, then swap them. These lines do that
    # This part is executed only when the counter exceeds 1
    # That is, from the third input onwards.
    if num < second:
        second = num
  1. Now See the smallest and second_smallest number. If second_smallest < smallest , then swap them. These lines do that:
    if second < smallest:
        smallest, second = second, smallest
  1. Now, there will be an edgecase for inputs [smallest, smallest, x, y, z] . That is, when smallest number is repeated. It is because, the smallest and second variables store the first smallest and second smallest numbers respectively. But when the smallest number is repeated, they both store the same value(ie the smallest value). Therefore in this case, the actual second_smallest value will not be able to replace the value in second (since second stores the smallest in this case) To fix this, We have this line of code:
     if second == smallest:
         second = num

We can solve the problem by using another one temp variable.
Let's assume to input one variable - v.
First , compare v with the smallest number.
If v is smaller than the smallest number, the smallest number will be v and the second small number will be a previous smallest number.
Second , if v is equal or bigger than the smallest number, compare with the second small number and handle it.

import sys
numbers = 5
second = sys.maxsize
smallest = sys.maxsize

for x in range(0, numbers):
    use = int(input('Enter #' + str(x + 1) + ': '))
    if use < smallest:
      second = smallest
      smallest = use
    elif use < second:
      second = use

print('\nSecond smallest number is ' + str(second))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM