From 0d07992c8e9958afc2c941ca0b5f034bd426cdba Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 9 Sep 2024 22:10:09 +0100 Subject: Add Python solution to challenge 055 --- challenge-055/paulo-custodio/python/ch-2.py | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 challenge-055/paulo-custodio/python/ch-2.py (limited to 'challenge-055/paulo-custodio/python/ch-2.py') diff --git a/challenge-055/paulo-custodio/python/ch-2.py b/challenge-055/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..ab2ebac891 --- /dev/null +++ b/challenge-055/paulo-custodio/python/ch-2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# Challenge 055 +# +# TASK #2 +# Wave Array +# Any array N of non-unique, unsorted integers can be arranged into a wave-like +# array such that n1 = n2 = n3 = n4 = n5 and so on. +# +# For example, given the array [1, 2, 3, 4], possible wave arrays include +# [2, 1, 4, 3] or [4, 1, 3, 2], since 2 = 1 = 4 = 3 and 4 = 1 = 3 = 2. +# This is not a complete list. +# +# Write a script to print all possible wave arrays for an integer array N of +# arbitrary length. +# +# Notes: +# When considering N of any length, note that the first element is always +# greater than or equal to the second, and then the =, =, =, … sequence +# alternates until the end of the array. + +import sys + +def show_waves(wave, next): + if len(next) == 0: + print(" ".join([str(x) for x in wave])) + elif len(wave) == 0: + for i in range(len(next)): + show_waves(wave+[next[i]], next[:i]+next[i+1:]) + elif len(wave) % 2 == 1: # going down + for i in range(len(next)): + if wave[-1] >= next[i]: + show_waves(wave+[next[i]], next[:i]+next[i+1:]) + else: # going up + for i in range(len(next)): + if wave[-1] <= next[i]: + show_waves(wave+[next[i]], next[:i]+next[i+1:]) + +n = [int(x) for x in sys.argv[1:]] +show_waves([], n) -- cgit