-
Notifications
You must be signed in to change notification settings - Fork 704
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47c6e6c
commit a9ca3c5
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |