Skip to content

Commit 3e36485

Browse files
Kaamkiyarzuckerm
andauthored
Add new project: base64 encode decode (#4523)
--------- Co-authored-by: rzuckerm <[email protected]>
1 parent 590a008 commit 3e36485

File tree

6 files changed

+137
-7
lines changed

6 files changed

+137
-7
lines changed

.glotter.yml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,94 @@ projects:
3333
- " *"
3434
transformations:
3535
- "splitlines"
36+
base64_encode_decode:
37+
words:
38+
- "base64"
39+
- "encode"
40+
- "decode"
41+
requires_parameters: true
42+
tests:
43+
base64_encode_valid:
44+
params:
45+
- name: "lowercase string"
46+
input: '"encode" "hello world"'
47+
expected: "aGVsbG8gd29ybGQ="
48+
- name: "long string"
49+
input: '"encode" "They swam along the boat at incredible speeds."'
50+
expected: "VGhleSBzd2FtIGFsb25nIHRoZSBib2F0IGF0IGluY3JlZGlibGUgc3BlZWRzLg=="
51+
- name: "numbers"
52+
input: '"encode" "1234567890"'
53+
expected: "MTIzNDU2Nzg5MA=="
54+
- name: "symbols"
55+
input: |
56+
"encode" "x!\\"#$%&'()*+,-./:;<=>?@[\\\\]^_`{|}~"
57+
expected: "eCEiIyQlJicoKSorLC0uLzo7PD0+P0BbXF1eX2B7fH1+"
58+
- name: "all base64 characters"
59+
input: '"encode" "! }gggIIT55;qqs!!Gjjb??=~~2$$+;;i::x..4kk,ppnoo"'
60+
expected: "ISAgfWdnZ0lJVDU1O3FxcyEhR2pqYj8/PX5+MiQkKzs7aTo6eC4uNGtrLHBwbm9v"
61+
transformations:
62+
- "strip"
63+
base64_decode_valid:
64+
params:
65+
- name: "lowercase string"
66+
input: '"decode" "aGVsbG8gd29ybGQ="'
67+
expected: "hello world"
68+
- name: "long string"
69+
input: '"decode" "VGhleSBzd2FtIGFsb25nIHRoZSBib2F0IGF0IGluY3JlZGlibGUgc3BlZWRzLg=="'
70+
expected: "They swam along the boat at incredible speeds."
71+
- name: "numbers"
72+
input: '"decode" "MTIzNDU2Nzg5MA=="'
73+
expected: "1234567890"
74+
- name: "symbols"
75+
input: '"decode" "eCEiIyQlJicoKSorLC0uLzo7PD0+P0BbXF1eX2B7fH1+"'
76+
expected: "x!\"#$%&'()*+,-./:;<=>?@[\\\\]^_`{|}~"
77+
- name: "all base64 characters"
78+
input: '"decode" "ISAgfWdnZ0lJVDU1O3FxcyEhR2pqYj8/PX5+MiQkKzs7aTo6eC4uNGtrLHBwbm9v"'
79+
expected: "! }gggIIT55;qqs!!Gjjb??=~~2$$+;;i::x..4kk,ppnoo"
80+
transformations:
81+
- "strip"
82+
base64_invalid_inputs:
83+
params:
84+
- name: "no input"
85+
input: null
86+
expected: "Usage: please provide a mode and a string to encode/decode"
87+
- name: "invalid mode"
88+
input: '"blue" "Oh look a Pascal triangle"'
89+
expected: "Usage: please provide a mode and a string to encode/decode"
90+
transformations:
91+
- "strip"
92+
base64_invalid_encode:
93+
params:
94+
- name: "missing string"
95+
input: '"encode"'
96+
expected: "Usage: please provide a mode and a string to encode/decode"
97+
- name: "empty string"
98+
input: '"encode" ""'
99+
expected: "Usage: please provide a mode and a string to encode/decode"
100+
transformations:
101+
- "strip"
102+
base64_invalid_decode:
103+
params:
104+
- name: "missing string"
105+
input: '"decode"'
106+
expected: "Usage: please provide a mode and a string to encode/decode"
107+
- name: "empty string"
108+
input: '"decode" ""'
109+
expected: "Usage: please provide a mode and a string to encode/decode"
110+
- name: "length number not multiple of 4"
111+
input: '"decode" "hello+world"'
112+
expected: "Usage: please provide a mode and a string to encode/decode"
113+
- name: "invalid characters"
114+
input: '"decode" "hello world="'
115+
expected: "Usage: please provide a mode and a string to encode/decode"
116+
- name: "too many pad characters at end"
117+
input: '"decode" "MTIzNDU2Nzg5M==="'
118+
expected: "Usage: please provide a mode and a string to encode/decode"
119+
- name: "pad characters in middle"
120+
input: '"decode" "MTIzNDU2=Nzg5M=="'
121+
expected: "Usage: please provide a mode and a string to encode/decode"
122+
transformations:
123+
- "strip"
36124
binarysearch:
37125
words:
38126
- "binary"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import base64
2+
import binascii
3+
import sys
4+
from typing import NoReturn
5+
6+
7+
def usage() -> NoReturn:
8+
print("Usage: please provide a mode and a string to encode/decode")
9+
sys.exit(1)
10+
11+
12+
def base64_encode(s: str) -> str:
13+
return base64.b64encode(s.encode("ascii")).decode("ascii")
14+
15+
16+
def base64_decode(s: str) -> str:
17+
return base64.b64decode(s.encode("ascii")).decode("ascii")
18+
19+
20+
def main() -> None | NoReturn:
21+
if len(sys.argv) < 3 or not sys.argv[2]:
22+
usage()
23+
24+
mode = sys.argv[1]
25+
s = sys.argv[2]
26+
if mode == "encode":
27+
result = base64_encode(s)
28+
elif mode == "decode":
29+
try:
30+
result = base64_decode(s)
31+
except binascii.Error:
32+
usage()
33+
else:
34+
usage()
35+
36+
print(result)
37+
38+
39+
if __name__ == "__main__":
40+
main()

archive/p/python/prime_number.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
def is_prime(x):
6-
if (x % 2 == 0 and x is not 2) or (x == 1):
6+
if (x % 2 == 0 and x != 2) or (x == 1):
77
return False
88
return not bool([n for n in range(3, int(ceil(sqrt(x))+1)) if x % n == 0])
99

archive/p/python/testinfo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ folder:
44

55
container:
66
image: "python"
7-
tag: "3.7-alpine"
7+
tag: "3.12-alpine"
88
cmd: "python {{ source.name }}{{ source.extension }}"

poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MIT"
88

99
dependencies = [
1010
"ronbun (>=0.9.0,<0.10.0)",
11-
"glotter2 (>=0.10.1,<0.11.0)"
11+
"glotter2 (>=0.10.2,<0.11.0)"
1212
]
1313

1414
# Initiator of the collection
@@ -32,5 +32,7 @@ maintainers = [
3232
[tool.poetry]
3333
package-mode = false
3434

35+
[tool.poetry.dependencies]
36+
glotter2 = {develop = true}
3537
[tool.pytest.ini_options]
3638
console_output_style = "count"

0 commit comments

Comments
 (0)