๐Ÿ’ป Activity Rube Goldberg Machine: Modular Functions with *args and **kwargs

# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment

responses = initialize_assignment("10_rube_goldberg", "week_5", "lecture-not-graded", assignment_points = 21.0, assignment_tag = 'week5-lecture-not-graded')

# Initialize Otter
import otter
grader = otter.Notebook("10_rube_goldberg.ipynb")

๐Ÿ’ป Activity Rube Goldberg Machine: Modular Functions with *args and **kwargs#

Question 1 (Points: 21.0): Rube Goldberg Machine#

Rube Goldberg Machine

Instructions:#

You are designing a Rube Goldberg machine simulator where multiple functions interact in a sequence to achieve a simple goal in an overly complex way.

Your task is to define multiple modular functions that take flexible inputs using *args and **kwargs.

Requirements:#

  1. Define a Function start_machine(*args, **kwargs)

    • Calls other functions based on *args.

    • Passes metadata (like speed=, material=) using **kwargs.

  2. Define Helper Functions for Components:

    • Each function should accept flexible arguments and pass data to the next function.

    • Example:

      def lever(action, *args, **kwargs):
          return f"Lever {action} โ†’ {next_step(*args, **kwargs)}"
      
  3. Ensure Function Interaction:

    • Functions should call one another, creating a step-by-step simulation.

    • The final function should return "Goal Achieved!".

  4. Example Execution:

start_machine("lever", "pulley", "dominoes", speed="fast", material="wood")

Expected Output:

Lever triggered โ†’ Pulley activated โ†’ Dominoes toppled โ†’ Goal Achieved!

Helpful Information:#

  • Use *args to dynamically chain components.

  • Use **kwargs to pass machine properties like speed or material.

  • The machine should always end with "Goal Achieved!".

# Function Definitions
# A Rube Goldberg machine is a contraption that is deliberately over-engineered to perform a simple task.


# Define a function lever that takes an argument and returns a string that says "Lever triggered โ†’ [next_step]".
...


# Define a function pulley that takes an action as an argument and returns a string that says "Pulley [action] โ†’ [next_step]".
...


# Define a function dominoes that takes an action as an argument and returns a string that says "Dominoes [action] โ†’ [next_step]".
...


# Define a function next_step that takes a variable number of arguments and returns a string that says "Goal Achieved!" if no arguments are passed.
# If arguments are passed, the function should determine the next step in the machine based on the first argument and call the corresponding function.
# The function should return "Unknown step!" if the first argument does not match any of the defined functions.
def next_step(*args, **kwargs):
    """Determines the next step in the machine."""

    # if no arguments are passed, return "Goal Achieved!"
    ...

    # From the first argument, determine the next component, save to the `next_component`` variable, and save the remaining arguments to the `remaining_steps` variable
    ...

    # Create a dictionary that maps the next component to the corresponding function
    # Save this dictionary to the `component_map` variable
    ...

    # If the next component is in the component map, call the corresponding function with the remaining steps and return the result
    # If the next component is not in the component map, return "Unknown step!"
    # Recall, you can use the in operator to check if a key is in a dictionary
    ...


# Define a function start_machine that takes a variable number of arguments and keyword arguments and initiates the Rube Goldberg machine.
# The function should call the next_step function with the provided arguments and return the result.
# This is the main function call that initiates the machine.
...

# Example execution
print(start_machine("lever", "pulley", "dominoes", speed="fast", material="wood"))
grader.check("rube-goldberg-modularity")

Submitting Assignment#

Please run the following block of code using shift + enter to submit your assignment, you should see your score.

from pykubegrader.submit.submit_assignment import submit_assignment

submit_assignment("week5-lecture-not-graded", "10_rube_goldberg")