Using a random list of true false values I want to make a rule based code

I have a list of true false values, starting at 000 if the next value is true the code jumps to 100 but if its false it only jumps 10.

This is for a level code where some are primary levels and some are secondary. Primary get the hundredth marker and secondary have the tens. Each hundredth resets the tens value.

the code would look like this
true 000
true 100
false 110
false 120
true 200
false 210
true 300
false 310
true 400
true 500

I think this is what you mean?

With codes and lists like this, I normally find Python a lot easier (and quicker)

def generate_level_code(input_list):
    code_list = []
    code = 0
    for value in input_list:
        if value:
            code = (code // 100 + 1) * 100
            code_list.append(str(code).zfill(3))
        else:
            code += 10
            code_list.append(str(code).zfill(3))
    return code_list

# Example input list
input_list = IN[0]

OUT = generate_level_code(input_list)

I’ve assumed you wanted the output as a 3 character long string

2 Likes

hi
too late as usual

import sys
import math

stock=[0]
lb=IN[0]
for i in range(1,len(lb)):
    if lb[i]:
        stock.append(math.floor(stock[i-1]/100+1)*100)
    else:
        stock.append(stock[i-1]+10)
stock[0]='000'
OUT = stock

cordially
christian.stan

1 Like

If first item is true value is ‘000’ and I would assume false is ‘010’
I think the best way to do this is to prime the code_list result off the first item and go from there

input_list = [True, False, True, False, False, True]

# prime list
if input_list[0]:
    code_list = [0]
else:
    code_list = [10]

for i in input_list[1:]:
    if i:
        code_list.append((code_list[-1] // 100 + 1) * 100)
    else:
        code_list.append(code_list[-1] + 10)

code_list = list(map(lambda n: str(n).zfill(3), code_list))
2 Likes