From 628112a4e70119fe734ae390eae0db6e5852f674 Mon Sep 17 00:00:00 2001 From: garrettmasters Date: Sat, 24 Oct 2020 15:47:40 -0700 Subject: [PATCH] added ThanosSort in python --- ThanosSort/ThanosSort.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 ThanosSort/ThanosSort.py diff --git a/ThanosSort/ThanosSort.py b/ThanosSort/ThanosSort.py new file mode 100644 index 00000000..d7ce0e6a --- /dev/null +++ b/ThanosSort/ThanosSort.py @@ -0,0 +1,32 @@ +import random +import typing.List + +def thanos_sort(a: List[int]) -> List[int]: + '''Removes half of the list until it's perfectly balanced, like all things should be.''' + + def _perfectly_balanced(a: List[int]) -> bool: + + like_all_things_should_be = True + + for i in range(1, len(a)): + if a[i] < a[i-1]: + like_all_things_should_be = False + break + + return like_all_things_should_be + + def _snap(a: List[int]) -> List[int]: + + numbers_that_dont_feel_so_good = random.sample(range(len(a)), round(len(a)/2, 0)) + + b = [] + for i in range(len(a)): + if i not in numbers_that_dont_feel_so_good: + b.append(a[i]) + + return b + + while not _perfectly_balanced(a): + a = _snap(a) + + return a