From 94fa0af4c1fe01c119e5f0081d35eaf02528cf30 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 12 May 2024 16:20:35 +1000 Subject: sgreen solutions to challenge 268 --- challenge-268/sgreen/python/ch-2.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 challenge-268/sgreen/python/ch-2.py (limited to 'challenge-268/sgreen/python/ch-2.py') diff --git a/challenge-268/sgreen/python/ch-2.py b/challenge-268/sgreen/python/ch-2.py new file mode 100755 index 0000000000..d0abc6522c --- /dev/null +++ b/challenge-268/sgreen/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys + + +def numbers_game(ints: list) -> list: + # Check we have an even number of items + if len(ints) % 2 != 0: + raise ValueError('Please provide an even number of items!') + + # Sort the list + ints = sorted(ints) + + for i in range(0, len(ints), 2): + # Switch the pairs of numbers around + ints[i], ints[i+1], = ints[i+1], ints[i] + + return ints + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = numbers_game(array) + print(tuple(result)) + + +if __name__ == '__main__': + main() -- cgit