-
Notifications
You must be signed in to change notification settings - Fork 0
/
largest_num.py
80 lines (55 loc) · 1.99 KB
/
largest_num.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Given a list of numbers combine them to make the largest numberself.
# 1. transform all numbers to strings
# 2. iterate over list with while loop
# 3. compare 0th index to right neighbor,
# 4. if smaller, move right
# 5. if == compare next index
# 6. if no next index, stay
# 7. join list to make one big string
# 8. return string as number
def gen_longest_num(nums):
""" Returns largest combination of numbers from list
>>> gen_longest_num([1,2,3])
321
>>> gen_longest_num([3, 30, 34, 5, 9])
9534330
>>> gen_longest_num([54, 546, 548, 60])
6054854654
"""
str_nums = [str(x) for x in nums]
rotated = True
while rotated:
rotated = False
# print range(len(nums)-1)
for i in range(len(str_nums)-1):
current = str_nums[i]
neighbor = str_nums[i+1]
if (neighbor + current) > (current + neighbor):
str_nums[i], str_nums[i+1] = str_nums[i+1], str_nums[i]
rotated = True
# str_nums = [str(x) for x in nums]
result = "".join(str_nums)
return int(result)
# if current[0] == neighbor[0]:
# longer = ""
# if len(current) > len(neighbor):
# longer = current
# else:
# longer = neighbor
#
# # evaluate the next number
# for k in range(1, len(longer) +1):
# try:
# if current[k] < neighbor[k]:
# str_nums[i], str_nums[i+1] = str_nums[i+1], str_nums[i]
# except IndexError:
# if neighbor
# do some iterative comparison
# elif current[0] < neighbor[0]:
#
# str_nums[i], str_nums[i+1] = str_nums[i+1], str_nums[i]
# rotated = True
# rotated = False
if __name__ == "__main__":
import doctest
doctest.testmod()