0

I have two numbers: n and sp. I want to create a pattern of list elements with sp elements that rotate like binary digits up to n. To elaborate more on the second part, here is an example:

n = 2, sp = 3
[0, 0, 0]
[0, 0, 1]
[0, 1, 0]
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
[1, 1, 0]
[1, 1, 1]

Similarly:

n = 3, sp = 3
[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 1, 0]
[0, 2, 0]
[0, 1, 1]
[0, 1, 2]
[0, 2, 1]
[0, 2, 2]
[1, 0, 0]
[1, 0, 1]
[1, 0, 2]
[1, 1, 0]
[1, 2, 0]
[1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[1, 2, 2]
[2, 0, 0]
[2, 0, 1]
[2, 0, 2]
[2, 1, 0]
[2, 2, 0]
[2, 1, 1]
[2, 1, 2]
[2, 2, 1]
[2, 2, 2]

I wish to maintain the order that I have given in the examples, that is the LSB is assigned first, then the next bit, and so on...

I could not think of any tactic to solve such a problem. All I could figure out is to use sp to create a temporary list of size sp, for instance, if sp == 3, the temp_list can be [0 0 0]. Then we iterate in a manner so that we get the pattern.

There is no constraint on the complexity of the algorithm, although a shorter algorithm would be very great.

2
  • 2
    Your ideas are a good start! Show us your code attempts and we will surely be able to help you get your problem solved. :) Commented Oct 11, 2019 at 7:46
  • All I could do was create 2 nested loops, and start from the LSB, where we first set the LSB to 1, then to 2, then to 0. After that, we moved to the next bit and incremented it and repeated the innermost loop again. It was working, but it would fail for a bigger sp value (I would need more loops for that). And that is why I was/am vary of my approach. :D Commented Oct 11, 2019 at 8:06

2 Answers 2

1

One simple way of doing it is:

def get_patterns(n, sp):
    ans = []

    def _get_patterns(so_far):
        if len(so_far) == sp:
            ans.append(so_far[:])
            return

        for i in range(n):
            so_far.append(i)
            _get_patterns(so_far)
            so_far.pop()

    _get_patterns([])
    return ans

n = 3
sp = 3

assert get_patterns(n, sp) == sorted([
    [0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], 
    [0, 2, 0], [0, 1, 1], [0, 1, 2], [0, 2, 1], [0, 2, 2],

    [1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1, 0],
    [1, 2, 0], [1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 2, 2],

    [2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1, 0], 
    [2, 2, 0], [2, 1, 1], [2, 1, 2], [2, 2, 1], [2, 2, 2],
])
Sign up to request clarification or add additional context in comments.

2 Comments

Whoa... Thanks for this method. I did now know that you can define a function in a function. Is it like an "anonymous" function? Anyways, it does the job. Thanks a lot! :D
Yes, Python allows a function inside another function, and it is called nested functions.
1

I wrote this

n = 4 # base
sp = 2 # length

def fix(num, base):
    for j in range(len(num)):
        if num[j] == base:
            num[j] = 0
            num[j+1] += 1
    return num

num= [ 0 for _ in range(sp)]
print(num)
for i in range(n**sp -1):
    num[0] += 1
    num= fix(num, n)
    print(num[::-1])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.