Skip to content

Commit

Permalink
Merge pull request #25 from eelcodijkstra/main
Browse files Browse the repository at this point in the history
Programmatekst bij sessie Traceren van Tim Steenvoorden
  • Loading branch information
eelcodijkstra authored Apr 19, 2024
2 parents 863e111 + 4c47182 commit 6fc3e9b
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions inf/traceren.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,80 @@ programmeermodellen. Zijn onderzoeksinteresses liggen in het ontwerpen,
implementeren en verifiëren van programmeertalen. Daarnaast heeft hij de
afgelopen jaren bijgedragen aan de lesmodules algoritmiek en cognitive
computing voor het nieuwe informatica curriculum.

De programmacode bij deze sessie:

```Python
def section(name):
print()
print("====", name, "example", "====")


section("Product")

"https://pastefy.app/kflGA7c2"
inventory = [
{"name": "piano", "quantity": 2, "price": 5000},
{"name": "tv", "quantity": 20, "price": 1000},
{"name": "teddy", "quantity": 54, "price": 25},
{"name": "pencil", "quantity": 120, "price": 1},
]

print(inventory[2])


section("Total")

def stock_total(products):
som = 0
i = 0
while i < len(products):
som += products[i]["quantity"]
i += 1
return som

def price_total(products):
som = 0
i = 0
while i < len(products):
som += products[i]["price"]
i += 1
return som

def price_total_rec(products):
match products:
case []:
return 0
case first, *rest:
return first["price"] + price_total_rec(rest)

print(stock_total(inventory))
print(price_total(inventory))
print(price_total_rec(inventory))


section("Average")

def average_price(products):
return price_total(products) / len(products)

print(average_price(inventory))


section("Palindrome")

def palindrome(chars):
match chars:
case [] | [_]:
return True
case first, *middle, last:
return first == last and palindrome(middle)

print(palindrome(list("")))
print(palindrome(list("a")))
print(palindrome(list("rar")))
print(palindrome(list("raar")))
print(palindrome(list("lepel")))
print(palindrome(list("not")))
print(palindrome(list("expensive")))
```

0 comments on commit 6fc3e9b

Please sign in to comment.