Advertisement
gruntfutuk

3numsort

Oct 26th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. #!/usr/bin/python3
  2. #
  3. # input three numbers and sort them into ascending order without using any
  4. # built in sorting functions
  5.  
  6.  
  7. def getlist():
  8.     nums = []
  9.     while len(nums) < 3:
  10.         num = input('Enter a whole positive number: ')
  11.         if num.isdecimal():
  12.             nums.append(int(num))
  13.         else:
  14.             print(f'{num.rstrip()} is  not a whole positive number.')
  15.     return(nums)
  16.  
  17.  
  18. def threeswap(l, i):  # swap ith and i+1th elements in list l
  19.     l[i:i+1], l[i+1:i+2] = l[i+1:i+2], l[i:i+1]
  20.     return l
  21.  
  22. def threesort(three):
  23.     if three[0] <= three[1]:
  24.         if three[1] <= three[2]:
  25.             return
  26.         else:
  27.             threeswap(three, 1)
  28.     else:
  29.         threeswap(three, 0)
  30.     threesort(three)
  31.  
  32.  
  33. threelist = getlist()
  34. threesort(threelist)
  35. print(f'smallest: {threelist[0]}, mid: {threelist[1]}, largest: {threelist[2]}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement