Skip to content

Instantly share code, notes, and snippets.

@joefromct
Created July 29, 2019 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joefromct/489281b9094b38d46bbfa2afbc2b5486 to your computer and use it in GitHub Desktop.
Save joefromct/489281b9094b38d46bbfa2afbc2b5486 to your computer and use it in GitHub Desktop.
from pprint import pprint as pp
def reached_top_of_stairs(stairway_height, step_tuple):
return sum(step_tuple) == stairway_height
def compute_max_steps(stairway_height, step_options):
non_zero = [f for f in step_options if f != 0]
min_step = min(non_zero)
return int(stairway_height / min_step)
def smallest_possible_steps(stairway_height, step_options):
max_steps = compute_max_steps(stairway_height, step_options)
min_value = min(step_options)
return [min_value,] * max_steps
def largest_possible_steps(stairway_height, step_options):
max_steps = compute_max_steps(stairway_height, step_options)
max_value = max(step_options)
return [max_value,] * max_steps
def inc_digit(step_options, d):
cur_index = step_options.index(d)
next_index = 1 + cur_index
try:
return step_options[next_index]
except IndexError:
return None
def inc_tuple(stairway_height, step_options, v):
v_len = len(v)
inc_value = inc_digit(step_options, v[-1])
min_digit = min(step_options)
largest_steps = largest_possible_steps(stairway_height, step_options)
if v == largest_steps:
return None
if inc_value:
v[v_len -1] = inc_value
return v
else:
drop_last = v[:-1]
ret = inc_tuple(stairway_height, step_options, drop_last )
ret.append(min_digit)
return ret
def generate_all_possibilities(stairway_height, step_options):
cur_tuple = smallest_possible_steps(stairway_height, step_options)
largest_steps = largest_possible_steps(stairway_height, step_options)
all_possibilities = []
while True:
all_possibilities.append(cur_tuple)
if cur_tuple == largest_steps:
break
else:
cur_tuple = inc_tuple(stairway_height, step_options, cur_tuple)
return all_possibilities
step_options = [0, 1, 2]
stairway_height = 3
pp(generate_all_possibilities(stairway_height, step_options))
# ???? horray python.
# In [6]: [[0, 0, 2],
# [0, 0, 2],
# [0, 0, 2],
# [0, 1, 2],
# [0, 1, 2],
# [0, 1, 2],
# [0, 2, 2],
# [0, 2, 2],
# [0, 2, 2],
# [1, 0, 2],
# [1, 0, 2],
# [1, 0, 2],
# [1, 1, 2],
# [1, 1, 2],
# [1, 1, 2],
# [1, 2, 2],
# [1, 2, 2],
# [1, 2, 2],
# [2, 0, 2],
# [2, 0, 2],
# [2, 0, 2],
# [2, 1, 2],
# [2, 1, 2],
# [2, 1, 2],
# [2, 2, 2],
# [2, 2, 2],
# [2, 2, 2]]
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment