aboutsummaryrefslogtreecommitdiff
path: root/challenge-284/sgreen/python/ch-2.py
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2024-09-01 18:59:00 +1000
committerSimon Green <mail@simon.green>2024-09-01 18:59:00 +1000
commit55c3d22707c0a0d6f119d16b6024a78bd1d901d2 (patch)
tree5919d5ef4df8f45c21bb34585fdb69ef729e63b3 /challenge-284/sgreen/python/ch-2.py
parent3726f5d22a659f31a090dd40e9e888f440850596 (diff)
downloadperlweeklychallenge-club-55c3d22707c0a0d6f119d16b6024a78bd1d901d2.tar.gz
perlweeklychallenge-club-55c3d22707c0a0d6f119d16b6024a78bd1d901d2.tar.bz2
perlweeklychallenge-club-55c3d22707c0a0d6f119d16b6024a78bd1d901d2.zip
sgreen solutions to challenge 284
Diffstat (limited to 'challenge-284/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-284/sgreen/python/ch-2.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-284/sgreen/python/ch-2.py b/challenge-284/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..a236c11600
--- /dev/null
+++ b/challenge-284/sgreen/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python3
+
+import json
+import sys
+
+
+def find_index(lst: list, val: int):
+ # Find the position of val in lst. If it does not appear, return the
+ # length of the list
+ return lst.index(val) if val in lst else len(lst)
+
+
+def relative_sort(list1: list, list2: list) -> list:
+ # Sort the first list, in the order they appear in the second list. If
+ # it doesn't appear in the second list, append to the end in numerical order.
+ return sorted(list1, key=lambda i: (find_index(list2, i), i))
+
+
+def main():
+ lists = json.loads(sys.argv[1])
+ result = relative_sort(*lists)
+
+ # Convert to a tuple to match expected output
+ print(tuple(result))
+
+
+if __name__ == '__main__':
+ main()