Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.

Link zu der Vergleichsansicht

Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung
Nächste Überarbeitung
Vorherige Überarbeitung
gf_informatik:daten:processing [2023-05-26 14:27] hofgf_informatik:daten:processing [2024-06-10 10:45] (aktuell) hof
Zeile 6: Zeile 6:
  
 Wieviel ist viel? 1000?  Wieviel ist viel? 1000? 
 +
 #### Aufgabe 1 #### Aufgabe 1
  
Zeile 73: Zeile 74:
   * Mit `print(values[1])` wird der Kanton (die zweite Spalte) ausgegeben.   * Mit `print(values[1])` wird der Kanton (die zweite Spalte) ausgegeben.
   * Wieviele Gemeinden gibt es im Kanton Thurgau? Wieviele im Kanton Bern?   * Wieviele Gemeinden gibt es im Kanton Thurgau? Wieviele im Kanton Bern?
 +
 +<nodisp 1>
 +++++Lösung:|
 +<code python>
 +with open('gemeinden.csv', 'r') as infile:
 +    be = 0
 +    tg = 0
 +    for line in infile:
 +        tokens = line.split(',')
 +        canton = tokens[1]
 +        if canton == 'TG':
 +            tg = tg + 1
 +        elif canton == 'BE':
 +            be = be + 1
 +    print("Bern", be)
 +    print("Thurgau", tg)
 +</code>
 +++++
 +</nodisp>
  
  
Zeile 114: Zeile 134:
 def find_smallest_population(): def find_smallest_population():
     with open('gemeinden.csv') as towns:     with open('gemeinden.csv') as towns:
 +        # Search for the smallest, so start out with a value larger than any expected.
 +        min_pop = 1000000
 +        town = None
 +        for line in towns:
 +            cells = line.split(',')
 +            try:
 +                # Population is in the third column, and it is an integer.
 +                population = int(cells[2])
 +                if population < min_pop:
 +                    # We found a town smaller than the currently known smallest.
 +                    min_pop = population
 +                    town = cells[0]
 +            except ValueError:
 +                pass
 +        return town, min_pop
 +
 +print("Smallest town: ", find_smallest_population())
 +</code>
 +++++
 +</nodisp>
 +
 +<nodisp 2>
 +++++Code:|
 +<code python>
 +def find_smallest_population():
 +    with open('gemeinden.csv') as towns:
 +        # Search for the smallest, so start out with a value larger than any expected.
         min_pop = 1000000         min_pop = 1000000
         town = None         town = None
Zeile 119: Zeile 166:
             cells = line.split(',')             cells = line.split(',')
             try:             try:
 +                # Population is in the third column, and it is an integer.
                 population = int(cells[2])                 population = int(cells[2])
                 if population < min_pop:                 if population < min_pop:
 +                    # We found a town smaller than the currently known smallest.
                     min_pop = population                     min_pop = population
                     town = cells[0]                     town = cells[0]
Zeile 131: Zeile 180:
 def find_largest_area(): def find_largest_area():
     with open('gemeinden.csv') as towns:     with open('gemeinden.csv') as towns:
 +        # We search for the largest, so start with a small value.
         max_area = 0         max_area = 0
         town = None         town = None
Zeile 136: Zeile 186:
             cells = line.split(',')             cells = line.split(',')
             try:             try:
 +                # Area is in the fourth column, and it is a floating point number.
                 area = float(cells[3])                 area = float(cells[3])
                 if area > max_area:                 if area > max_area:
 +                    # Found a town with larger area than the largest known so far.
                     max_area = area                     max_area = area
                     town = cells[0]                     town = cells[0]
  • gf_informatik/daten/processing.1685111254.txt.gz
  • Zuletzt geändert: 2023-05-26 14:27
  • von hof