Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # please get the 1st and 2nd largest number from the combination of a given number
- perm_mem = {}
- def perm(_list):
- global perm_mem
- if perm_mem.get(tuple(_list)) is not None:
- return perm_mem[tuple(_list)]
- if len(_list) == 1:
- perm_mem[tuple(_list)] = [_list]
- return [_list]
- else:
- result = []
- for i in range(len(_list)):
- x = _list[i]
- y = _list[:i] + _list[i+1:]
- for p in perm(y):
- if [x] + p not in result:
- result.append([x] + p)
- perm_mem[tuple(_list)] = result
- return result
- input = 4358
- result = perm(list(str(input)))
- output = []
- [output.append(int(''.join(map(str, x)))) for x in result]
- print(result)
- print(output)
- output.sort(reverse=True)
- print(output)
- print(f'1st largest: {output[0]} and 2nd largest: {output[1]}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement