Skip to content

Instantly share code, notes, and snippets.

@acbart
Created August 14, 2022 15:54
Show Gist options
  • Select an option

  • Save acbart/58a18e200cb010eae574b5d7fc11362c to your computer and use it in GitHub Desktop.

Select an option

Save acbart/58a18e200cb010eae574b5d7fc11362c to your computer and use it in GitHub Desktop.
Mutable Data Code from CS1 Bakery Assignment
# Comment out each section's triple quotes to show or hide them!
"""
def double(number: int) -> int:
new_number = number * 2
return new_number
def double_mutate(number: int) -> int:
number = number * 2
return number
price = 5
new_price = double(price)
cost = 5
new_cost = double(cost)
"""
# Section 2
"""
def shout(text: str) -> str:
new_text = text.upper() + "!"
return new_text
def shout_mutate(text: str) -> str:
text = text.upper()
text += "!"
return text
word = "apple"
new_word = shout(word)
message = "hello"
new_message = shout_mutate(message)
"""
# Section 3
"""
from dataclasses import dataclass
@dataclass
class Lightbulb:
on: bool
color: str
def flip(bulb: Lightbulb) -> Lightbulb:
new_bulb = Lightbulb(not bulb.on, bulb.color)
return new_bulb
def flip_mutate(bulb: Lightbulb) -> Lightbulb:
bulb.on = not bulb.on
return bulb
hallway = Lightbulb(True, 'white')
new_hallway = flip(hallway)
kitchen = Lightbulb(True, 'white')
new_kitchen = flip_mutate(kitchen)
"""
# Section 4
"""
def add_third(numbers: list[int]) -> list[int]:
new_numbers = [numbers[0], numbers[1]]
new_numbers.append(0)
return new_numbers
def add_third_mutate(numbers: list[int]) -> list[int]:
numbers.append(0)
return numbers
values = [1, 2]
new_values = add_third(values)
integers = [1, 2]
new_integers = add_third_mutate(integers)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment