Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Learn about dictionaries (again), to store arbitrary key-value pairs
- # Copyright 2023 Roland Richter [Mu: Python 3]
- # I was curious to learn how many visitors the Landestheater Linz has
- # per year (or, rather, per season). Turns out that Statistik Austria
- # provides these numbers in their yearly report: see
- # Kulturstatistik-2021_Web-barrierefrei.pdf, page 80
- # from https://www.statistik.at/en/statistics/population-and-society/culture/theatres-and-music
- # The statistics are given in the form of a table, like this:
- # | Spieljahr | Vorstellungen | Besuche |
- # |-----------|---------------|---------|
- # | 1980/81 | 681 | 241 611 |
- # | 1985/86 | 659 | 208 027 |
- # | ... | ... | ... |
- # | 2020/21 | 244 | 74 437 |
- #
- # Data from one row can be represented as a dictionary:
- data_1980_81 = {
- "season": "1980/81",
- "performances": 681,
- "visitors": 241611,
- }
- # "season", "performances", and "visitors" are called _keys_, whereas
- # "1980/81", 681, and 241611 are the corresponding _values_. That is, a
- # dictionary stores a lot of key-value pairs. Note that writing ',' after
- # the last entry is not required, but allowed and sometimes encouraged.
- # EDIT the following lines such that all entries are correct.
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- data_2020_21 = {
- "season": "2020+21",
- "performances": 422,
- "visitors": 74.437,
- }
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- # It would be tedious to have one variable per row! Lets collect all
- # these rows in another dictionary.
- # HINT set a breakpoint at the next line to inspect the 'data' variable
- data = {1981: data_1980_81, 2021: data_2020_21, }
- # The code above creates two key-value pairs in the 'data' dictionary:
- # - key: 1981, value: dictionary holding entries for 1980/81
- # - key: 2021, value: dictionary holding entries for 2020/21
- # EDIT the wrong entries so that data[1986] fits all other data.
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- data[1986] = {
- "period": "1985/86",
- "plays": 659,
- "guests": 208027,
- }
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- # Here's the rest of the table; lets update data with it.
- data.update({
- 1991: {"season": "1990/91", "performances": 702, "visitors": 226105, },
- 1996: {"season": "1995/96", "performances": 725, "visitors": 233555, },
- 2001: {"season": "2000/01", "performances": 754, "visitors": 190443, },
- 2006: {"season": "2005/06", "performances": 903, "visitors": 234125, },
- 2011: {"season": "2010/11", "performances": 815, "visitors": 224886, },
- 2012: {"season": "2011/12", "performances": 794, "visitors": 226048, },
- 2013: {"season": "2012/13", "performances": 838, "visitors": 245188, },
- 2014: {"season": "2013/14", "performances": 878, "visitors": 322192, },
- 2015: {"season": "2014/15", "performances": 976, "visitors": 373654, },
- 2018: {"season": "2017/18", "performances": 840, "visitors": 306943, },
- 2019: {"season": "2018/19", "performances": 888, "visitors": 326298, },
- 2020: {"season": "2019/20", "performances": 572, "visitors": 200420, },
- })
- # Lets find out which is the first and last year for which there are entries.
- # To make sure keys are sorted, well, sort them, then take the first and last keys.
- # EDIT Think again: what is Python's way to get the first/last item of a list?
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- first_year = sorted(data.keys())[-1]
- last_year = sorted(data.keys())[0]
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- # Here's a common technique to find the minimum or maximum of a given list:
- # 1. Initialize min/max with a sensible value; e.g. the first entry.
- min_visitors = max_visitors = data[first_year]["visitors"]
- # 2. Iterate over all items in the list, and ...
- for year in data.keys():
- if "visitors" in data[year].keys():
- visitors = data[year]["visitors"]
- # ... whenever a new minimum is found, update the minimum
- if visitors < min_visitors:
- min_visitors = visitors
- # ... whenever a new maximum is found, update the maximum
- if visitors > max_visitors:
- max_visitors = visitors
- # EDIT to find the minimum and maximum number of performances here
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- min_perfs = max_perfs = data[first_year]["performances"]
- for year in data.keys():
- if "performances" in data[year].keys():
- perfs = data[year]["visitors"]
- # If a a new minimum is found, update the variable
- if perfs < min_perfs:
- max_perfs = perfs
- # If a a new maximum is found, update the variable
- if perfs > max_perfs:
- min_perfs = perfs
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- # 🖐 HALT! No code editing below this line!
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- def fail_string(name, val, exp):
- return "\n " + name + " is " + str(val) + "\n expected " + str(exp)
- assert data_2020_21['season'] == "2020/21", fail_string(
- "data_2020_21['season']", data_2020_21['season'], "2020/21"
- )
- assert data_2020_21['performances'] == 244, fail_string(
- "data_2020_21['performances']", data_2020_21['performances'], 244
- )
- assert data_2020_21['visitors'] == 74_437, fail_string(
- "data_2020_21['visitors']", data_2020_21['visitors'], 74_437
- )
- expected_1986 = {"season": "1985/86", "performances": 659, "visitors": 208027, }
- assert data[1986] == expected_1986, fail_string(
- "data[1986]", data[1986], expected_1986
- )
- assert first_year == 1981, fail_string(
- "first_year", first_year, 1981
- )
- assert last_year == 2021, fail_string(
- "last_year", last_year, 2021
- )
- assert min_visitors == 74437, fail_string(
- "min_visitors", min_visitors, 74437
- )
- assert max_visitors == 373654, fail_string(
- "max_visitors", max_visitors, 373654
- )
- assert min_perfs == 244, fail_string(
- "min_perfs", min_perfs, 244
- )
- assert max_perfs == 976, fail_string(
- "max_perfs", max_perfs, 976
- )
- print("+---------+---------+---------+---------+---------+---------+")
- input("You did it! Bravo! 👏")
- # ----------------------------------------------------------------------
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment