-
Notifications
You must be signed in to change notification settings - Fork 1
/
050_Word to binary.py
69 lines (45 loc) · 1.56 KB
/
050_Word to binary.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
"""
Codewars Coding Challenge
Day 50/366
Level: 7kyu
Word to binary
Write a function that takes a string and returns an array containing binary numbers equivalent to the ASCII codes of the characters of the string. The binary strings should be eight digits long.
Example: 'man' should return [ '01101101', '01100001', '01101110' ]
def word_to_bin(word):
# code away!!!
return []
https://www.codewars.com/kata/59859f435f5d18ede7000050/train/python
"""
# My Solutions
def word_to_bin(word):
bin_list = []
for char in word:
ascii_code = ord(char)
bin_reps = bin(ascii_code)[2:].zfill(8)
bin_list.append(bin_reps)
return bin_list
print(word_to_bin("Ony"))
"""
Sample Tests
import codewars_test as test
from solution import word_to_bin
@test.describe("Fixed Tests")
def fixed_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
for word,exp in [ ('man', [ '01101101', '01100001', '01101110' ]),
('AB', ['01000001', '01000010']),
('wecking',[ '01110111', '01100101', '01100011', '01101011', '01101001', '01101110', '01100111' ] ),
('Ruby',[ '01010010', '01110101', '01100010', '01111001' ] ),
('Yosemite',[ '01011001', '01101111', '01110011', '01100101', '01101101', '01101001', '01110100', '01100101' ] ) ]:
test.assert_equals( word_to_bin(word), exp )
"""
"""
Perfefct Solutions From Codewars
=1=
def word_to_bin(word):
return ['{:08b}'.format(ord(c)) for c in word]
=2=
def word_to_bin(word):
return [ f"{ord(c):08b}" for c in word ]
"""