Skip to content

Commit 3060a00

Browse files
Create maximum_combination.py
1 parent a03b3f7 commit 3060a00

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

maths/maximum_combination.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Source: https://www.instagram.com/p/CG7kv65A6s1/?utm_source=ig_web_copy_link
3+
4+
The following function accepts 2 positive integers and makes the first number as large as possible by swapping out its digits for digits in second numebr
5+
>>> maximum_combination(9132,5564)
6+
9655
7+
>>> maximum_combination(523,76)
8+
763
9+
>>> maximum_combination(8732, 91255)
10+
9755
11+
>>> maximum_combination(6472,0)
12+
6472
13+
>>> maximum_combination(-12,10)
14+
0
15+
"""
16+
17+
18+
def maximum_combination(first_num, second_num):
19+
if first_num <= 0:
20+
return 0
21+
if second_num <= 0:
22+
return first_num
23+
second_num = [int(x) for x in str(second_num)]
24+
first_num = [int(x) for x in str(first_num)]
25+
second_num.sort(reverse=True)
26+
for index, val in enumerate(first_num):
27+
if len(second_num) > 0:
28+
if second_num[0] > val:
29+
first_num[index] = second_num[0]
30+
second_num.pop(0)
31+
else:
32+
break
33+
return int("".join(map(str, first_num)))
34+
35+
36+
if __name__ == "__main__":
37+
from doctest import testmod
38+
39+
testmod()

0 commit comments

Comments
 (0)