rric

portrait_

Dec 4th, 2023 (edited)
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.09 KB | None | 0 0
  1. # Learn about dictionaries (again), to store arbitrary key-value pairs
  2. # Copyright 2023 Roland Richter                           [Mu: Python 3]
  3.  
  4. # I was curious to learn how many visitors the Landestheater Linz has
  5. # per year (or, rather, per season). Turns out that Statistik Austria
  6. # provides these numbers in their yearly report: see
  7. # Kulturstatistik-2021_Web-barrierefrei.pdf, page 80
  8. # from https://www.statistik.at/en/statistics/population-and-society/culture/theatres-and-music
  9.  
  10. # The statistics are given in the form of a table, like this:
  11. # | Spieljahr | Vorstellungen | Besuche |
  12. # |-----------|---------------|---------|
  13. # | 1980/81   | 681           | 241 611 |
  14. # | 1985/86   | 659           | 208 027 |
  15. # | ...       | ...           | ...     |
  16. # | 2020/21   | 244           |  74 437 |
  17. #
  18. # Data from one row can be represented as a dictionary:
  19. data_1980_81 = {
  20.     "season": "1980/81",
  21.     "performances": 681,
  22.     "visitors": 241611,
  23. }
  24. # "season", "performances", and "visitors" are called _keys_, whereas
  25. # "1980/81", 681, and 241611 are the corresponding _values_. That is, a
  26. # dictionary stores a lot of key-value pairs. Note that writing ',' after
  27. # the last entry is not required, but allowed and sometimes encouraged.
  28.  
  29. # EDIT the following lines such that all entries are correct.
  30. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  31. data_2020_21 = {
  32.     "season": "2020+21",
  33.     "performances": 422,
  34.     "visitors": 74.437,
  35. }
  36. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  37.  
  38. # It would be tedious to have one variable per row! Lets collect all
  39. # these rows in another dictionary.
  40. # HINT set a breakpoint at the next line to inspect the 'data' variable
  41. data = {1981: data_1980_81, 2021: data_2020_21, }
  42. # The code above creates two key-value pairs in the 'data' dictionary:
  43. # - key: 1981, value: dictionary holding entries for 1980/81
  44. # - key: 2021, value: dictionary holding entries for 2020/21
  45.  
  46.  
  47. # EDIT the wrong entries so that data[1986] fits all other data.
  48. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  49. data[1986] = {
  50.     "period": "1985/86",
  51.     "plays": 659,
  52.     "guests": 208027,
  53. }
  54. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  55.  
  56. # Here's the rest of the table; lets update data with it.
  57. data.update({
  58.     1991: {"season": "1990/91", "performances": 702, "visitors": 226105, },
  59.     1996: {"season": "1995/96", "performances": 725, "visitors": 233555, },
  60.     2001: {"season": "2000/01", "performances": 754, "visitors": 190443, },
  61.     2006: {"season": "2005/06", "performances": 903, "visitors": 234125, },
  62.     2011: {"season": "2010/11", "performances": 815, "visitors": 224886, },
  63.     2012: {"season": "2011/12", "performances": 794, "visitors": 226048, },
  64.     2013: {"season": "2012/13", "performances": 838, "visitors": 245188, },
  65.     2014: {"season": "2013/14", "performances": 878, "visitors": 322192, },
  66.     2015: {"season": "2014/15", "performances": 976, "visitors": 373654, },
  67.     2018: {"season": "2017/18", "performances": 840, "visitors":  306943, },
  68.     2019: {"season": "2018/19", "performances": 888, "visitors":  326298, },
  69.     2020: {"season": "2019/20", "performances": 572, "visitors":  200420, },
  70. })
  71.  
  72. # Lets find out which is the first and last year for which there are entries.
  73. # To make sure keys are sorted, well, sort them, then take the first and last keys.
  74. # EDIT Think again: what is Python's way to get the first/last item of a list?
  75. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  76. first_year = sorted(data.keys())[-1]
  77. last_year = sorted(data.keys())[0]
  78. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  79.  
  80. # Here's a common technique to find the minimum or maximum of a given list:
  81. # 1. Initialize min/max with a sensible value; e.g. the first entry.
  82. min_visitors = max_visitors = data[first_year]["visitors"]
  83. # 2. Iterate over all items in the list, and ...
  84. for year in data.keys():
  85.     if "visitors" in data[year].keys():
  86.         visitors = data[year]["visitors"]
  87.  
  88.         # ... whenever a new minimum is found, update the minimum
  89.         if visitors < min_visitors:
  90.             min_visitors = visitors
  91.         # ... whenever a new maximum is found, update the maximum
  92.         if visitors > max_visitors:
  93.             max_visitors = visitors
  94.  
  95. # EDIT to find the minimum and maximum number of performances here
  96. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  97. min_perfs = max_perfs = data[first_year]["performances"]
  98.  
  99. for year in data.keys():
  100.     if "performances" in data[year].keys():
  101.         perfs = data[year]["visitors"]
  102.  
  103.         # If a a new minimum is found, update the variable
  104.         if perfs < min_perfs:
  105.             max_perfs = perfs
  106.         # If a a new maximum is found, update the variable
  107.         if perfs > max_perfs:
  108.             min_perfs = perfs
  109. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  110.  
  111.  
  112. # 🖐 HALT! No code editing below this line!
  113. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  114.  
  115. def fail_string(name, val, exp):
  116.     return "\n  " + name + " is " + str(val) + "\n  expected " + str(exp)
  117.  
  118. assert data_2020_21['season'] == "2020/21", fail_string(
  119.     "data_2020_21['season']", data_2020_21['season'], "2020/21"
  120. )
  121.  
  122. assert data_2020_21['performances'] == 244, fail_string(
  123.     "data_2020_21['performances']", data_2020_21['performances'], 244
  124. )
  125.  
  126. assert data_2020_21['visitors'] == 74_437, fail_string(
  127.     "data_2020_21['visitors']", data_2020_21['visitors'], 74_437
  128. )
  129.  
  130. expected_1986 = {"season": "1985/86", "performances": 659, "visitors": 208027, }
  131.  
  132. assert data[1986] == expected_1986, fail_string(
  133.     "data[1986]", data[1986], expected_1986
  134. )
  135.  
  136. assert first_year == 1981, fail_string(
  137.     "first_year", first_year, 1981
  138. )
  139.  
  140. assert last_year == 2021, fail_string(
  141.     "last_year", last_year, 2021
  142. )
  143.  
  144. assert min_visitors == 74437, fail_string(
  145.     "min_visitors", min_visitors, 74437
  146. )
  147.  
  148. assert max_visitors == 373654, fail_string(
  149.     "max_visitors", max_visitors, 373654
  150. )
  151.  
  152. assert min_perfs == 244, fail_string(
  153.     "min_perfs", min_perfs, 244
  154. )
  155.  
  156. assert max_perfs == 976, fail_string(
  157.     "max_perfs", max_perfs, 976
  158. )
  159.  
  160. print("+---------+---------+---------+---------+---------+---------+")
  161. input("You did it! Bravo! 👏")
  162.  
  163. # ----------------------------------------------------------------------
  164. # This program is free software: you can redistribute it and/or modify
  165. # it under the terms of the GNU General Public License as published by
  166. # the Free Software Foundation, either version 3 of the License, or
  167. # (at your option) any later version.
  168. #
  169. # This program is distributed in the hope that it will be useful,
  170. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  171. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  172. # GNU General Public License for more details.
  173. #
  174. # You should have received a copy of the GNU General Public License
  175. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  176.  
Advertisement
Add Comment
Please, Sign In to add comment