File tree Expand file tree Collapse file tree 4 files changed +80
-0
lines changed
Expand file tree Collapse file tree 4 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+
3+ read = lambda : sys .stdin .readline ().rstrip ()
4+
5+
6+ class Problem :
7+ def __init__ (self ):
8+ self .isbn = list (read ())
9+
10+ def solve (self ) -> None :
11+ total = sum ([(1 if idx % 2 == 0 else 3 ) * int (x ) for idx , x in enumerate (self .isbn ) if x != "*" ])
12+
13+ print (self .find_unknown (total ))
14+
15+ def find_unknown (self , total : int ) -> int :
16+ weight = 1 if self .isbn .index ("*" ) % 2 == 0 else 3
17+
18+ for candidate in range (10 ):
19+ if (total + weight * candidate ) % 10 == 0 :
20+ return candidate
21+
22+ return 0
23+
24+
25+ if __name__ == "__main__" :
26+ Problem ().solve ()
Original file line number Diff line number Diff line change 1+ [
2+ {
3+ "input" : [
4+ " 9788968322*73"
5+ ],
6+ "expected" : [
7+ " 2"
8+ ]
9+ },
10+ {
11+ "input" : [
12+ " 97389414*4397"
13+ ],
14+ "expected" : [
15+ " 0"
16+ ]
17+ }
18+ ]
Original file line number Diff line number Diff line change 1+ import json
2+ import os .path
3+ import unittest
4+ from io import StringIO
5+ from unittest .mock import patch
6+
7+ from parameterized import parameterized
8+
9+ from main import Problem
10+
11+
12+ def load_sample (filename : str ):
13+ path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), filename )
14+
15+ with open (path , "r" ) as file :
16+ return [(case ["input" ], case ["expected" ]) for case in json .load (file )]
17+
18+
19+ class TestCase (unittest .TestCase ):
20+ @parameterized .expand (load_sample ("sample.json" ))
21+ def test_case (self , case : str , expected : list [str ]):
22+ # When
23+ with (
24+ patch ("sys.stdin.readline" , side_effect = case ),
25+ patch ("sys.stdout" , new_callable = StringIO ) as output ,
26+ ):
27+ Problem ().solve ()
28+
29+ result = output .getvalue ().rstrip ()
30+
31+ # Then
32+ self .assertEqual ("\n " .join (expected ), result )
33+
34+
35+ if __name__ == "__main__" :
36+ unittest .main ()
You can’t perform that action at this time.
0 commit comments