Skip to content

Instantly share code, notes, and snippets.

@andreburto
Last active October 22, 2017 00:00
Show Gist options
  • Save andreburto/14fa9fd418d13dd99965eef9a774be1c to your computer and use it in GitHub Desktop.
Save andreburto/14fa9fd418d13dd99965eef9a774be1c to your computer and use it in GitHub Desktop.
Calculate the ratio from System bricks/plates to Technic holes.
from __future__ import print_function
from sys import exit
from math import floor
from nose.tools import assert_equals
__author__ = "Andy Burton"
PLATES_IN_BRICK = 3
PLATE_TO_HOLE_RATIO_DICT = {
'plate': 5,
'hole': 2,
}
PLATE_TO_HOLE_MIN_DICT = {
'plate': 2,
'hole': 1,
}
def bricks_and_plates(plates):
""" Convert a number of plates into the total number of bricks and plates remainder. """
if plates < PLATES_IN_BRICK:
return 0, plates
else:
bricks = int(floor(plates / PLATES_IN_BRICK))
plates_remainder = plates % PLATES_IN_BRICK
return bricks, plates_remainder
def increase_numbers(current_numbers_dict):
""" Increment by one ratio set. """
new_hole_amt = current_numbers_dict['hole'] + PLATE_TO_HOLE_RATIO_DICT['hole']
new_plate_amt = current_numbers_dict['plate'] + PLATE_TO_HOLE_RATIO_DICT['plate']
return {
'hole': new_hole_amt,
'plate': new_plate_amt,
}
def decrease_numbers(current_numbers_dict):
""" Decrease by one ratio set. Cannot go below the minimum ratio. """
new_hole_amt = current_numbers_dict['hole'] - PLATE_TO_HOLE_RATIO_DICT['hole']
new_plate_amt = current_numbers_dict['plate'] - PLATE_TO_HOLE_RATIO_DICT['plate']
if new_hole_amt < PLATE_TO_HOLE_MIN_DICT['hole']:
return PLATE_TO_HOLE_MIN_DICT
else:
return {
'hole': new_hole_amt,
'plate': new_plate_amt,
}
def display_text(current_numbers_dict):
""" Display according to what types of pieces you need. """
bricks, plates = bricks_and_plates(current_numbers_dict['plate'])
if bricks > 0:
return "The smalled ratio you can have is {0} bricks and {1} plates to {2} Technic holes.".\
format(bricks, plates, current_numbers_dict['hole'])
else:
return "The smalled ratio you can have is {0} plates to {1} Technic holes.". \
format(plates, current_numbers_dict['hole'])
def byebye(*args, **kwargs):
print("Good bye!")
exit()
def run_tests():
""" Run tests when starting up to verify everyting works as expected. """
try:
# Test that the three plates always equals one brick plus the remaider of plates
assert_equals(bricks_and_plates(1), (0, 1))
assert_equals(bricks_and_plates(3), (1, 0))
assert_equals(bricks_and_plates(4), (1, 1))
# Test that decreasing the ratio does not go below the base ratio
test_ratio_dict = PLATE_TO_HOLE_MIN_DICT
assert_equals(decrease_numbers(test_ratio_dict), PLATE_TO_HOLE_MIN_DICT)
# Test that increasing the ratio returns what we expect
# Smallest set
assert_equals(test_ratio_dict, PLATE_TO_HOLE_MIN_DICT)
# Up one
test_ratio_dict = increase_numbers(test_ratio_dict)
assert_equals(test_ratio_dict, {'hole': 3, 'plate': 7})
# One more
test_ratio_dict = increase_numbers(test_ratio_dict)
assert_equals(test_ratio_dict, {'hole': 5, 'plate': 12})
except Exception as ex:
raise ex
# These are the available actions.
ACTIONS_LIST = {
'I': increase_numbers,
'D': decrease_numbers,
'E': byebye,
}
if __name__ == '__main__':
input_text = "What is your choice? (I)ncrease, (D)ecrease, (E)xit: "
current_ratio_dict = PLATE_TO_HOLE_MIN_DICT
run_tests()
# Main loop to allow user to view ratio amounts.
# TODO: Increase or decrease by amount. Create parser to look for second parameter.
while True:
print(display_text(current_ratio_dict))
action_to_take = raw_input(input_text).upper()
action_func = ACTIONS_LIST[action_to_take if ACTIONS_LIST.has_key(action_to_take) else 'E']
current_ratio_dict = action_func(current_ratio_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment