Repository navigation
Expand file tree
/
Copy pathpython_basics.py
More file actions
77 lines (60 loc) · 1.97 KB
/
Copy pathpython_basics.py
File metadata and controls
77 lines (60 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Решения задач на базовый Python."""
from grader_contracts.python_basics import PositiveIntegerInput, TextInput, VectorPairInput
def count_vowels(data: TextInput) -> int:
text = data.value
return sum(1 for ch in text.lower() if ch in "aeiou")
def has_unique_characters(data: TextInput) -> bool:
text = data.value
return len(set(text)) == len(text)
def count_one_bits(data: PositiveIntegerInput) -> int:
number = data.value
return bin(number).count("1")
def multiplicative_persistence(data: PositiveIntegerInput) -> int:
number = data.value
steps = 0
while number >= 10:
product = 1
for digit in str(number):
product *= int(digit)
number = product
steps += 1
return steps
def mse(data: VectorPairInput) -> float:
predicted, expected = data.predicted, data.expected
if len(predicted) == 0:
return 0.0
total = sum((p - e) ** 2 for p, e in zip(predicted, expected))
return total / len(predicted)
def prime_factorization(data: PositiveIntegerInput) -> str:
number = data.value
parts = []
divisor = 2
while divisor * divisor <= number:
power = 0
while number % divisor == 0:
number //= divisor
power += 1
if power == 1:
parts.append(f"({divisor})")
elif power > 1:
parts.append(f"({divisor}**{power})")
divisor += 1
if number > 1:
parts.append(f"({number})")
return "".join(parts)
def pyramid(data: PositiveIntegerInput) -> int | str:
cube_count = data.value
total = 0
k = 0
while total < cube_count:
k += 1
total += k * k
if total == cube_count:
return k
return "It is impossible"
def is_balanced_number(data: PositiveIntegerInput) -> bool:
digits = str(data.value)
n = len(digits)
left = digits[: (n - 1) // 2]
right = digits[n // 2 + 1:]
return sum(map(int, left)) == sum(map(int, right))