Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day05 #11

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions exercise/python/day00/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from exercise.python.day00.encryption_email.encryption import Encryption
from exercise.python.day00.tests.utils import load_file, convert_key, convert_iv

if __name__ == '__main__':
contents = load_file("EncryptedEmail.txt")
key = convert_key('Advent Of Craft')
iv = convert_iv('2024')
print(Encryption(key, iv).decrypt(contents))
38 changes: 38 additions & 0 deletions exercise/python/day05/eid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from enum import Enum

MIN_SN = 1
MAX_SN = 999


class ElvenSex(Enum):
SLOUBI = "SLOUBI"
GAGNA = "GAGNA"
CATACT = "CATACT"


class Eid:
@staticmethod
def generate(sex: ElvenSex, sn: int, birth_year: int) -> str:
prefix = Eid.format_sex(sex) + Eid.format_birth_year(birth_year) + Eid.validate_and_format(sn)
control_key = Eid.format_control_key(prefix)
return prefix + control_key

@staticmethod
def format_sex(sex: ElvenSex) -> str:
if type(sex) != ElvenSex:
raise ValueError(f"sex {sex} is not an ElvenSex")
return {ElvenSex.SLOUBI: "1", ElvenSex.GAGNA: "2", ElvenSex.CATACT: "3"}[sex]

@staticmethod
def format_birth_year(birth_year: int):
return "{:02d}".format(birth_year % 100)

@staticmethod
def validate_and_format(sn: int) -> str:
if sn < MIN_SN or sn > MAX_SN:
raise ValueError(f"sn {sn} is out of range [{MIN_SN}, {MAX_SN}]")
return "{:03d}".format(sn)

@staticmethod
def format_control_key(prefix: str) -> str:
return "{:02d}".format(97 - (int(prefix) % 97))
68 changes: 66 additions & 2 deletions exercise/python/day05/tests/test_eid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,74 @@

from assertpy import assert_that

from eid import Eid, ElvenSex


class EIDTest(unittest.TestCase):
def test_eid(self):
assert_that(42).is_equal_to("Universe")

def generate_and_check_eid(self, *args, **kwargs):
eid = Eid.generate(*args, **kwargs)
assert_that(eid).is_length(8)
try:
int(eid)
except ValueError:
raise AssertionError(f"eid {eid} is not an number")
return eid

def test_eid_sloubi(self):
eid = self.generate_and_check_eid(ElvenSex.SLOUBI, birth_year=0, sn=1)
assert_that(eid).starts_with("1")

def test_eid_gagna(self):
eid = self.generate_and_check_eid(ElvenSex.GAGNA, birth_year=0, sn=1)
assert_that(eid).starts_with("2")

def test_eid_catact(self):
eid = self.generate_and_check_eid(ElvenSex.CATACT, birth_year=0, sn=1)
assert_that(eid).starts_with("3")

def test_eid_sex_must_be_elven_sex(self):
with self.assertRaises(ValueError):
self.generate_and_check_eid(sex="MALE", birth_year=0, sn=1)

def test_eid_birthyear(self):
eid = self.generate_and_check_eid(ElvenSex.SLOUBI, birth_year=1950, sn=1)
assert_that(eid[1:3]).is_equal_to("50")

def test_eid_birthyear_has_leading_zero(self):
eid = self.generate_and_check_eid(ElvenSex.SLOUBI, birth_year=1, sn=1)
assert_that(eid[1:3]).is_equal_to("01")

def test_eid_sn(self):
eid = self.generate_and_check_eid(ElvenSex.SLOUBI, birth_year=1, sn=123)
assert_that(eid[3:6]).is_equal_to("123")

def test_eid_sn_has_leading_zero(self):
eid = self.generate_and_check_eid(ElvenSex.SLOUBI, birth_year=1, sn=5)
assert_that(eid[3:6]).is_equal_to("005")

def test_eid_cannot_be_out_of_bound(self):
with self.assertRaises(ValueError):
self.generate_and_check_eid(ElvenSex.SLOUBI, birth_year=1, sn=1000)
with self.assertRaises(ValueError):
self.generate_and_check_eid(ElvenSex.SLOUBI, birth_year=1, sn=0)

@staticmethod
def check_eid_control_key_match_first_6_digits(eid):
prefix = eid[:6]
control_key = eid[6:]
assert_that((int(prefix) + int(control_key)) % 97).is_equal_to(0)

def test_eid_control_key(self):
for birth_year in range(100):
eid = self.generate_and_check_eid(sex=ElvenSex.SLOUBI, birth_year=birth_year, sn=1)
self.check_eid_control_key_match_first_6_digits(eid)
for sn in range(1, 999):
eid = self.generate_and_check_eid(sex=ElvenSex.SLOUBI, birth_year=1, sn=sn)
self.check_eid_control_key_match_first_6_digits(eid)
for sex in ElvenSex:
eid = self.generate_and_check_eid(sex=sex, birth_year=1, sn=1)
self.check_eid_control_key_match_first_6_digits(eid)


if __name__ == "__main__":
Expand Down