diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/2021_01/submarine_depth.py b/2021_01/submarine_depth.py new file mode 100644 index 0000000..390912c --- /dev/null +++ b/2021_01/submarine_depth.py @@ -0,0 +1,42 @@ +from dataclasses import dataclass +from typing import Optional + +MEASUREMENTS = ( + 199, + 200, + 208, + 210, + 200, + 207, + 240, + 269, + 260, + 263, +) + + +@dataclass +class Sonar: + last: Optional[int] = None + + def calc_measurement(self, new_measurement: int): + if self.last is None: + direction = 'N/A no previous measurement' + else: + if self.last < new_measurement: + direction = 'increased' + else: + direction = 'decreased' + + print(f'{new_measurement}: {direction}') + self.last = new_measurement + + +def main(): + sonar = Sonar() + for measurement in MEASUREMENTS: + sonar.calc_measurement(measurement) + + +if __name__ == '__main__': + main() diff --git a/2021_01/submarine_depth_iter.py b/2021_01/submarine_depth_iter.py new file mode 100644 index 0000000..3366db0 --- /dev/null +++ b/2021_01/submarine_depth_iter.py @@ -0,0 +1,37 @@ +MEASUREMENTS = ( + 199, + 200, + 208, + 210, + 200, + 207, + 240, + 269, + 260, + 263, +) + + +def _calc_measurement(x: int, y: int): + if x - y > 0: + return f'{x}: increased' + return f'{x}: decreased' + + +def main(): + it_a = iter(MEASUREMENTS) + it_b = iter(MEASUREMENTS) + print( + f'{next(it_a)}: N/A - no previous measurement\n' + + '\n'.join( + map( + _calc_measurement, + it_a, + it_b + ) + ) + ) + + +if __name__ == '__main__': + main()