morse_clock   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A checkio() 0 6 2
A to_binary() 0 15 1
1
def to_binary(int_digit, length=4):
2
    """Convert a digit into binary string.
3
4
    Arguments:
5
        int_digit {str} -- the digit needed to be convert
6
7
    Keyword Arguments:
8
        length {int} -- length of converted string (default: {4})
9
10
    Returns:
11
        str -- a string with specific length converted from int
12
13
    """
14
    format_str = '{:0>%ds}' % length
15
    return format_str.format(bin(int(int_digit))[2:])
16
17
18
def checkio(data):
19
    data = ['{:0>2s}'.format(i) for i in data.split(':')]
20
    bin_data = [[to_binary(i[0], 3), to_binary(i[1])] for i in data]
21
    bin_data = list(map(lambda x: ' '.join(x), bin_data))
22
    bin_data = ' : '.join(bin_data)
23
    return bin_data.replace('0', '.').replace('1', '-')[1:]
24
25
26
# These "asserts" using only for self-checking
27
# and not necessary for auto-testing
28
if __name__ == '__main__':
29
    assert checkio("10:37:49") == ".- .... : .-- .--- : -.. -..-", "First Test"
30
    assert checkio("21:34:56") == "-. ...- : .-- .-.. : -.- .--.", "Second Test"
31
    assert checkio("11:10:12") == ".- ...- : ..- .... : ..- ..-.", "Third Test"
32
    assert checkio("23:59:59") == "-. ..-- : -.- -..- : -.- -..-", "Fourth Test"
33