aboutsummaryrefslogtreecommitdiff
path: root/challenge-055/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-09-16 09:41:35 +0200
committerLubos Kolouch <lubos@kolouch.net>2024-09-16 09:41:35 +0200
commit65b9d6b25e0a823ca7ab6d15744ff98eb3697471 (patch)
treefcbcdebd50e3e146dfecf519701ec04b191053eb /challenge-055/paulo-custodio/python/ch-2.py
parentbd1fe7ae50ca42bda58c134b9edfdc287fb3f386 (diff)
parent68e321dd32a834f54b55d5e8924f04358e41cf1f (diff)
downloadperlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.tar.gz
perlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.tar.bz2
perlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-055/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-055/paulo-custodio/python/ch-2.py40
1 files changed, 40 insertions, 0 deletions
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)