Skip to content

Instantly share code, notes, and snippets.

@modos
Created July 30, 2023 05:02
Show Gist options
  • Save modos/19bca059749111590716f0febb89624c to your computer and use it in GitHub Desktop.
Save modos/19bca059749111590716f0febb89624c to your computer and use it in GitHub Desktop.
باقر مخالف است
from typing import Optional, List
def get_smaller_digit(sorted_digits: List[str], bigger_than: str) -> Optional[str]:
for i in range(len(sorted_digits)):
if sorted_digits[i] > bigger_than:
return sorted_digits[i]
return None
def solve(number: str) -> str:
for i in range(len(number) - 2, -1, -1):
remain_digits = sorted(number[i:])
smaller_digit = get_smaller_digit(remain_digits, number[i])
if smaller_digit is None:
continue
remain_digits.remove(smaller_digit)
return number[0:i] + smaller_digit + ''.join(remain_digits)
return '0'
def main():
number = input()
result = solve(number)
print(result)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment