jules0707

largest_number

Oct 10th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. # Uses python3
  2.  
  3. import sys
  4. import math
  5.  
  6. # didn't pass tests #8/11
  7. def is_greater_or_equal5(n, current_max):
  8.     # case n = 1000 any max will be greater except 1000!
  9.     if len(n) == 4:
  10.         if len(current_max) == 4:
  11.             return True
  12.         return False
  13.     elif len(current_max) == 4:
  14.         return True
  15.     # otherwise if both numbers have same length a straight comparison works
  16.     elif len(n) == len(current_max):
  17.         return n >= current_max
  18.     # other the possible pairs length are :
  19.     # case A len(1,2)
  20.     elif len(n) == 1 and len(current_max) == 2:
  21.         if n[0] == current_max[0]:
  22.             # we compare the second digit
  23.             return n[0] >= current_max[1]
  24.         return n[0] >= current_max[0]
  25.     # case B len(1,3)
  26.     elif len(n) == 1 and len(current_max) == 3:
  27.         if n[0] == current_max[0]:
  28.             # we compare the second digit
  29.             if n[0] == current_max[1]:
  30.                 # we compare with the third digit
  31.                 return n[0] >= current_max[2]
  32.             return n[0] >= current_max[1]
  33.         return n[0] >= current_max[0]
  34.     # case C len(2,1)
  35.     elif len(n) == 2 and len(current_max) == 1:
  36.         if n[0] == current_max[0]:
  37.             # we compare the second digit
  38.             return n[1] >= current_max[0]
  39.         return n[0] >= current_max[0]
  40.     # case D (2,3)
  41.     elif len(n) == 2 and len(current_max) == 3:
  42.         if n[0] == current_max[0]:
  43.             # we compare with the second digit
  44.             if n[1] == current_max[1]:
  45.                 # we compare with the last digit
  46.                 return n[1] >= current_max[2]
  47.             return n[1] >= current_max[1]
  48.         return n[0] >= current_max[0]
  49.     # case E (3,1)
  50.     elif len(n) == 3 and len(current_max) == 1:
  51.         if n[0] == current_max[0]:
  52.             # we compare the second digit
  53.             if n[1] == current_max[0]:
  54.                 # we compare the third digit
  55.                 if n[2] == current_max[0]:
  56.                     return True
  57.                 return n[2] >= current_max[0]
  58.             return n[1] >= current_max[0]
  59.         return n[0] >= current_max[0]
  60.     # case F (3,2)
  61.     elif len(n) == 3 and len(current_max) == 2:
  62.         if n[0] == current_max[0]:
  63.             # we compare the second digit
  64.             if n[1] == current_max[1]:
  65.                 # we compare the third digit.
  66.                 if n[2] == current_max[1]:
  67.                     return True
  68.                 return n[2] >= int(current_max[1])
  69.             return n[1] >= current_max[1]
  70.         return n[0] >= current_max[0]
  71.    
  72.  
  73. # did pass all the tests thanks Mo Xin's hint
  74. def compare_strings(s1, s2):
  75.     first = s1+s2
  76.     last = s2+s1
  77.     return first >= last
  78.  
  79.  
  80. def largest_number(a):
  81.     # straight forward implementation of pseudo code given in assignment
  82.     res = ""
  83.     while a:
  84.         max_digit = "0"
  85.         for x in a:
  86.             if compare_strings(x, max_digit):
  87.                 max_digit = x
  88.         res += max_digit
  89.         a.remove(max_digit)
  90.     return res
  91.  
  92.  
  93. if __name__ == '__main__':
  94.     user_input = sys.stdin.read()
  95.     data = user_input.split()
  96.     digits = data[1:]
  97.     print(largest_number(digits))
Add Comment
Please, Sign In to add comment