from timeit import timeit
from math import floor
# 1. Can you implement another optimised function (sum_even_numbers2) using purely native python?
# Can you timeit to show it's faster and describe why it is.
# Can you also state if there are any pros or cons to how it is implemented?
# 2. Can you implement another optimised function (sum_even_numbers3) using third party packages?
# Can you timeit to show it's faster and describe why it is.
# Can you also state if there are any pros or cons to how it is implemented?
def sum_even_numbers1(numbers: list[int]) -> int:
total = 0
for num in numbers:
if floor(num/2) == num/2:
total += num
return total
import codewars_test as test
import solution # or from solution import example
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
iterations = 100
list_length = 100000
input_data = [i for i in range(0, list_length)]
print(timeit(lambda: sum_even_numbers1(input_data), number=iterations))
from timeit import timeit
from math import floor
# 1. Can you implement another optimised function (sum_even_numbers2) using purely native python?
# Can you timeit to show it's faster and describe why it is.
# Can you also state if there are any pros or cons to how it is implemented?
# 2. Can you implement another optimised function (sum_even_numbers3) using third party packages?
# Can you timeit to show it's faster and describe why it is.
# Can you also state if there are any pros or cons to how it is implemented?
def sum_even_numbers1(numbers: list[int]) -> int:
total = 0
for num in numbers:
if floor(num/2) == num/2:
total += num
return total
import codewars_test as test
import solution # or from solution import example
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
iterations = 100
list_length = 100000
input_data = [i for i in range(0, list_length)]
print(timeit(lambda: sum_even_numbers1(input_data), number=iterations))
"""Test equipment positions."""
from __future__ import annotations
from collections import Counter, defaultdict
import datetime as dt
import math
import numpy as np
import pandas as pd
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": 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": 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": 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": None, "timestamp": dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
return d
def find_furthest_west(d: defaultdict) -> str:
"""
Find the name of the truck that is furthest west. That is,
the truck with the smallest easting position component.
"""
pass
def get_latest_snapshot(d: defaultdict) -> dict[str, Any]:
"""
Return a snapshot of the latest positional updates for the
equipment.
"""
pass
def get_counts(d: defaultdict) -> dict[str, int]:
"""Return a dict of trucks and the times they have provided updates."""
pass
def calculate_speeds(d: defaultdict) -> defaultdict[str, Any]:
"""Return a dict of equipment and the speeds they are travelling at."""
pass
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)
from timeit import timeit
from math import floor
# 1. Can you implement another optimised function (sum_even_numbers2) using purely native python?
# Can you timeit to show it's faster and describe why it is.
# Can you also state if there are any pros or cons to how it is implemented?
# 2. Can you implement another optimised function (sum_even_numbers3) using third party packages?
# Can you timeit to show it's faster and describe why it is.
# Can you also state if there are any pros or cons to how it is implemented?
def sum_even_numbers1(numbers: list[int]) -> int:
total = 0
for num in numbers:
if floor(num/2) == num/2:
total += num
return total
import codewars_test as test
import solution # or from solution import example
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
iterations = 100
list_length = 100000
input_data = [i for i in range(0, list_length)]
print(timeit(lambda: sum_even_numbers1(input_data), number=iterations))
from timeit import timeit
from math import floor
# 1. Can you implement another optimised function (sum_even_numbers2) using purely native python?
# Can you timeit to show it's faster and describe why it is.
# Can you also state if there are any pros or cons to how it is implemented?
# 2. Can you implement another optimised function (sum_even_numbers3) using third party packages?
# Can you timeit to show it's faster and describe why it is.
# Can you also state if there are any pros or cons to how it is implemented?
def sum_even_numbers1(numbers: list[int]) -> int:
total = 0
for num in numbers:
if floor(num/2) == num/2:
total += num
return total
import codewars_test as test
import solution # or from solution import example
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
iterations = 100
list_length = 100000
input_data = [i for i in range(0, list_length)]
print(timeit(lambda: sum_even_numbers1(input_data), number=iterations))
"""Test equipment positions."""
from __future__ import annotations
from collections import Counter, defaultdict
import datetime as dt
import math
import numpy as np
import pandas as pd
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": 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": 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": 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": None, "timestamp": dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
return d
def find_furthest_west(d: defaultdict) -> str:
"""
Find the name of the truck that is furthest west. That is,
the truck with the smallest easting position component.
"""
pass
def get_latest_snapshot(d: defaultdict) -> dict[str, Any]:
"""
Return a snapshot of the latest positional updates for the
equipment.
"""
pass
def get_counts(d: defaultdict) -> dict[str, int]:
"""Return a dict of trucks and the times they have provided updates."""
pass
def calculate_speeds(d: defaultdict) -> defaultdict[str, Any]:
"""Return a dict of equipment and the speeds they are travelling at."""
pass
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)
"""Test equipment positions."""
from __future__ import annotations
from collections import Counter, defaultdict
import datetime as dt
import math
import numpy as np
import pandas as pd
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": 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": 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": 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": None, "timestamp": dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
return d
def find_furthest_west(d: defaultdict) -> str:
"""
Find the name of the truck that is furthest west. That is,
the truck with the smallest easting position component.
"""
pass
def get_latest_snapshot(d: defaultdict) -> dict[str, Any]:
"""
Return a snapshot of the latest positional updates for the
equipment.
"""
pass
def get_counts(d: defaultdict) -> dict[str, int]:
"""Return a dict of trucks and the times they have provided updates."""
pass
def calculate_speeds(d: defaultdict) -> defaultdict[str, Any]:
"""Return a dict of equipment and the speeds they are travelling at."""
pass
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)
Python interview questions
from __future__ import annotations
from collections import defaultdict
import datetime as dt
from typing import Any
import numpy as np
DataDictType = dict[str, list[dict[str, Any]]]
def data_dict() -> DataDictType:
"""Return all equipment positions."""
d = defaultdict(list)
d["T123"].append({'position': { "x": 42, "y": 24, "z": 0.42 }, 'last_updated': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
d["T456"].append({"position": { 'x': 21., 'y': 34, 'z': 0.289 }, 'last_updated': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
d["T789"].append({"position": { 'x': 17., 'y': 39, 'z': 0.789 }, 'last_updated': dt.datetime(2022, 1, 1, hour=12, minute=0, second=0)})
d["T456"].append({"position": { 'x': 91., 'y': 74, 'z': 0.389 }, 'last_updated': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)})
d["T123"].append({'position': { "x": 43, "y": 25, "z": 0.43 }, 'last_updated': dt.datetime(2022, 1, 1, hour=12, minute=0, second=1)})
d["T789"].append({"position": { 'x': 19., 'y': 79, 'z': 0.991 }, 'last_updated': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)})
d["T123"].append({'position': { "x": 44, "y": 26, "z": 0.43 }, 'last_updated': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)})
return d
def latest_snapshot() -> dict[str, Any]:
"""Return a snapshot of latest equipment."""
return {
"T456": { "position": { 'x': 91., 'y': 74, 'z': 0.389 }, 'last_updated': dt.datetime(2022, 1, 1, hour=12, minute=0, second=3)},
"T123": { 'position': { "x": 44, "y": 26, "z": 0.43 }, 'last_updated': dt.datetime(2022, 1, 1, hour=12, minute=0, second=2)},
"T789": { "position": { 'x': 19., 'y': 79, 'z': 0.991 }, 'last_updated': dt.datetime(2022, 1, 1, hour=12, minute=0, second=7)},
}
def counts() -> dict[str, int]:
"""Return counts per equipment."""
return {
"T123": 3,
"T456": 2,
"T789": 2
}
def average_speed_ms() -> dict[str, float]:
return {
"T123": 1.414231239822,
"T456": 26.87421316678,
"T789": 5.721496885627,
}
def find_furthest_west(d: DataDictType) -> str:
"""
Find the name of the truck that is furthest west. That is,
the truck with the smallest easting position component.
"""
raise NotImplementedError
def get_latest_snapshot(d: DataDictType) -> dict[str, Any]:
"""
Return a snapshot of the latest positional updates for the
equipment.
"""
raise NotImplementedError
def get_counts(d: DataDictType) -> dict[str, int]:
"""Return a dict of trucks and the times they have provided updates."""
raise NotImplementedError
def get_average_speed_ms(d: DataDictType) -> dict[str, float]:
"""Return a dict of trucks and their average speed in meters per second over all the provided updates."""
raise NotImplementedError
import codewars_test as test
import solution
# 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_counts():
d = data_dict()
the_counts = counts()
test.assert_equals(get_counts(d), the_counts)
@test.it('test get_average_speed_ms')
def test_get_speeds():
d = data_dict()
expected = average_speed_ms()
actual = get_average_speed_ms(d)
for truck_id in expected.keys():
test.assert_approx_equals(expected[truck_id], actual[truck_id])