solved 2021_01
This commit is contained in:
parent
c1a93acefa
commit
9d5fda60a0
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.idea
|
42
2021_01/submarine_depth.py
Normal file
42
2021_01/submarine_depth.py
Normal file
@ -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()
|
37
2021_01/submarine_depth_iter.py
Normal file
37
2021_01/submarine_depth_iter.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user