Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cc263c4088 | |||
| 2f88ebe668 | |||
| 69ac65795a | |||
| c577565301 | |||
| 621fc4a731 | |||
| 347f337d6d | |||
| 8eef04d99e | |||
| dae045562b | |||
| e09a44dcd1 | |||
| 2966a01e71 | |||
| 24eee51297 | |||
| d4b184e993 | |||
| b5472e9716 | |||
| e4a038edaf | |||
| 522c4af42b | |||
| 6b28a6ab1d | |||
| ed538a8810 | |||
| f0af4d7dd2 | |||
| 17297aaf7d |
@ -15,7 +15,7 @@ Repository for CDS-2020 Programming and Promt Engineering II
|
|||||||
|build|Build-System, Dependencies|build: update requirements.txt|
|
|build|Build-System, Dependencies|build: update requirements.txt|
|
||||||
|
|
||||||
# Codewars
|
# Codewars
|
||||||
|Title|Source (src/codewars/)|Test (test/test_codewars/)|URL|
|
|Title|Source (src/codewars/)|Test (tests/test_codewars/)|URL|
|
||||||
|-|-|-|-|
|
|-|-|-|-|
|
||||||
|Find the force of gravity between two objects|kata_force_of_gravity.py|test_force_of_gravity.py|[5b609ebc8f47bd595e000627](https://www.codewars.com/kata/5b609ebc8f47bd595e000627)|
|
|Find the force of gravity between two objects|kata_force_of_gravity.py|test_force_of_gravity.py|[5b609ebc8f47bd595e000627](https://www.codewars.com/kata/5b609ebc8f47bd595e000627)|
|
||||||
|The Lamp: Revisited|kata_the_lamp.py|test_the_lamp.py|[570e6e32de4dc8a8340016dd](https://www.codewars.com/kata/570e6e32de4dc8a8340016dd)|
|
|The Lamp: Revisited|kata_the_lamp.py|test_the_lamp.py|[570e6e32de4dc8a8340016dd](https://www.codewars.com/kata/570e6e32de4dc8a8340016dd)|
|
||||||
@ -31,3 +31,5 @@ Repository for CDS-2020 Programming and Promt Engineering II
|
|||||||
|Next bigger number with the same digits|kata_next_bigger_number_same_digits.py|test_next_bigger_number_same_digits.py|[55983863da40caa2c900004e](https://www.codewars.com/kata/55983863da40caa2c900004e)|
|
|Next bigger number with the same digits|kata_next_bigger_number_same_digits.py|test_next_bigger_number_same_digits.py|[55983863da40caa2c900004e](https://www.codewars.com/kata/55983863da40caa2c900004e)|
|
||||||
|Snail|kata_snail.py|test_snail.py|[521c2db8ddc89b9b7a0000c1](https://www.codewars.com/kata/521c2db8ddc89b9b7a0000c1)|
|
|Snail|kata_snail.py|test_snail.py|[521c2db8ddc89b9b7a0000c1](https://www.codewars.com/kata/521c2db8ddc89b9b7a0000c1)|
|
||||||
|Chinese Numeral Encoder|kata_chinese_numeral_encoder.py|test_chinese_numeral_encoder.py|[52608f5345d4a19bed000b31](https://www.codewars.com/kata/52608f5345d4a19bed000b31)|
|
|Chinese Numeral Encoder|kata_chinese_numeral_encoder.py|test_chinese_numeral_encoder.py|[52608f5345d4a19bed000b31](https://www.codewars.com/kata/52608f5345d4a19bed000b31)|
|
||||||
|
|RGB To Hex Conversion|kata_rgb_2_hex.py|test_rgb_2_hex.py|[513e08acc600c94f01000001](https://www.codewars.com/kata/513e08acc600c94f01000001)|
|
||||||
|
|Most frequently used words in a text|kata_most_words_used.py|test_most_words_used.py|[51e056fe544cf36c410000fb](https://www.codewars.com/kata/51e056fe544cf36c410000fb)|
|
||||||
|
|||||||
2
src/codewars/kata_most_words_used.py
Normal file
2
src/codewars/kata_most_words_used.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
def top_3_words(text):
|
||||||
|
return None
|
||||||
11
src/codewars/kata_rgb_2_hex.py
Normal file
11
src/codewars/kata_rgb_2_hex.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
def rgb(r, g, b):
|
||||||
|
rgb = {"r": int(r), "g": int(g), "b": int(b)}
|
||||||
|
|
||||||
|
for decimal in rgb:
|
||||||
|
rgb[decimal] = min(255, max(rgb[decimal], 0))
|
||||||
|
rgb[decimal] = hex(rgb[decimal]).split("x")[-1]
|
||||||
|
|
||||||
|
if len(rgb[decimal]) == 1:
|
||||||
|
rgb[decimal] = "0" + rgb[decimal]
|
||||||
|
|
||||||
|
return f"{rgb['r']}{rgb['g']}{rgb['b']}".upper()
|
||||||
8
src/tutorial/hints/load_scores.py
Normal file
8
src/tutorial/hints/load_scores.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
def load_scores(data: list[tuple[str, str | int]]) -> dict:
|
||||||
|
result = {}
|
||||||
|
for name, points in data:
|
||||||
|
result[name] = int(points)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
print(load_scores([("Sandro", "54"), ("Arif", "45"), ("Sidney", 89)]))
|
||||||
0
src/tutorial/testing/practice/__init__.py
Normal file
0
src/tutorial/testing/practice/__init__.py
Normal file
16
src/tutorial/testing/practice/bmi.py
Normal file
16
src/tutorial/testing/practice/bmi.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# Task: Schreibt für die folgende Aufgabe einige Unit-Test. Nutzt dazu für den Happy-Path einen parametrisierten Test für
|
||||||
|
# einige valide Inputs und Outputs
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_bmi(weight_kg: float, height_m: float) -> float:
|
||||||
|
"""
|
||||||
|
Berechnet den Body Mass Index (BMI).
|
||||||
|
BMI = Gewicht (kg) / Grösse (m)^2
|
||||||
|
"""
|
||||||
|
if weight_kg <= 0 or height_m <= 0:
|
||||||
|
raise ValueError("Gewicht und Grösse müssen positiv sein.")
|
||||||
|
return weight_kg / (height_m**2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(calculate_bmi(weight_kg=72, height_m=1.84))
|
||||||
29
src/tutorial/testing/practice/catching_exeptions.py
Normal file
29
src/tutorial/testing/practice/catching_exeptions.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# TASK: Schreibe sinnvolle Tests für die folgende Funktion
|
||||||
|
# Schreibe Tests für die korrekte Struktur des Outputs der Funktion
|
||||||
|
# Teste, ob bei Übergabe von 'falschem' Parameter data eine Exception geworfen wird
|
||||||
|
|
||||||
|
# https://docs.python.org/3/library/exceptions.html
|
||||||
|
# Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError,
|
||||||
|
# but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a ValueError.
|
||||||
|
|
||||||
|
|
||||||
|
def double_integers(data: list[int]) -> list[int]:
|
||||||
|
"""Doubles a list of given integers
|
||||||
|
|
||||||
|
:param data: list of integers
|
||||||
|
:return: list of doubled integers
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not isinstance(data, list):
|
||||||
|
raise TypeError("data must be a list")
|
||||||
|
if not all([isinstance(i, int) for i in data]):
|
||||||
|
raise TypeError("data may contain only integers")
|
||||||
|
|
||||||
|
return [i * 2 for i in data]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
data = [1, 2, 3]
|
||||||
|
result = double_integers(data)
|
||||||
|
print(result)
|
||||||
14
src/tutorial/testing/practice/kata_list_filtering.py
Normal file
14
src/tutorial/testing/practice/kata_list_filtering.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# https://www.codewars.com/kata/53dbd5315a3c69eed20002dd
|
||||||
|
# level: 7 kyu
|
||||||
|
|
||||||
|
# In this kata you will create a function that takes a list of non-negative integers and strings and returns a new
|
||||||
|
# list with the strings filtered out.
|
||||||
|
#
|
||||||
|
# Example
|
||||||
|
# filter_list([1,2,'a','b']) == [1,2]
|
||||||
|
# filter_list([1,'a','b',0,15]) == [1,0,15]
|
||||||
|
# filter_list([1,2,'aasf','1','123',123]) == [1,2,123]
|
||||||
|
|
||||||
|
|
||||||
|
def filter_list(a_list):
|
||||||
|
return [i for i in a_list if isinstance(i, int)]
|
||||||
37
src/tutorial/testing/practice/pandas_filter_rows.py
Normal file
37
src/tutorial/testing/practice/pandas_filter_rows.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
# https://www.codewars.com/kata/5ea2baed9345eb001e8ce394
|
||||||
|
# 7 kyu
|
||||||
|
|
||||||
|
# Input parameters
|
||||||
|
|
||||||
|
# dataframe: pandas.DataFrame object
|
||||||
|
# col: target column
|
||||||
|
# func: filter function
|
||||||
|
|
||||||
|
# Task
|
||||||
|
# Your function must return a new pandas.DataFrame object with the same columns as the original input. However,
|
||||||
|
# include only the rows whose cell values in the designated column evaluate to False by func.
|
||||||
|
#
|
||||||
|
# Input DataFrame will never be empty. The target column will always be one of the dataframe columns. Filter function
|
||||||
|
# will be a valid one. Index value must remain the same.
|
||||||
|
|
||||||
|
# REMARK: leichte Modifikation -> es wird ausgegeben, was in der Funktion definiert wurde (True) und nicht umgekehrt.
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
|
||||||
|
def filter_dataframe(df, col, func):
|
||||||
|
if col not in df.columns:
|
||||||
|
raise ValueError(f"Column '{col}' is not present in the DataFrame.")
|
||||||
|
mask = df[col].apply(func)
|
||||||
|
return df[mask]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
df = pd.DataFrame(
|
||||||
|
{
|
||||||
|
"A": [1, 2, 3, 4, 5],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
print(filter_dataframe(df, "A", lambda x: x >= 4))
|
||||||
18
src/tutorial/testing/practice/vowel_count.py
Normal file
18
src/tutorial/testing/practice/vowel_count.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# https://www.codewars.com/kata/54ff3102c1bad923760001f3
|
||||||
|
# level: 7 kyu
|
||||||
|
|
||||||
|
# Return the number (count) of vowels in the given string.
|
||||||
|
# We will consider a, e, i, o, u as vowels for this Kata (but not y).
|
||||||
|
# The input string will only consist of lower case letters and/or spaces.
|
||||||
|
|
||||||
|
|
||||||
|
def get_count(inputStr):
|
||||||
|
num_vowels = 0
|
||||||
|
for char in inputStr:
|
||||||
|
if char in "aeiouAEIOU":
|
||||||
|
num_vowels = num_vowels + 1
|
||||||
|
return num_vowels
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(get_count("Hello World"))
|
||||||
6
src/tutorial/testing/shop/pricing.py
Normal file
6
src/tutorial/testing/shop/pricing.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
def discount_price(price: float, percent: float) -> float:
|
||||||
|
if price < 0:
|
||||||
|
raise ValueError("Price must be >= 0")
|
||||||
|
if not 0 <= percent <= 100:
|
||||||
|
raise ValueError("Percent must be between 0 and 100")
|
||||||
|
return price - price * percent / 100
|
||||||
5
tests/tests_codewars/test_most_words_used.py
Normal file
5
tests/tests_codewars/test_most_words_used.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from src.codewars.kata_most_words_used import top_3_words
|
||||||
|
|
||||||
|
|
||||||
|
def test_top_3_words():
|
||||||
|
assert top_3_words() == 0
|
||||||
16
tests/tests_codewars/test_rgb_2_hex.py
Normal file
16
tests/tests_codewars/test_rgb_2_hex.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from src.codewars.kata_rgb_2_hex import rgb
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("r", "g", "b", "expected"),
|
||||||
|
[
|
||||||
|
(0, 0, 0, "000000"),
|
||||||
|
(1, 2, 3, "010203"),
|
||||||
|
(255, 255, 255, "FFFFFF"),
|
||||||
|
(254, 253, 252, "FEFDFC"),
|
||||||
|
(-20, 275, 125, "00FF7D"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_rgb_2_hex(r, g, b, expected):
|
||||||
|
assert rgb(r, g, b) == expected
|
||||||
21
tests/tests_tutorial/shop/test_pricing.py
Normal file
21
tests/tests_tutorial/shop/test_pricing.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
from src.tutorial.testing.shop.pricing import discount_price
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def test_discount_price_reduces_price():
|
||||||
|
result = discount_price(100.0, 20.0)
|
||||||
|
assert result == 80
|
||||||
|
|
||||||
|
assert discount_price(100.0, 50.0) == 50
|
||||||
|
assert discount_price(100.0, 0) == 100
|
||||||
|
assert discount_price(100.0, 100) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_negative_orice_raises_value_error() -> None:
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
discount_price(-100.0, 50.0) is ValueError
|
||||||
|
|
||||||
|
|
||||||
|
def test_percent_above_hundered_message() -> None:
|
||||||
|
with pytest.raises(ValueError, match="between 0 and 100"):
|
||||||
|
discount_price(100.0, 150.0)
|
||||||
24
tests/tests_tutorial/test_catching_exeptions.py
Normal file
24
tests/tests_tutorial/test_catching_exeptions.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
from src.tutorial.testing.practice.catching_exeptions import double_integers
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("input", "expected"),
|
||||||
|
[
|
||||||
|
([1, 2, 3], [2, 4, 6]),
|
||||||
|
([5], [10]),
|
||||||
|
([100, 900, -2], [200, 1800, -4]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_double_integers(input, expected):
|
||||||
|
assert double_integers(input) == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_argument_is_list():
|
||||||
|
with pytest.raises(TypeError, match="data must be a list"):
|
||||||
|
double_integers(4)
|
||||||
|
|
||||||
|
|
||||||
|
def test_list_with_integers_only():
|
||||||
|
with pytest.raises(TypeError, match="data may contain only integers"):
|
||||||
|
double_integers([4, 3, 2, "str"])
|
||||||
24
tests/tests_tutorial/test_kata_list_filtering.py
Normal file
24
tests/tests_tutorial/test_kata_list_filtering.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
from src.tutorial.testing.practice.kata_list_filtering import filter_list
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("input", "expected"),
|
||||||
|
[
|
||||||
|
([3, 5, "m", "0", 7], [3, 5, 7]),
|
||||||
|
(["3", "5", "m", "0", "f"], []),
|
||||||
|
([1, 2, 3], [1, 2, 3]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_filter_list(input, expected):
|
||||||
|
assert filter_list(input) == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_parameter_is_list():
|
||||||
|
input = [7, 9, 0]
|
||||||
|
result = isinstance(input, list)
|
||||||
|
assert result
|
||||||
|
|
||||||
|
input = 9
|
||||||
|
result = isinstance(input, list)
|
||||||
|
assert not result
|
||||||
20
tests/tests_tutorial/test_pandas_filter_rows.py
Normal file
20
tests/tests_tutorial/test_pandas_filter_rows.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
from src.tutorial.testing.practice.pandas_filter_rows import filter_dataframe
|
||||||
|
import pytest
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def standard_df():
|
||||||
|
df = pd.DataFrame(
|
||||||
|
{
|
||||||
|
"A": [1, 2, 3, 4, 5],
|
||||||
|
"B": [0, 2, 2, 4, 5],
|
||||||
|
"C": [1, 2, 3, 8, 5],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return df
|
||||||
|
|
||||||
|
|
||||||
|
def test_col_not_in_dataframe(standard_df):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
filter_dataframe(standard_df, "Z", lambda x: x >= 4)
|
||||||
Loading…
x
Reference in New Issue
Block a user