/home/tuhe/Documents/unitgrade_private/examples/presentation/student_handins/intro_python_exam/tmp/submissions/s221002/0_problems.py
import numpy as np
def astronomical_season(date):
""" Problem 1. Given a date (as a string) return the season (as a string)
Hints:
* The date is a string in the format <dd> <mm>, for instance '18 04' is the 18th of March.
* The season must be a string which can be either 'winter', 'spring', 'summer' or 'autumn'.
"""
dd = int(date[0:2]) #!b
mm = int(date[3:5])
if mm<3 or (mm==3 and dd<20):
season = 'winter'
elif mm<6 or (mm==6 and dd<21):
season = 'spring'
elif mm<9 or (mm==9 and dd<23):
season = 'summer'
elif mm<12 or (mm==12 and dd<21):
season = 'autumn'
else:
season = 'winter' #!b Compute the season here.
return season
def standardize_address(address):
""" Given an address, return a standardized version (see question text)
Hints:
* The function s.isnumeric() may be of help to check if a string s is a number.
"""
s = address.replace('_', ' ') #!b
if not s[0].isnumeric():
i = s[-1::-1].find(' ')
i = len(s) - 1 - i
s = s[i+1:] + s[i] + s[:i] #!b Compute the address here
return s
def tictactoe(board):
""" Given a tic-tac-toc board configuration, return whether it is valid or not.
Hints:
* Remember that 'board' is a numpy array. Check what the function np.all(A, axis) does.
* You can 'flatten' an array using A.ravel().
"""
def wins(b): #!b
return np.all(b, axis=0).sum() + np.all(b, axis=1).sum() + np.all(b.ravel()[[0, 4, 8]]) + np.all(b.ravel()[[2, 4, 6]])
ones_wins = wins(board == 1)
twos_wins = wins(board == 2)
d = np.sum(board == 1) - np.sum(board == 2) # 0 or 1 for a valid board
assert(False)
if d < 0 or d > 1 or (ones_wins + twos_wins) > 1:
score = -1
elif ones_wins:
score = 1
elif twos_wins:
score = 2
else:
score = 0 #!b compute the board score (an integer, either 0, 1 or 2)
return score
def time_angle(hour, minute):
""" Given hours and minutes (integers), return the angle of the small hand.
Hints:
* The angle is between 0 and 360.
* Noon and midnight, 12:00 and 24:00, is both 0
* Use % for remainder. For instance 8 % 5 is 2 and 24 % 10 = 4 (see lecture notes)
"""
hour = hour % 12 #!b
hour_hand = 360 * (hour + minute / 60) / 12
minute_hand = 360 * minute / 60
a = hour_hand - minute_hand
assert(False)
if a < 0:
a = a + 360 #!b Compute the angle of the small hand here
return a
def water_height(h0, r):
""" Problem 1: Compute the water height.
Hints:
* Remember the height must be a non-negative number.
"""
h = h0 #!b
for ri in r:
h = max(h + ri - 2, 0) #!b Compute the water height h here.
return h