more on decomposition (slides)

CSC 110 more on decomposition

Class activity one

Read the code, discuss and answer the question in 1-2 sentences using natural language:

  • What problem does the code solve?

Rubik’s Cubes

cube_len = int(input("Cube length? "))
box_w = int(input("Box width? "))
box_h = int(input("Box height? "))
box_l = int(input("Box length? "))

fit_w = box_w // cube_len
fit_h = box_h // cube_len
fit_l = box_l // cube_len

print(str(fit_w) + " Rubik's cubes will fit width-wise.")
print(str(fit_h) + " Rubik's cubes will fit height-wise.")
print(str(fit_l) + " Rubik's cubes will fit length-wise.")

res = fit_w * fit_h * fit_l
print(str(res)+" Rubik's cubes will fit in that container.")

Activity two

Read the code and write the questions:

  • which lines of code are similar?

  • which lines of code are unique?

  • which lines can be grouped together into a function? what does the function do?

Rubik’s Cubes

cube_len = int(input("Cube length? "))
box_w = int(input("Box width? ")) 
box_h = int(input("Box height? "))
box_l = int(input("Box length? "))

fit_w = box_w // cube_len
fit_h = box_h // cube_len
fit_l = box_l // cube_len

print(str(fit_w) + " Rubik's cubes will fit width-wise.")
print(str(fit_h) + " Rubik's cubes will fit height-wise.")
print(str(fit_l) + " Rubik's cubes will fit length-wise.")

res = fit_w * fit_h * fit_l
print(str(res)+" Rubik's cubes will fit in that container.")

Activity two – answers

Read the code and write the questions:

  • which lines of code are similar?

    • line 2-4, line 6-8, line 10-12
  • which lines of code are unique?

    • line 1, line 14-15

Activity two – continued

Which lines can be grouped together into a function?

  • line 2, 6, 10:
box_w = int(input("Box width? ")) 

fit_w = box_w // cube_len

print(str(fit_w) + " Rubik's cubes will fit width-wise.")

What are the parameters of this function? In other words, what information must be known beforehand?

Activity two – answers continued

Given the length of a cube and the name of a box side:

  • It prompts you to the enter the length of the side

  • It calculates the number of cube that can fit in the side

  • It print the message and return the number.

Write a function

Write function get_fit that takes the length of a cube (integer) and the name of a side (string)

  • It prompts you to the enter the length of the side

  • It calculates the number of cube that can fit in the side

  • It print the message and return the number.

Name your file as cubes.py and submit to Gradescope.

def main():
    cube_len = int(input("Cube length? "))
    fit_w = get_fit(cube_len, 'width')
    fit_h = get_fit(cube_len, 'height')
    fit_l = get_fit(cube_len, 'length')
    result = fit_w * fit_h * fit_l
    print(str(result) + " Rubik's cubes will fit in that container.")

Write a function – answer

def get_fit(cube, side):
    large = int(input("Box {}? ".format(side)))
    fit = large // cube
    print("{} Rubik's cubes will fit {}-wise.".format(fit, side))
    return fit

def main():
    cube_len = int(input("Cube length? "))
    fit_w = get_fit(cube_len, 'width')
    fit_h = get_fit(cube_len, 'height')
    fit_l = get_fit(cube_len, 'length')
    result = fit_w * fit_h * fit_l
    print(str(result) + " Rubik's cubes will fit in that container.")

main()