Compare commits
No commits in common. "main" and "tutorial" have entirely different histories.
@ -15,7 +15,7 @@ Repository for CDS-2020 Programming and Promt Engineering II
|
||||
|build|Build-System, Dependencies|build: update requirements.txt|
|
||||
|
||||
# Codewars
|
||||
|Title|Source (src/codewars/)|Test (tests/test_codewars/)|URL|
|
||||
|Title|Source (src/codewars/)|Test (test/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)|
|
||||
|The Lamp: Revisited|kata_the_lamp.py|test_the_lamp.py|[570e6e32de4dc8a8340016dd](https://www.codewars.com/kata/570e6e32de4dc8a8340016dd)|
|
||||
@ -31,9 +31,3 @@ 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)|
|
||||
|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)|
|
||||
|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)|
|
||||
|Simple Fun #128: Doubly Not Less|kata_doubly_not_less.py|test_doubly_not_less.py|[58a3ea0a8bdda5093800000e](https://www.codewars.com/kata/58a3ea0a8bdda5093800000e)|
|
||||
|Rot13|kata_rot13.py|test_rot13.py|[530e15517bc88ac656000716](https://www.codewars.com/kata/530e15517bc88ac656000716/)|
|
||||
|The Hashtag Generator|kata_the_hashtag_generator.py|test_the_hashtag_generator.py|[52449b062fb80683ec000024](https://www.codewars.com/kata/52449b062fb80683ec000024)|
|
||||
|Tic-Tac-Toe Checker|kata_tic_tac_toe_checker.py|test_tic_tac_toe_checker.py|[525caa5c1bf619d28c000335](https://www.codewars.com/kata/525caa5c1bf619d28c000335)|
|
||||
|
||||
@ -4,4 +4,3 @@ pytest == 9.0.2
|
||||
pre-commit == 4.5.1
|
||||
matplotlib == 3.10.8
|
||||
pandas == 3.0.1
|
||||
requests == 2.33.1
|
||||
|
||||
@ -1,11 +0,0 @@
|
||||
def doubly_not_less(n):
|
||||
n_int = int(n)
|
||||
|
||||
# function reverses the order of a string of numbers to int
|
||||
reverse = lambda x: int("".join([str(item) for item in [int(x) for x in x][::-1]]))
|
||||
|
||||
if reverse(n) >= n_int:
|
||||
return n
|
||||
return None
|
||||
|
||||
print(doubly_not_less("23456"))
|
||||
@ -1,64 +0,0 @@
|
||||
def top_3_words(text: str) -> list:
|
||||
"""Given a string of text (possibly with punctuation and line-breaks),
|
||||
returns an array of the top-3 most occurring words, in descending order
|
||||
of the number of occurrences.
|
||||
|
||||
Args:
|
||||
text (str): Some text
|
||||
|
||||
Returns:
|
||||
list: [most used word, second most used word, third most used word]
|
||||
"""
|
||||
# remove invalid characters and replace with whitespace
|
||||
text = "".join(
|
||||
c if c.isalpha() or c.isdigit() or c in ("'", " ") else " " for c in text
|
||||
)
|
||||
text = text.split()
|
||||
|
||||
# remove word if it's just made of the character '
|
||||
text = [w for w in text if w.strip("' ") != ""]
|
||||
|
||||
word_count = {}
|
||||
|
||||
for word in text:
|
||||
word = word.lower()
|
||||
if word in word_count:
|
||||
word_count[word] += 1
|
||||
else:
|
||||
word_count[word] = 1
|
||||
|
||||
top_3 = {
|
||||
"first": {"word": None, "amount": 0},
|
||||
"second": {"word": None, "amount": 0},
|
||||
"third": {"word": None, "amount": 0},
|
||||
}
|
||||
|
||||
for word in word_count:
|
||||
if word_count[word] > top_3["first"]["amount"]:
|
||||
top_3["third"]["word"] = top_3["second"]["word"]
|
||||
top_3["third"]["amount"] = top_3["second"]["amount"]
|
||||
|
||||
top_3["second"]["word"] = top_3["first"]["word"]
|
||||
top_3["second"]["amount"] = top_3["first"]["amount"]
|
||||
|
||||
top_3["first"]["word"] = word
|
||||
top_3["first"]["amount"] = word_count[word]
|
||||
elif word_count[word] > top_3["second"]["amount"]:
|
||||
top_3["third"]["word"] = top_3["second"]["word"]
|
||||
top_3["third"]["amount"] = top_3["second"]["amount"]
|
||||
|
||||
top_3["second"]["word"] = word
|
||||
top_3["second"]["amount"] = word_count[word]
|
||||
elif word_count[word] > top_3["third"]["amount"]:
|
||||
top_3["third"]["word"] = word
|
||||
top_3["third"]["amount"] = word_count[word]
|
||||
|
||||
return [
|
||||
x
|
||||
for x in [
|
||||
top_3["first"]["word"],
|
||||
top_3["second"]["word"],
|
||||
top_3["third"]["word"],
|
||||
]
|
||||
if x is not None
|
||||
]
|
||||
@ -1,11 +0,0 @@
|
||||
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()
|
||||
@ -1,22 +0,0 @@
|
||||
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
|
||||
|
||||
|
||||
def rot13(s):
|
||||
cipher = []
|
||||
|
||||
for c in s:
|
||||
make_upper = c.isupper()
|
||||
c = c.lower()
|
||||
|
||||
if c.isalpha():
|
||||
i = (ALPHABET.index(c) + 13) % 26
|
||||
new_s = ALPHABET[i]
|
||||
|
||||
if make_upper:
|
||||
new_s = new_s.upper()
|
||||
|
||||
cipher.append(new_s)
|
||||
else:
|
||||
cipher.append(c)
|
||||
|
||||
return "".join(cipher)
|
||||
@ -1,19 +0,0 @@
|
||||
def generate_hashtag(s):
|
||||
if len(s) == 0:
|
||||
return False
|
||||
|
||||
words = s.split()
|
||||
hashtag = ["#"]
|
||||
|
||||
for word in words:
|
||||
word = word.lower()
|
||||
word = [x for x in word]
|
||||
word[0] = word[0].upper()
|
||||
word = "".join(word)
|
||||
hashtag.append(word)
|
||||
|
||||
s = "".join(hashtag)
|
||||
|
||||
if len(s) > 140:
|
||||
return False
|
||||
return s
|
||||
@ -1,29 +0,0 @@
|
||||
def is_solved(board):
|
||||
vertical_0 = [board[i][0] for i in range(len(board))]
|
||||
vertical_1 = [board[i][1] for i in range(len(board))]
|
||||
vertical_2 = [board[i][2] for i in range(len(board))]
|
||||
diagonal_l = [board[i][i] for i in range(len(board))]
|
||||
diagonal_r = [board[i][len(board) - 1 - i] for i in range(len(board))]
|
||||
|
||||
if len(set(board[0])) == 1 and 0 not in set(board[0]):
|
||||
return board[0][0]
|
||||
elif len(set(board[1])) == 1 and 0 not in set(board[1]):
|
||||
return board[1][0]
|
||||
elif len(set(board[2])) == 1 and 0 not in set(board[2]):
|
||||
return board[2][0]
|
||||
elif len(set(board[2])) == 1 and 0 not in set(board[2]):
|
||||
return board[2][0]
|
||||
elif len(set(vertical_0)) == 1 and 0 not in set(vertical_0):
|
||||
return board[0][0]
|
||||
elif len(set(vertical_1)) == 1 and 0 not in set(vertical_1):
|
||||
return board[1][1]
|
||||
elif len(set(vertical_2)) == 1 and 0 not in set(vertical_2):
|
||||
return board[2][2]
|
||||
elif len(set(diagonal_l)) == 1 and 0 not in set(diagonal_l):
|
||||
return board[0][0]
|
||||
elif len(set(diagonal_r)) == 1 and 0 not in set(diagonal_r):
|
||||
return board[0][2]
|
||||
elif 0 in board[0] or 0 in board[1] or 0 in board[2]:
|
||||
return -1
|
||||
else:
|
||||
return 0
|
||||
@ -1,12 +0,0 @@
|
||||
import requests
|
||||
|
||||
|
||||
def fetch_user(username: str) -> dict:
|
||||
response = requests.get(
|
||||
f"https://www.codewars.com/api/v1/users/{username}", timeout=5.0
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
|
||||
print(fetch_user("cw-sandro"))
|
||||
@ -1,8 +0,0 @@
|
||||
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)]))
|
||||
@ -1,27 +0,0 @@
|
||||
from src.codewars.kata_doubly_not_less import doubly_not_less
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("input", "expected"),
|
||||
[
|
||||
("5314", "5316"),
|
||||
("23456", "23456"),
|
||||
("65432", "65437"),
|
||||
("9998786543", "9998788999"),
|
||||
("1", "1"),
|
||||
("1101", "1102"),
|
||||
("9" * 25, "9" * 25),
|
||||
(
|
||||
"99999999999999999999888888888888877777777777777777766666666666655555555555555555444444444444444",
|
||||
"99999999999999999999888888888888877777777777777777766666666666655555555555999999999999999999999",
|
||||
),
|
||||
(
|
||||
"569735847563478256235634859346578345634895736459346778",
|
||||
"569735847563478256235634859346578345634895736459346778",
|
||||
),
|
||||
("8998989898989889898989898989989899", "8998989898989889898989898989989899"),
|
||||
],
|
||||
)
|
||||
def test_doubly_not_less(input, expected):
|
||||
assert doubly_not_less(input) == expected
|
||||
@ -1,27 +0,0 @@
|
||||
from src.codewars.kata_most_words_used import top_3_words
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("input", "expected"),
|
||||
[
|
||||
("a a a b c c d d d d e e e e e", ["e", "d", "a"]),
|
||||
("e e e e DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e e e", ["e", "ddd", "aa"]),
|
||||
(" //wont won't won't ", ["won't", "wont"]),
|
||||
(" , e .. ", ["e"]),
|
||||
(" ... ", []),
|
||||
(" ' ", []),
|
||||
(" ''' ", []),
|
||||
(
|
||||
"""In a village of La Mancha, the name of which I have no desire to call to
|
||||
mind, there lived not long since one of those gentlemen that keep a lance
|
||||
in the lance-rack, an old buckler, a lean hack, and a greyhound for
|
||||
coursing. An olla of rather more beef than mutton, a salad on most
|
||||
nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra
|
||||
on Sundays, made away with three-quarters of his income.""",
|
||||
["a", "of", "on"],
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_top_3_words(input, expected):
|
||||
assert top_3_words(input) == expected
|
||||
@ -1,16 +0,0 @@
|
||||
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
|
||||
@ -1,14 +0,0 @@
|
||||
from src.codewars.kata_rot13 import rot13
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("s", "expected"),
|
||||
[
|
||||
("test", "grfg"),
|
||||
("Test", "Grfg"),
|
||||
("aA bB zZ 1234 *!?%", "nN oO mM 1234 *!?%"),
|
||||
],
|
||||
)
|
||||
def test_rot13(s, expected):
|
||||
assert rot13(s) == expected
|
||||
@ -1,24 +0,0 @@
|
||||
from src.codewars.kata_the_hashtag_generator import generate_hashtag
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("s", "expected"),
|
||||
[
|
||||
("Codewars", "#Codewars"),
|
||||
("Codewars ", "#Codewars"),
|
||||
(" Codewars", "#Codewars"),
|
||||
("Codewars Is Nice", "#CodewarsIsNice"),
|
||||
("codewars is nice", "#CodewarsIsNice"),
|
||||
("CoDeWaRs is niCe", "#CodewarsIsNice"),
|
||||
("c i n", "#CIN"),
|
||||
("codewars is nice", "#CodewarsIsNice"),
|
||||
("", False),
|
||||
(
|
||||
"Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong",
|
||||
False,
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_generate_hashtag(s, expected):
|
||||
assert generate_hashtag(s) == expected
|
||||
@ -1,18 +0,0 @@
|
||||
from src.codewars.kata_tic_tac_toe_checker import is_solved
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("board", "expected"),
|
||||
[
|
||||
([[0, 0, 1], [0, 1, 2], [2, 1, 0]], -1),
|
||||
([[1, 1, 1], [0, 2, 2], [0, 0, 0]], 1),
|
||||
([[2, 1, 2], [2, 1, 1], [1, 1, 2]], 1),
|
||||
([[2, 1, 2], [2, 1, 1], [1, 2, 1]], 0),
|
||||
([[1, 1, 1], [0, 2, 2], [0, 0, 0]], 1),
|
||||
([[2, 1, 1], [0, 1, 1], [2, 2, 2]], 2),
|
||||
([[1, 2, 1], [1, 1, 2], [2, 1, 2]], 0),
|
||||
],
|
||||
)
|
||||
def test_is_solved(board, expected):
|
||||
assert is_solved(board) == expected
|
||||
Loading…
x
Reference in New Issue
Block a user