-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJ_test_Q4.py
63 lines (41 loc) · 1.43 KB
/
J_test_Q4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Test Question 4 from J interview
# inputs = [60, 8, 2, 4, 5]
# output = 860542 (the max integer that can be formed by the numbers in inputs)
class Number:
def __init__(self, value):
# self.value = value
self.strr = str(value) # string representation
self.length = len(self.strr) # total number of digits
def __lt__(self, other):
count = min(self.length, other.length)
for i in range(count):
if self.strr[i] < other.strr[i]:
return True # e.g. 60 < 8
if self.strr[:count] == other.strr[:count]:
if self.length > other.length \
and self.strr[count] < other.strr[0]:
return True # e.g. 605 < 60
if self.length < other.length \
and self.strr[0] <= other.strr[count]:
return True # e.g. 60 < 608
return False
def __str__(self):
return self.strr
def __repr__(self):
return self.strr
def main():
inputs = [60, 608, 8, 605, 606]
numbers = []
print("inputs = {}".format(inputs))
for item in inputs:
numbers.append(Number(item))
# sort in ascending order
numbers.sort(reverse=True)
print("sorted = {}".format(numbers))
result = ""
for item in numbers:
result = result + item.strr
print("result = {}".format(result))
return int(result)
if __name__ == "__main__":
main()