Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Uses python3
- import sys
- import math
- # didn't pass tests #8/11
- def is_greater_or_equal5(n, current_max):
- # case n = 1000 any max will be greater except 1000!
- if len(n) == 4:
- if len(current_max) == 4:
- return True
- return False
- elif len(current_max) == 4:
- return True
- # otherwise if both numbers have same length a straight comparison works
- elif len(n) == len(current_max):
- return n >= current_max
- # other the possible pairs length are :
- # case A len(1,2)
- elif len(n) == 1 and len(current_max) == 2:
- if n[0] == current_max[0]:
- # we compare the second digit
- return n[0] >= current_max[1]
- return n[0] >= current_max[0]
- # case B len(1,3)
- elif len(n) == 1 and len(current_max) == 3:
- if n[0] == current_max[0]:
- # we compare the second digit
- if n[0] == current_max[1]:
- # we compare with the third digit
- return n[0] >= current_max[2]
- return n[0] >= current_max[1]
- return n[0] >= current_max[0]
- # case C len(2,1)
- elif len(n) == 2 and len(current_max) == 1:
- if n[0] == current_max[0]:
- # we compare the second digit
- return n[1] >= current_max[0]
- return n[0] >= current_max[0]
- # case D (2,3)
- elif len(n) == 2 and len(current_max) == 3:
- if n[0] == current_max[0]:
- # we compare with the second digit
- if n[1] == current_max[1]:
- # we compare with the last digit
- return n[1] >= current_max[2]
- return n[1] >= current_max[1]
- return n[0] >= current_max[0]
- # case E (3,1)
- elif len(n) == 3 and len(current_max) == 1:
- if n[0] == current_max[0]:
- # we compare the second digit
- if n[1] == current_max[0]:
- # we compare the third digit
- if n[2] == current_max[0]:
- return True
- return n[2] >= current_max[0]
- return n[1] >= current_max[0]
- return n[0] >= current_max[0]
- # case F (3,2)
- elif len(n) == 3 and len(current_max) == 2:
- if n[0] == current_max[0]:
- # we compare the second digit
- if n[1] == current_max[1]:
- # we compare the third digit.
- if n[2] == current_max[1]:
- return True
- return n[2] >= int(current_max[1])
- return n[1] >= current_max[1]
- return n[0] >= current_max[0]
- # did pass all the tests thanks Mo Xin's hint
- def compare_strings(s1, s2):
- first = s1+s2
- last = s2+s1
- return first >= last
- def largest_number(a):
- # straight forward implementation of pseudo code given in assignment
- res = ""
- while a:
- max_digit = "0"
- for x in a:
- if compare_strings(x, max_digit):
- max_digit = x
- res += max_digit
- a.remove(max_digit)
- return res
- if __name__ == '__main__':
- user_input = sys.stdin.read()
- data = user_input.split()
- digits = data[1:]
- print(largest_number(digits))
Add Comment
Please, Sign In to add comment