-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathelement_helpers.py
132 lines (79 loc) · 2.27 KB
/
element_helpers.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import itertools
from typing import Union, Callable, Iterable
from helper import Number
def add(a: Number, b: Number):
return a + b
def subtract(a: Number, b: Number):
return a - b
def multiply(a: Number, b: Number):
return a * b
def divide(a: Number, b: Number):
if b == 0:
return float(
f"{'-' if a < 0 else ''}Infinity"
) # Although negative numbers are not supported (yet)
return a / b
def concat(a: str, b: str):
"""Concat two strings"""
return a + b
def interleave(a: list, b: list):
"""Interleave a and b (two iterables)"""
b_cycle = itertools.cycle(b)
convert_func = "".join if isinstance(a, str) else type(a)
return convert_func(itertools.chain.from_iterable(zip(a, b_cycle)))
def print_with_newline(a: Union[str, Callable]):
"""Format a as a string with newline, a:str"""
if callable(a):
return str(a()) # For constants (which are stored as lambdas)
return str(a) + "\n"
def power(a: Number, b: Number):
"""Take the power of a and b, a:int, b:int"""
# fmt: off
return a ** b
# fmt: on
def joinab(a: Iterable, b: str):
"""b.join(a), a:Iterable[Any, Not[Int]], b:str"""
return b.join(a)
def sumdigits(a: int):
"""Sum of the digits of an integer"""
return sum(map(int, str(a)))
def sumascii(a: str):
"""Convert a to code points, then sum"""
return sum(bytes(a, encoding="utf-8"))
def general_input():
inp = input("> ")
if inp.isdigit():
return int(inp)
elif inp.replace(".", "", 1).isdigit():
return float(inp)
return inp
def repeat(a: str, b: int):
return a * b
def mod(a: Number, b: Number):
return a % b
def addascii(a: Number, b: str):
return a + sum(map(ord, b))
def removechars(a: str, b: str):
s = ""
for char in a:
if char not in b:
s += char
return s
def uppercase(a: str):
return a.upper()
def lowercase(a: str):
return a.lower()
def swapcase(a: str):
return a.swapcase()
def negate(a: int):
return -a
def index(a: str, b: int):
return a[b]
def slice(a: str, b: int, c: int):
return a[b:c]
def head(a: str, b: int):
return a[:b]
def tail(a: str, b: int):
return a[b:]
def halvestr(a: str):
return a[:len(a) // 2]