first commit
This commit is contained in:
Vendored
BIN
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,21 @@
|
||||
from task15 import hello
|
||||
import pytest
|
||||
|
||||
# task 1
|
||||
@pytest.mark.parametrize(
|
||||
"arg,res",
|
||||
[
|
||||
('', 'Hello!'),
|
||||
('Masha', 'Hello, Masha!'),
|
||||
(' ', 'Hello, !'),
|
||||
('r', 'Hello, r!'),
|
||||
('123', 'Hello, 123!'),
|
||||
('I love machine learning', 'Hello, I love machine learning!')
|
||||
]
|
||||
)
|
||||
def test_one_argument(arg, res):
|
||||
assert hello(arg) == res
|
||||
|
||||
|
||||
def test_no_arguments():
|
||||
assert hello() == 'Hello!'
|
||||
Binary file not shown.
@@ -0,0 +1,36 @@
|
||||
from task15 import int_to_roman
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"num,ans",
|
||||
[
|
||||
[1, 'I'],
|
||||
[2, 'II'],
|
||||
[3, 'III'],
|
||||
[4, 'IV'],
|
||||
[5, 'V'],
|
||||
[6, 'VI'],
|
||||
[7, 'VII'],
|
||||
[9, "IX"],
|
||||
[10, 'X'],
|
||||
[20, 'XX'],
|
||||
[50, 'L'],
|
||||
[54, 'LIV'],
|
||||
[90, "XC"],
|
||||
[100, 'C'],
|
||||
[199, "CXCIX"],
|
||||
[328, "CCCXXVIII"],
|
||||
[400, "CD"],
|
||||
[500, 'D'],
|
||||
[754, "DCCLIV"],
|
||||
[888, "DCCCLXXXVIII"],
|
||||
[973, "CMLXXIII"],
|
||||
[1000, 'M'],
|
||||
[1996, 'MCMXCVI'],
|
||||
[2143, "MMCXLIII"]
|
||||
]
|
||||
)
|
||||
def test_int_to_roman(num, ans):
|
||||
assert int_to_roman(num) == ans
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,22 @@
|
||||
from task15 import longest_common_prefix
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"arg,res",
|
||||
[
|
||||
[["flower","flow","flight"], "fl"],
|
||||
[[" flower"," flow"," flight", "flight "], "fl"],
|
||||
[["dog","racecar","car"], ""],
|
||||
[["c","cc","ccc"], "c"],
|
||||
[[""," "," "], ""],
|
||||
[[" "," "," "], ""],
|
||||
[["123"," 1 23","12 3"], "1"],
|
||||
[["1" for _ in range(100)], "1"],
|
||||
[["23" + str(i) for i in range(100)], "23"],
|
||||
[[" ML;", "\t\t\tML", "\n \tML"], "ML"],
|
||||
[[], ""]
|
||||
]
|
||||
)
|
||||
def test_prefix(arg, res):
|
||||
assert longest_common_prefix(arg) == res
|
||||
Binary file not shown.
@@ -0,0 +1,34 @@
|
||||
from task15 import BankCard
|
||||
import pytest
|
||||
|
||||
# task 4
|
||||
def test_bank_card():
|
||||
a = BankCard(100, 2)
|
||||
assert a.total_sum == 100
|
||||
assert a.balance_limit == 2
|
||||
assert a.__str__() == "To learn the balance call balance."
|
||||
a(50)
|
||||
assert a.total_sum == 50
|
||||
assert a.balance == 50
|
||||
assert a.balance_limit == 1
|
||||
try:
|
||||
a(50)
|
||||
except ValueError:
|
||||
pass
|
||||
assert a.total_sum == 0
|
||||
a.put(30)
|
||||
assert a.balance == 30
|
||||
try:
|
||||
a.balance
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
b = BankCard(50)
|
||||
|
||||
for i in range(100):
|
||||
assert b.balance == 50
|
||||
|
||||
c = BankCard(300, 2)
|
||||
d = a + c
|
||||
assert d.total_sum == 330
|
||||
assert d.balance_limit == 2
|
||||
Binary file not shown.
@@ -0,0 +1,16 @@
|
||||
from task15 import primes
|
||||
import itertools
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"arg,res",
|
||||
[
|
||||
[list(itertools.takewhile(lambda x : x <= 31, primes())), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]],
|
||||
[list(itertools.takewhile(lambda x : x <= 35, primes())), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]],
|
||||
[list(itertools.takewhile(lambda x : x <= 1, primes())), []],
|
||||
[list(itertools.takewhile(lambda x : x <= 700, primes())), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691]]
|
||||
]
|
||||
)
|
||||
def test_one_argument(arg, res):
|
||||
assert arg == res
|
||||
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from json import load, dumps
|
||||
from glob import glob
|
||||
from os import environ
|
||||
from os.path import join
|
||||
from sys import argv, exit
|
||||
|
||||
|
||||
def run_single_test(data_dir, output_dir):
|
||||
from pytest import main
|
||||
exit(main(['-vv', '-p', 'no:cacheprovider', join(data_dir, 'test.py')]))
|
||||
|
||||
|
||||
def check_test(data_dir):
|
||||
pass
|
||||
|
||||
|
||||
def grade(data_path):
|
||||
results = load(open(join(data_path, 'results.json')))
|
||||
max_mark = 5
|
||||
grade_mapping = [1, 1, 1, 1, 1]
|
||||
total_grade = 0
|
||||
ok_count = 0
|
||||
for result, grade in zip(results, grade_mapping):
|
||||
if result['status'] == 'Ok':
|
||||
total_grade += grade
|
||||
ok_count += 1
|
||||
total_count = len(results)
|
||||
description = '%02d/%02d' % (ok_count, total_count)
|
||||
mark = total_grade / sum(grade_mapping) * max_mark
|
||||
res = {'description': description, 'mark': mark}
|
||||
if environ.get('CHECKER'):
|
||||
print(dumps(res))
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if environ.get('CHECKER'):
|
||||
# Script is running in testing system
|
||||
if len(argv) != 4:
|
||||
print('Usage: %s mode data_dir output_dir' % argv[0])
|
||||
exit(0)
|
||||
|
||||
mode = argv[1]
|
||||
data_dir = argv[2]
|
||||
output_dir = argv[3]
|
||||
|
||||
if mode == 'run_single_test':
|
||||
run_single_test(data_dir, output_dir)
|
||||
elif mode == 'check_test':
|
||||
check_test(data_dir)
|
||||
elif mode == 'grade':
|
||||
grade(data_dir)
|
||||
else:
|
||||
# Script is running locally
|
||||
if len(argv) != 3:
|
||||
print(f'Usage: {argv[0]} test/unittest test_name')
|
||||
exit(0)
|
||||
|
||||
mode = argv[1]
|
||||
test_name = argv[2]
|
||||
test_dir = glob(f'python_intro_public_test/[0-9][0-9]_{mode}_{test_name}_input')
|
||||
if not test_dir:
|
||||
print('Test not found')
|
||||
exit(0)
|
||||
|
||||
from pytest import main
|
||||
exit(main(['-vv', join(test_dir[0], 'test.py')]))
|
||||
@@ -0,0 +1,112 @@
|
||||
def hello(x=None) -> str:
|
||||
return f"Hello{', ' + str(x) if x else ''}!"
|
||||
|
||||
|
||||
def int_to_roman(x: int) -> str:
|
||||
int_roman = [
|
||||
(1000, "M"),
|
||||
(900, "CM"),
|
||||
(500, "D"),
|
||||
(400, "CD"),
|
||||
(100, "C"),
|
||||
(90, "XC"),
|
||||
(50, "L"),
|
||||
(40, "XL"),
|
||||
(10, "X"),
|
||||
(9, "IX"),
|
||||
(5, "V"),
|
||||
(4, "IV"),
|
||||
(1, "I"),
|
||||
]
|
||||
|
||||
res = ""
|
||||
for n, rom in int_roman:
|
||||
while x >= n:
|
||||
res += rom
|
||||
x -= n
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def longest_common_prefix(x: list[str]) -> str:
|
||||
if not x or not x[0]:
|
||||
return ""
|
||||
|
||||
prefix = x[0].strip()
|
||||
break_flag = False
|
||||
|
||||
i = 0
|
||||
while i < len(prefix):
|
||||
for s in x:
|
||||
s = s.strip()
|
||||
|
||||
if i >= len(s) or s[i] != prefix[i]:
|
||||
break_flag = True
|
||||
|
||||
break
|
||||
|
||||
if break_flag:
|
||||
break
|
||||
|
||||
i += 1
|
||||
|
||||
return prefix[:i]
|
||||
|
||||
|
||||
class BankCard:
|
||||
def __init__(self, total_sum: int, balance_limit: int = -1) -> None:
|
||||
self.total_sum = total_sum
|
||||
self.balance_limit = balance_limit
|
||||
|
||||
def put(self, sum_put: int):
|
||||
self.total_sum += sum_put
|
||||
print(f"You put {sum_put} dollars.")
|
||||
return self
|
||||
|
||||
@property
|
||||
def balance(self) -> int:
|
||||
if self.balance_limit == 0:
|
||||
raise ValueError("Balance check limits exceeded.")
|
||||
|
||||
self.balance_limit -= 1
|
||||
return self.total_sum
|
||||
|
||||
def __add__(self, other):
|
||||
return type(self)(
|
||||
self.total_sum + other.total_sum,
|
||||
(
|
||||
max(self.balance_limit, other.balance_limit)
|
||||
if self.balance_limit != -1 and other.balance_limit != -1
|
||||
else -1
|
||||
),
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "To learn the balance call balance."
|
||||
|
||||
def __call__(self, sum_spent: int) -> None:
|
||||
if self.total_sum < sum_spent:
|
||||
raise ValueError(f"Not enough money to spend {sum_spent} dollars.")
|
||||
|
||||
print(f"You spent {sum_spent} dollars")
|
||||
self.total_sum -= sum_spent
|
||||
|
||||
return
|
||||
|
||||
|
||||
def primes():
|
||||
num = 2
|
||||
|
||||
while True:
|
||||
prime = True
|
||||
|
||||
for i in range(2, int(num**0.5) + 1):
|
||||
if not num % i:
|
||||
prime = False
|
||||
|
||||
break
|
||||
|
||||
if prime:
|
||||
yield num
|
||||
|
||||
num += 1
|
||||
@@ -0,0 +1,115 @@
|
||||
def hello(x=None) -> str:
|
||||
s = "Hello"
|
||||
if x:
|
||||
s += f", {x}"
|
||||
|
||||
return s + "!"
|
||||
|
||||
|
||||
def int_to_roman(x: int) -> str:
|
||||
match = [
|
||||
(1000, "M"),
|
||||
(900, "CM"),
|
||||
(500, "D"),
|
||||
(400, "CD"),
|
||||
(100, "C"),
|
||||
(90, "XC"),
|
||||
(50, "L"),
|
||||
(40, "XL"),
|
||||
(10, "X"),
|
||||
(9, "IX"),
|
||||
(5, "V"),
|
||||
(4, "IV"),
|
||||
(1, "I"),
|
||||
]
|
||||
|
||||
lst = []
|
||||
for num, text in match:
|
||||
while x >= num:
|
||||
x -= num
|
||||
lst.append(text)
|
||||
|
||||
return "".join(lst)
|
||||
|
||||
|
||||
def longest_common_prefix(x: list[str]) -> str:
|
||||
if not x:
|
||||
return ""
|
||||
|
||||
s = x[0].strip()
|
||||
ind = 0
|
||||
flag = False
|
||||
|
||||
while ind < len(s):
|
||||
for word in x:
|
||||
word = word.strip()
|
||||
if len(word) <= ind or word[ind] != s[ind]:
|
||||
flag = True
|
||||
break
|
||||
if flag:
|
||||
break
|
||||
|
||||
ind += 1
|
||||
|
||||
if ind == -1:
|
||||
return ""
|
||||
|
||||
return s[:ind]
|
||||
|
||||
|
||||
class BankCard:
|
||||
def __init__(self, total_sum: int, balance_limit: int = -1) -> None:
|
||||
self.total_sum = total_sum
|
||||
self.balance_limit = balance_limit
|
||||
|
||||
@property
|
||||
def balance(self) -> int:
|
||||
if self.balance_limit == 0:
|
||||
raise ValueError("Balance check limits exceeded.")
|
||||
|
||||
self.balance_limit -= 1
|
||||
|
||||
return self.total_sum
|
||||
|
||||
def put(self, sum_put: int):
|
||||
self.total_sum += sum_put
|
||||
print(f"You put {sum_put} dollars.")
|
||||
|
||||
return self
|
||||
|
||||
def __add__(self, other):
|
||||
if self.balance_limit == -1 or other.balance_limit == -1:
|
||||
new_balance_limit = -1
|
||||
else:
|
||||
new_balance_limit = max(self.balance_limit, other.balance_limit)
|
||||
|
||||
return type(self)(self.total_sum + other.total_sum, new_balance_limit)
|
||||
|
||||
def __call__(self, sum_spent: int) -> None:
|
||||
if self.total_sum < sum_spent:
|
||||
raise ValueError(f"Not enough money to spend sum_spent dollars.")
|
||||
|
||||
self.total_sum -= sum_spent
|
||||
print(f"You spent {sum_spent} dollars")
|
||||
|
||||
return
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "To learn the balance call balance."
|
||||
|
||||
|
||||
def primes():
|
||||
pr = 2
|
||||
|
||||
while True:
|
||||
flag = False
|
||||
|
||||
for n in range(2, int(pr**0.5) + 1):
|
||||
if pr % n == 0:
|
||||
flag = True
|
||||
break
|
||||
|
||||
if not flag:
|
||||
yield pr
|
||||
|
||||
pr += 1
|
||||
Reference in New Issue
Block a user