Skip to content

Commit 0b26c8a

Browse files
author
James Collier
committed
Can I evaluate plots?
1 parent 8870664 commit 0b26c8a

7 files changed

Lines changed: 1566 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"description": {
3+
"names": {
4+
"en": "Bar Plot"
5+
}
6+
},
7+
"labels": [
8+
"real-world programming"
9+
],
10+
"internals": {
11+
"token": "G4iAJA8szv6ihdYCfAXzZdQH_Zwp9BugYTY2t-YkRGAYBPXDcpU_UHSRE4S6WJ62",
12+
"_info": "These fields are used for internal bookkeeping in Dodona, please do not change them."
13+
}
14+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
I've left a secret message for you in a text file [here](media/secret.txt). Because it's very
2+
secret I've encrypted it using a Caesar cipher.
3+
4+
Your task is to write a function to decrypt Caesar ciphertext. First let's understand what a Caesar
5+
cipher does with an example:
6+
7+
Here is some text to encrypt: `"I CAN HAZ CHEESEBURGER"`. The Caesar cipher also needs a "key" which is a number
8+
between 1 and 26. This number is used to "rotate" the plaintext input characters. For this example,
9+
the key is `1`.
10+
11+
The first letter is `"I"`. `"I"` "rotated" by `1` is the letter after `"I"` which is `"J"`. So the
12+
encrypted ciphertext is `"J"` so far.
13+
14+
The next letter is `" "`. Spaces are not changed by encryption and decryption so the encrypted
15+
ciphertext is now: `"J "`.
16+
17+
The same logic applies to all lettes in the string up until `"Z"`. At this point the plaintext,
18+
`"I CAN HA"` has been encryped to `"J DBO IB"`. To encrypt a `"Z"` you count back again from the
19+
beginning of the alphabet. So the plaintext `"Z"` is encrypted to `"A"`.
20+
21+
This is why it's called a "rotation". If you write the latin alphabet, A...Z on a circular piece of
22+
paper. Then again on a slightly smaller piece of paper. Rotate one of the circular pieces of paper by
23+
the key and this gives the mapping between plaintext letters and ciphertext letters.
24+
25+
![Caesar cipher](https://gkaccess.com/wp-content/uploads/2020/01/Caesar_Cipher_GateKeeper_security_compliance_proximity_authentication_2fa_mfa-768x803.jpg)
26+
27+
The final encryption of the string `"I CAN HAZ CHEESEBURGER"` with a key of `1` is then:
28+
`"J DBO IBA DIFFTFCVSHFS"`.
29+
30+
Your task is to write a function to decode caesar ciphers called `decrypt`. Your function should accept
31+
2 arguments:
32+
33+
- The encrypted ciphertext (as uninterpreted binary data type: `bytes`)
34+
- The key (type: `int`)
35+
36+
Your function should return a `str`.
37+
38+
You can use this template:
39+
40+
```python
41+
def decrypt(ciphertext: bytes, key: int) -> str:
42+
...
43+
```
44+
45+
46+
You should also `print()` my message to the console using your `decrypt` function.
47+
You can use a key of `13` to decode my message. The message is in a file called, `"secret.txt"`.
48+
49+
## Example
50+
51+
```console?lang=python&prompt=>>>
52+
>>> decrypt(b'B NFTTBHF', 1)
53+
"A MESSAGE"
54+
>>> decrypt(b'', 12)
55+
""
56+
>>> decrypt(b'QBUN CM SIOL HUGY', 6)
57+
"WHAT IS YOUR NAME"
58+
>>> decrypt(b'HELLO WORLD', 26)
59+
"HELLO WORLD"
60+
```
61+
62+
63+
Do not forget to include a docstring on your `decrypt` function and a test function called
64+
`test_decrypt` that returns `"Success"` if the `decrypt` function passes all tests.
13 KB
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- tab: "Bar plot global data"
2+
testcases:
3+
- expression:
4+
python: "submission.bar_plot_global('HadCRUT.5.0.1.0.analysis.summary_series.global.monthly.csv')"
5+
return: !oracle
6+
value: ""
7+
oracle: "custom_check"
8+
file: "test.py"
9+
name: "Image check"
10+
arguments: []
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from evaluation_utils import EvaluationResult, ConvertedOracleContext
2+
from matplotlib.testing.compare import compare_images
3+
4+
def evaluate_test(context: ConvertedOracleContext):
5+
context.actual.savefig("actual.png")
6+
result = compare_images("actual.png", "expected.png")
7+
return EvaluationResult(
8+
result=result is None,
9+
readable_expected="",
10+
readable_actual="",
11+
messages=[result] if result is not None else [],
12+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import csv
2+
from datetime import datetime
3+
4+
import matplotlib.pyplot as plt
5+
6+
def clean_global_data(raw: dict[str, str]) -> dict:
7+
return {
8+
"time": datetime.strptime(raw["Time"], "%Y-%m"),
9+
"temperature": float(raw["Anomaly (deg C)"])
10+
}
11+
12+
def parse_global_data(filename: str) -> list[dict]:
13+
data = []
14+
with open(filename) as raw:
15+
reader = csv.DictReader(raw)
16+
for row in reader:
17+
data.append(clean_global_data(row))
18+
19+
def times(data: list[dict]) -> list[datetime]:
20+
t = []
21+
for obs in data:
22+
t.append(obs["time"])
23+
24+
return t
25+
26+
def temperatures(data: list[dict]) -> list[float]:
27+
t = []
28+
for obs in data:
29+
t.append(obs["temperature"])
30+
return t
31+
32+
def bar_plot_global(filename: str):
33+
data = parse_global_data(filename)
34+
xaxis = times(data)
35+
yaxis = temperatures(data)
36+
37+
return plt.bar(x=xaxis, height=yaxis)

0 commit comments

Comments
 (0)