I use the variable number as a string to use it as a array, sort it and return it as a ingeter again
def maxnum(number): lista = [] result = "" for char in str(number): lista.append(char) lista.sort(reverse = True) for char in lista: result = result + char return int(result)
maxnum = lambda n: int(n) if len(str(n)) == 1 else int(n) if len({*str(n)}) == 1 else int(n) if len(str(n)) < str(n).count("0")*2 else n if n < 100 and n % 10 == 0 else int(sorted([i for i in range(int("".join(sorted([*str(n)])[::-1])) + 1) if sorted([*str(i)]) == sorted([*str(n)])])[::-1][1]) if sorted([i for i in range(int("".join(sorted([*str(n)])[::-1])) + 1) if sorted([*str(i)]) == sorted([*str(n)])])[::-1][0] == n else int(sorted([i for i in range(int("".join(sorted([*str(n)])[::-1])) + 1) if sorted([*str(i)]) == sorted([*str(n)])])[::-1][0])- def maxnum(number):
- lista = []
- result = ""
- for char in str(number):
- lista.append(char)
- lista.sort(reverse = True)
- for char in lista:
- result = result + char
- return int(result)
import codewars_test as test from solution import maxnum import random @test.describe('Example Tests') def example_tests(): @test.it('Simple Tests') def example_test_case(): test.assert_equals(maxnum(4), 4) test.assert_equals(maxnum(12), 21) test.assert_equals(maxnum(101), 110) test.assert_equals(maxnum(4), 4) test.assert_equals(maxnum(13213), 33211) @test.it('random tests') def random_test_case(): for i in range(1, 607): i = random.randint(1, 10000) test.assert_equals(sorted([*str(maxnum(i))]), sorted([*str(i)]), str(i))
- import codewars_test as test
- from solution import maxnum
- import random
- @test.describe('Example Tests')
- def example_tests():
- @test.it('Simple Tests')
- def example_test_case():
- test.assert_equals(maxnum(4), 4)
test.assert_equals(maxnum(21), 12)test.assert_equals(maxnum(110), 101)- test.assert_equals(maxnum(12), 21)
- test.assert_equals(maxnum(101), 110)
- test.assert_equals(maxnum(4), 4)
- test.assert_equals(maxnum(13213), 33211)
- @test.it('random tests')
- def random_test_case():
- for i in range(1, 607):
- i = random.randint(1, 10000)
- test.assert_equals(sorted([*str(maxnum(i))]), sorted([*str(i)]), str(i))