aboutsummaryrefslogtreecommitdiff
path: root/challenge-055/paulo-custodio/python/ch-2.py
blob: ab2ebac8915864935fca5ddad09cb8c41e8e2e38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)