Commit 3ed49a73 authored by Swicech's avatar Swicech
Browse files

updated solutions

parent b178ca21
%% Cell type:markdown id: tags:
a)
100110101
b)
```
0 This number is divisible by 4
2
4 This number is divisible by 4
6
8 This number is divisible by 4
```
c)
16
d)
8
e)
```
*
**
***
****
*****
```
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
n = int(input("How many iterations? "))
for i in range(n):
adj = input("Describe yourself with an adjective: ")
print("Hah, du", adj + "!? Jeg er mye", adj + "ere!")
print("Goodbye!")
```
%% Cell type:markdown id: tags:
b)
%% Cell type:code id: tags:
``` python
print("Press enter without inputting an answer to exit.")
adj = input("Describe yourself with an adjective: ")
while adj != "":
print("Hah, you", adj + "!? I am", adj + "re!")
adj = input("Describe yourself with an adjective: ")
print("Goodbye!")
```
%% Cell type:markdown id: tags:
c)
%% Cell type:code id: tags:
``` python
letter = 42
while letter > 0:
print("You have", letter, "letters remaining.")
adj = input("Describe yourself with an adjective: ")
print("Hah, you", adj + "!? I am", adj + "re!")
letter = letter - len(adj)
print("Goodbye!")
```
%% Cell type:markdown id: tags:
d)
%% Cell type:code id: tags:
``` python
print("Odd numbers from 1 to 20:")
for number in range(1, 20, 2):
print(number, end = " ")
print()
print("Every third number from 12 to 25:")
for number in range(12, 25, 3):
print(number, end = " ")
print()
print("Every fifth number from 20 to 81:")
for number in range(20, 81, 5):
print(number, end = " ")
print()
print("The number sequence 48, 56, 64, 72, 80")
for number in range(48, 81, 8):
print(number, end = " ")
print()
print("Count backwards from 100 to 80, with an interval of -3, eg. 100, 97, ...:")
for number in range(100, 80, -3):
print(number, end = " ")
print()
```
%% Cell type:markdown id: tags:
e)
%% Cell type:code id: tags:
``` python
for x in range(1,6):
print(x)
```
%% Cell type:markdown id: tags:
f)
%% Cell type:code id: tags:
``` python
for x in range(15, 0, -1):
print(x)
```
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
am_stud = int(input("How many students? "))
am_courses = int(input("How many courses? "))
for x in range(1, ant_stud+1):
for y in range(1, ant_emner+1):
print("Stud", x, "loves Course", y, end=" ; ")
print()
```
%% Cell type:markdown id: tags:
EXTRA-TASK (You can choose other courses, names, ....):
%% Cell type:code id: tags:
``` python
from random import random
stud = ['Ada', 'Grace', 'Guido', 'Linus', 'Runa']
courses = ['Ex.phil.', 'Ex.fac.', 'Matte', 'ITGK']
for st in stud:
print(st, end = " ")
for cours in courses:
num = random()
if num > 0.9:
verb = 'loves'
elif num > 0.6:
verb = 'digs'
elif num > 0.3:
verb = 'likes'
else:
verb = '"likes"'
print(verb, cours, end = "; ")
print()
```
%% Cell type:markdown id: tags:
b)
The "straight-forward" solution to this is a double loop with print() in the inner loop. This program will be very slow:
%% Cell type:code id: tags:
``` python
for x in range(24):
for y in range(60):
print(x + ":" + y)
```
%% Cell type:markdown id: tags:
The reason for this is that printout on the screen is time-consuming. If you want a faster program will it be smart to print fewer times and write bigger chunks of texts each time. You can also gather everything and print in the end:
%% Cell type:code id: tags:
``` python
out_string = ""
for x in range(24):
for y in range(60):
out_string += str(x) + ":" + str(y) + "\n"
# "\n" is new line
print(out_string)
```
%% Cell type:markdown id: tags:
This will be much faster than the first version.
But if you want it to be slower, that a new time should be shown when a minute has passed can we use the first code with a little modification:
%% Cell type:code id: tags:
``` python
import time
for x in range(24):
for y in range(60):
print(x + ":" + y)
time.sleep(60)
```
%% Cell type:markdown id: tags:
You can also google and find other functions in the time-module that can be used to get the program to start with the current time instead of at 0:0.
c)
%% Cell type:code id: tags:
``` python
for x in range(1, 11):
for y in range(1, 11):
print(x*y)
#nicer printout:
for x in range(1, 11):
for y in range(1, 11):
print(x*y, end=' ')
print()
```