Python

We did some exercises translating from Pyret to Python. See the lecture capture for details; here’s the Pyret and Python code.

# Program 1

fun add-shipping(subtotal :: Number) -> Number:
  doc: "add shipping amount to subtotal"
  if subtotal <= 50:
    subtotal + 10
  else if (subtotal > 50) and (subtotal <= 100):
    20
  else:
    30
  end
end

# Program 2

import lists as L
dairy = [list:milk,cheese,yogurt]

fun is-vegan(food :: String) -> Boolean:
  doc:determine whether food is vegan"
  not(L.member(dairy, food))
end

# Program 3

fun number-size(n :: Number) -> String:
  doc:approximate size of a number"
  if n > 1000:big else:small end
end


# Program 1

def add_shipping(subtotal: int) -> int:
    """add shipping amount to subtotal"""
    if subtotal <= 50:
        return subtotal + 10
    elif subtotal > 50 and subtotal <= 100:
        return subtotal + 20
    else:
        return subtotal + 30

# Program 2

dairy = [“milk”, “cheese”, “yogurt”]

def is_vegan(food: str) -> bool:
  “””determine whether food is vegan”””
  return food not in dairy

# Program 3

def number_size(n: int) -> str:
    “””approximate size of a number”””
    if n > 1000:
       return “big”
   return “small”