Updated find the furthest to the west
We take in consideration the fursthest truck reagrading its last position "last snapshot"
"""Test equipment positions.""" from __future__ import annotations from collections import defaultdict import datetime as dt import functools from typing import Any def data_dict() -> defaultdict[str, Any]: """Return all equipment positions.""" d = defaultdict(list) d['T123'].append({'position': {'x': 42, 'y': 24, 'z': 0.42}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) d['T456'].append({'position': {'x': 21.0, 'y': 34, 'z': 0.289}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) d['T789'].append({'position': {'x': 17, 'y': 39, 'z': 0.789}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) d['T456'].append({'position': {'x': 91.0, 'y': 114, 'z': 0.489}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)}) d['T123'].append({'position': {'x': 43, 'y': 25, 'z': 0.43}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=1)}) d['T789'].append({'position': {'x': 19., 'y': 79, 'z': 0.991}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=6)}) d['T123'].append({'position': {'x': 46, 'y': 29, 'z': 0.44}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)}) d['T456'].append({'position': {'x': 24.0, 'y': 37, 'z': 0.297}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)}) d['T123'].append({'position': {'x': 49.0, 'y': 32, 'z': 0.451}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)}) d['T789'].append({'position': {'x': 23., 'y': 81, 'z': 1.103}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)}) return d def latest_snapshot() -> dict[str, Any]: """Return a snapshot of latest equipment.""" return { 'T123': {'position': {'x': 49.0, 'y': 32, 'z': 0.451}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)}, 'T456': {'position': {'x': 24.0, 'y': 37, 'z': 0.297}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)}, 'T789': {'position': {'x': 23.0, 'y': 81, 'z': 1.103}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)}, } def counts() -> dict[str, int]: """Return counts per equipment.""" return { 'T123': 4, 'T456': 3, 'T789': 3 } def speeds() -> defaultdict[str, Any]: """Return speeds of equipment.""" d = defaultdict(list) d['T123'].append({'speed': round(4.242654947082074, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)}) d['T123'].append({'speed': round(5.00000999999, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)}) d['T123'].append({'speed': round(1.4142489172702237, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=1)}) d['T123'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) d['T456'].append({'speed': round(102.0687849638664, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)}) d['T456'].append({'speed': round(35.43388209045123, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)}) d['T456'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) d['T789'].append({'speed': round(4.473538196997986, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)}) d['T789'].append({'speed': round(6.6750796998987205, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=6)}) d['T789'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) return d def find_furthest_west(data: defaultdict) -> str: """ Find the name of the truck that is furthest west. That is, the truck with the smallest easting position component. """ #f = lambda v: min([x['position']['x'] for x in v]) last = get_latest_snapshot(data) return functools.reduce(lambda a, b: a if last[a]["position"]["x"] < last[b]["position"]["x"] else b, last) def get_latest_snapshot(data: defaultdict) -> dict[str, Any]: """ Return a snapshot of the latest positional updates for the equipment. """ return {k: v[-1] for k, v in data.items()} def get_counts(data: defaultdict) -> dict[str, int]: """Return a dict of trucks and the times they have provided updates.""" return {k : len(v) for k, v in data.items()} def calculate_speeds(data: defaultdict) -> defaultdict[str, Any]: """Return a dict of equipment and the speeds they are travelling at.""" out = defaultdict(list) diff = lambda x: [b - a for a, b in zip(x, x[1:])] for k, v in data.items(): t = [x['timestamp'] for x in v] dt = diff([x["timestamp"].second for x in v]) dx = diff([x["position"]["x"] for x in v]) dy = diff([x["position"]["y"] for x in v]) dd = [(a ** 2 + b ** 2) ** 0.5 for a, b in zip(dx, dy)] s = [a / b for a, b in zip(dd, dt)] for speed, timestamp in zip(s[::-1], t[::-1]): out[k].append({'speed': round(speed, 1), 'timestamp': timestamp}) out[k].append({'speed': None, 'timestamp': t[0]}) return out
- """Test equipment positions."""
- from __future__ import annotations
- from collections import defaultdict
- import datetime as dt
- import functools
- from typing import Any
- def data_dict() -> defaultdict[str, Any]:
- """Return all equipment positions."""
- d = defaultdict(list)
- d['T123'].append({'position': {'x': 42, 'y': 24, 'z': 0.42}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
- d['T456'].append({'position': {'x': 21.0, 'y': 34, 'z': 0.289}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
- d['T789'].append({'position': {'x': 17, 'y': 39, 'z': 0.789}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
- d['T456'].append({'position': {'x': 91.0, 'y': 114, 'z': 0.489}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})
- d['T123'].append({'position': {'x': 43, 'y': 25, 'z': 0.43}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=1)})
- d['T789'].append({'position': {'x': 19., 'y': 79, 'z': 0.991}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=6)})
- d['T123'].append({'position': {'x': 46, 'y': 29, 'z': 0.44}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)})
- d['T456'].append({'position': {'x': 24.0, 'y': 37, 'z': 0.297}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)})
- d['T123'].append({'position': {'x': 49.0, 'y': 32, 'z': 0.451}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})
- d['T789'].append({'position': {'x': 23., 'y': 81, 'z': 1.103}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)})
- return d
- def latest_snapshot() -> dict[str, Any]:
- """Return a snapshot of latest equipment."""
- return {
- 'T123': {'position': {'x': 49.0, 'y': 32, 'z': 0.451}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)},
- 'T456': {'position': {'x': 24.0, 'y': 37, 'z': 0.297}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)},
- 'T789': {'position': {'x': 23.0, 'y': 81, 'z': 1.103}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)},
- }
- def counts() -> dict[str, int]:
- """Return counts per equipment."""
- return {
- 'T123': 4,
- 'T456': 3,
- 'T789': 3
- }
- def speeds() -> defaultdict[str, Any]:
- """Return speeds of equipment."""
- d = defaultdict(list)
- d['T123'].append({'speed': round(4.242654947082074, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})
- d['T123'].append({'speed': round(5.00000999999, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)})
- d['T123'].append({'speed': round(1.4142489172702237, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=1)})
- d['T123'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
- d['T456'].append({'speed': round(102.0687849638664, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)})
- d['T456'].append({'speed': round(35.43388209045123, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})
- d['T456'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
- d['T789'].append({'speed': round(4.473538196997986, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)})
- d['T789'].append({'speed': round(6.6750796998987205, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=6)})
- d['T789'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
- return d
- def find_furthest_west(data: defaultdict) -> str:
- """
- Find the name of the truck that is furthest west. That is,
- the truck with the smallest easting position component.
- """
f = lambda v: min([x['position']['x'] for x in v])return functools.reduce(lambda a, b: a if f(data[a]) < f(data[b]) else b, data.keys())- #f = lambda v: min([x['position']['x'] for x in v])
- last = get_latest_snapshot(data)
- return functools.reduce(lambda a, b: a if last[a]["position"]["x"] < last[b]["position"]["x"] else b, last)
- def get_latest_snapshot(data: defaultdict) -> dict[str, Any]:
- """
- Return a snapshot of the latest positional updates for the
- equipment.
- """
- return {k: v[-1] for k, v in data.items()}
- def get_counts(data: defaultdict) -> dict[str, int]:
- """Return a dict of trucks and the times they have provided updates."""
- return {k : len(v) for k, v in data.items()}
- def calculate_speeds(data: defaultdict) -> defaultdict[str, Any]:
- """Return a dict of equipment and the speeds they are travelling at."""
- out = defaultdict(list)
- diff = lambda x: [b - a for a, b in zip(x, x[1:])]
- for k, v in data.items():
- t = [x['timestamp'] for x in v]
- dt = diff([x["timestamp"].second for x in v])
- dx = diff([x["position"]["x"] for x in v])
- dy = diff([x["position"]["y"] for x in v])
- dd = [(a ** 2 + b ** 2) ** 0.5 for a, b in zip(dx, dy)]
- s = [a / b for a, b in zip(dd, dt)]
- for speed, timestamp in zip(s[::-1], t[::-1]):
- out[k].append({'speed': round(speed, 1), 'timestamp': timestamp})
- out[k].append({'speed': None, 'timestamp': t[0]})
- return out
"""Test equipment positions.""" from __future__ import annotations from collections import defaultdict import datetime as dt import functools from typing import Any def data_dict() -> defaultdict[str, Any]: """Return all equipment positions.""" d = defaultdict(list) d['T123'].append({'position': {'x': 42, 'y': 24, 'z': 0.42}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) d['T456'].append({'position': {'x': 21.0, 'y': 34, 'z': 0.289}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) d['T789'].append({'position': {'x': 17, 'y': 39, 'z': 0.789}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) d['T456'].append({'position': {'x': 91.0, 'y': 114, 'z': 0.489}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)}) d['T123'].append({'position': {'x': 43, 'y': 25, 'z': 0.43}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=1)}) d['T789'].append({'position': {'x': 19., 'y': 79, 'z': 0.991}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=6)}) d['T123'].append({'position': {'x': 46, 'y': 29, 'z': 0.44}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)}) d['T456'].append({'position': {'x': 24.0, 'y': 37, 'z': 0.297}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)}) d['T123'].append({'position': {'x': 49.0, 'y': 32, 'z': 0.451}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)}) d['T789'].append({'position': {'x': 23., 'y': 81, 'z': 1.103}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)}) return d def latest_snapshot() -> dict[str, Any]: """Return a snapshot of latest equipment.""" return { 'T123': {'position': {'x': 49.0, 'y': 32, 'z': 0.451}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)}, 'T456': {'position': {'x': 24.0, 'y': 37, 'z': 0.297}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)}, 'T789': {'position': {'x': 23.0, 'y': 81, 'z': 1.103}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)}, } def counts() -> dict[str, int]: """Return counts per equipment.""" return { 'T123': 4, 'T456': 3, 'T789': 3 } def speeds() -> defaultdict[str, Any]: """Return speeds of equipment.""" d = defaultdict(list) d['T123'].append({'speed': round(4.242654947082074, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)}) d['T123'].append({'speed': round(5.00000999999, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)}) d['T123'].append({'speed': round(1.4142489172702237, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=1)}) d['T123'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) d['T456'].append({'speed': round(102.0687849638664, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)}) d['T456'].append({'speed': round(35.43388209045123, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)}) d['T456'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) d['T789'].append({'speed': round(4.473538196997986, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)}) d['T789'].append({'speed': round(6.6750796998987205, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=6)}) d['T789'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)}) return d def find_furthest_west(data: defaultdict) -> str: """ Find the name of the truck that is furthest west. That is, the truck with the smallest easting position component. """ f = lambda v: min([x['position']['x'] for x in v]) return functools.reduce(lambda a, b: a if f(data[a]) < f(data[b]) else b, data.keys()) def get_latest_snapshot(data: defaultdict) -> dict[str, Any]: """ Return a snapshot of the latest positional updates for the equipment. """ return {k: v[-1] for k, v in data.items()} def get_counts(data: defaultdict) -> dict[str, int]: """Return a dict of trucks and the times they have provided updates.""" return {k : len(v) for k, v in data.items()} def calculate_speeds(data: defaultdict) -> defaultdict[str, Any]: """Return a dict of equipment and the speeds they are travelling at.""" out = defaultdict(list) diff = lambda x: [b - a for a, b in zip(x, x[1:])] for k, v in data.items(): t = [x['timestamp'] for x in v] dt = diff([x["timestamp"].second for x in v]) dx = diff([x["position"]["x"] for x in v]) dy = diff([x["position"]["y"] for x in v]) dd = [(a ** 2 + b ** 2) ** 0.5 for a, b in zip(dx, dy)] s = [a / b for a, b in zip(dd, dt)] for speed, timestamp in zip(s[::-1], t[::-1]): out[k].append({'speed': round(speed, 1), 'timestamp': timestamp}) out[k].append({'speed': None, 'timestamp': t[0]}) return out
- """Test equipment positions."""
- from __future__ import annotations
from collections import Counter, defaultdict- from collections import defaultdict
- import datetime as dt
import mathimport numpy as npimport pandas as pd- import functools
- from typing import Any
from datetime import datetime # datetime required for calculation of time differences.- def data_dict() -> defaultdict[str, Any]:
- """Return all equipment positions."""
- d = defaultdict(list)
- d['T123'].append({'position': {'x': 42, 'y': 24, 'z': 0.42}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
- d['T456'].append({'position': {'x': 21.0, 'y': 34, 'z': 0.289}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
- d['T789'].append({'position': {'x': 17, 'y': 39, 'z': 0.789}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
- d['T456'].append({'position': {'x': 91.0, 'y': 114, 'z': 0.489}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})
- d['T123'].append({'position': {'x': 43, 'y': 25, 'z': 0.43}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=1)})
- d['T789'].append({'position': {'x': 19., 'y': 79, 'z': 0.991}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=6)})
- d['T123'].append({'position': {'x': 46, 'y': 29, 'z': 0.44}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)})
- d['T456'].append({'position': {'x': 24.0, 'y': 37, 'z': 0.297}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)})
- d['T123'].append({'position': {'x': 49.0, 'y': 32, 'z': 0.451}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})
- d['T789'].append({'position': {'x': 23., 'y': 81, 'z': 1.103}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)})
- return d
def latest_snapshot() -> defaultdict[str, Any]:- def latest_snapshot() -> dict[str, Any]:
- """Return a snapshot of latest equipment."""
# put latest information into a defaultdict[list] list to match list type other def() such as data_dict#return {# 'T123': {'position': {'x': 49.0, 'y': 32, 'z': 0.451}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)},# 'T456': {'position': {'x': 24.0, 'y': 37, 'z': 0.297}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)},# 'T789': {'position': {'x': 23.0, 'y': 81, 'z': 1.103}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)},#}d = defaultdict(list)d['T123'].append({'position': {'x': 49.0, 'y': 32, 'z': 0.451}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})d['T456'].append({'position': {'x': 24.0, 'y': 37, 'z': 0.297}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)})d['T789'].append({'position': {'x': 23.0, 'y': 81, 'z': 1.103}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)})return d- return {
- 'T123': {'position': {'x': 49.0, 'y': 32, 'z': 0.451}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)},
- 'T456': {'position': {'x': 24.0, 'y': 37, 'z': 0.297}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)},
- 'T789': {'position': {'x': 23.0, 'y': 81, 'z': 1.103}, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)},
- }
- def counts() -> dict[str, int]:
- """Return counts per equipment."""
- return {
- 'T123': 4,
- 'T456': 3,
- 'T789': 3
- }
- def speeds() -> defaultdict[str, Any]:
- """Return speeds of equipment."""
- d = defaultdict(list)
d['T123'].append({'speed': 4.242654947082074, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})d['T123'].append({'speed': 5.00000999999, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)})d['T123'].append({'speed': 1.4142489172702237, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=1)})- d['T123'].append({'speed': round(4.242654947082074, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})
- d['T123'].append({'speed': round(5.00000999999, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)})
- d['T123'].append({'speed': round(1.4142489172702237, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=1)})
- d['T123'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
d['T456'].append({'speed': 102.0687849638664, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)})d['T456'].append({'speed': 35.43388209045123, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})- d['T456'].append({'speed': round(102.0687849638664, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=4)})
- d['T456'].append({'speed': round(35.43388209045123, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})
- d['T456'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
d['T789'].append({'speed': 4.473538196997986, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)})d['T789'].append({'speed': 6.6750796998987205, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=6)})- d['T789'].append({'speed': round(4.473538196997986, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)})
- d['T789'].append({'speed': round(6.6750796998987205, 1), 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=6)})
- d['T789'].append({'speed': None, 'timestamp': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
- return d
def find_furthest_west(d: defaultdict) -> str:- def find_furthest_west(data: defaultdict) -> str:
- """
- Find the name of the truck that is furthest west. That is,
- the truck with the smallest easting position component.
- """
- f = lambda v: min([x['position']['x'] for x in v])
- return functools.reduce(lambda a, b: a if f(data[a]) < f(data[b]) else b, data.keys())
# get most west truckmin_lat = 180truck = '' # truck numberfor key in d.keys():a1 = d[key]posit = (a1.__getitem__(0))['position'] # get positionlat_temp = posit['x']if lat_temp < min_lat: # save truck number with lowest x (hence west) value.min_lat = lat_temptruck = keyreturn truckdef get_latest_snapshot(d: defaultdict) -> dict[str, Any]:- def get_latest_snapshot(data: defaultdict) -> dict[str, Any]:
- """
- Return a snapshot of the latest positional updates for the
- equipment.
- """
# get latest entry for each truck.e = defaultdict(list)- return {k: v[-1] for k, v in data.items()}
for key in d.keys(): # Key is truck numbertruck = d[key]#print(len(truck))count = 0#print(range(len(truck)))for entry_n1 in range(len(truck)): # loop through all entries for a truck to get latest entrytime_temp_n1 = (truck.__getitem__(entry_n1))['timestamp'] # get time information from listif count == 0:time_temp_n2 = time_temp_n1entry_n2 = entry_n1count = 1else:if time_temp_n1 > time_temp_n2:time_temp_n2 = time_temp_n1entry_n2 = entry_n1# put latest entry in e list.e[key].append({'position':(truck.__getitem__(entry_n2))['position'],'timestamp':time_temp_n2})return edef get_counts(d: defaultdict) -> dict[str, int]:- def get_counts(data: defaultdict) -> dict[str, int]:
- """Return a dict of trucks and the times they have provided updates."""
e = {}- return {k : len(v) for k, v in data.items()}
for key in d.keys(): # key is the truck number.truck = d[key]no_entries = len(truck) # get number of entries per truck.e[key] = no_entriesreturn(e)def calculate_speeds(d: defaultdict) -> defaultdict[str, Any]:- def calculate_speeds(data: defaultdict) -> defaultdict[str, Any]:
- """Return a dict of equipment and the speeds they are travelling at."""
# Select a truck, order entries by time, calculate difference in time and space, calculate the speed.# No units is given so no conversion in speed is done. Speed in distance / second.e = defaultdict(list)for key in d.keys():truck = d[key]truck_ordered = truck.sort(key=lambda x:x['timestamp']) # order truck time# get position and time difference. Calculuate speed.no_entries = len(truck)count = 0len_n1 = len(truck)for entry_n1 in range(len_n1):time_temp_n1 = (truck.__getitem__(entry_n1))['timestamp'] # get timeposition_temp_n1 = (truck.__getitem__(entry_n1))['position'] # get positionif count == 0:e[key].append({'speed':None,'timestamp':time_temp_n1}) # first time entry, set speed to None.count = 1else:time_temp_n2 = (truck.__getitem__(entry_n1 + 1))['timestamp']position_temp_n2 = (truck.__getitem__(entry_n1 + 1))['position']# get spatial differencex_diff = abs(position_temp_n2['x'] - position_temp_n1['x'])y_diff = abs(position_temp_n2['y'] - position_temp_n1['y'])distance = math.sqrt((x_diff * x_diff) + (y_diff * y_diff))# get temporal differencetime_diff = abs((time_temp_n1 - time_temp_n2).total_seconds())# calculate speedspeed = round((distance / time_diff),1) # round to 1 decimal placee[key].append({'speed':speed,'timestamp':time_temp_n1})count = count + 1# Break loopif count == len_n1 - 1:breakreturn e- out = defaultdict(list)
- diff = lambda x: [b - a for a, b in zip(x, x[1:])]
- for k, v in data.items():
- t = [x['timestamp'] for x in v]
- dt = diff([x["timestamp"].second for x in v])
- dx = diff([x["position"]["x"] for x in v])
- dy = diff([x["position"]["y"] for x in v])
- dd = [(a ** 2 + b ** 2) ** 0.5 for a, b in zip(dx, dy)]
- s = [a / b for a, b in zip(dd, dt)]
- for speed, timestamp in zip(s[::-1], t[::-1]):
- out[k].append({'speed': round(speed, 1), 'timestamp': timestamp})
- out[k].append({'speed': None, 'timestamp': t[0]})
- return out
import codewars_test as test import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it('test find_furthest_west') def test_cases(): d = data_dict() test.assert_equals(find_furthest_west(d), 'T789') @test.it('test get_latest_snapshot') def test_get_latest_snapshot(): d = data_dict() snapshot = latest_snapshot() test.assert_equals(get_latest_snapshot(d), snapshot) @test.it('test get_counts') def test_get_count(): d = data_dict() the_counts = counts() test.assert_equals(get_counts(d), the_counts) @test.it('test calculate_speeds') def test_calculate_speeds(): d = data_dict() the_speeds = speeds() test.assert_equals(calculate_speeds(d), the_speeds)
- import codewars_test as test
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it('test find_furthest_west')
- def test_cases():
- d = data_dict()
#e = find_furthest_west(d) # for testing, get furthest west#print(e)- test.assert_equals(find_furthest_west(d), 'T789')
- @test.it('test get_latest_snapshot')
- def test_get_latest_snapshot():
- d = data_dict()
- snapshot = latest_snapshot()
# For testing#print(snapshot) # get latest snapshot.#test = get_latest_snapshot(d) # get latest for each truck in list d.#print(test)# compare latest snapshot entries with the latest truck entries in data dict- test.assert_equals(get_latest_snapshot(d), snapshot)
- @test.it('test get_counts')
- def test_get_count():
- d = data_dict()
- the_counts = counts()
# for testing#print(the_counts)#print(get_counts(d)) # get the number of times a truck is in d.- test.assert_equals(get_counts(d), the_counts)
- @test.it('test calculate_speeds')
- def test_calculate_speeds():
- d = data_dict()
- the_speeds = speeds()
# for testing#print(the_speeds)#print("===")#print(calculate_speeds(d))#print("+++")test.assert_equals(calculate_speeds(d), the_speeds)- test.assert_equals(calculate_speeds(d), the_speeds)