Sources

My solutions

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
import doctest
import pytest
from os import system


def digits(n: int) -> int:
    '''Return the number of digits in `n`.
    >>> digits(123)
    3
    '''
    return len(str(n))


def test_mytest_digits():
    assert digits(0) == 1


def test_provided_digits():
    assert digits(0) == 1
    assert digits(56) == 2
    assert digits(123) == 3
    assert digits(7154) == 4
    assert digits(61217311514) == 11


def sort_ascend(ns: list) -> list:
    '''Return a new list containing `ns` sorted ascending.
    >>> sort_ascend([3, 2, 1])
    [1, 2, 3]
    '''
    if not ns or len(ns) == 1:
        return ns
    return sorted(ns)


def test_sort_ascend_mytest():
    assert sort_ascend([]) == []
    assert sort_ascend([0]) == [0]
    assert sort_ascend([1, 0]) == [0, 1]
    assert sort_ascend([0, -1]) == [-1, 0]


def test_sort_ascend_provided():
    assert sort_ascend([]) == []
    assert sort_ascend([1, 2, 10, 50, 5]) == [1, 2, 5, 10, 50]
    assert sort_ascend([80, 29, 4, -95, -24, 85]) == \
        [-95, -24, 4, 29, 80, 85]


def mean(ns: list) -> float:
    '''Return the mean value of `ns`.
    >>> mean([1, 2, 3, 4])
    2.5
    '''
    return float(sum(ns)) / max(len(ns), 1)


def stats(ns: list) -> list:
    '''Return a list containing the min, max, len, mean.
    >>> stats([1, 2, 3, 4])
    [1, 4, 4, 2.5]
    '''
    if not ns:
        return ns
    return [min(ns), max(ns), len(ns), mean(ns)]


def test_stats_mytest():
    assert stats([]) == []


def test_stats_provided():
    assert stats([6, 9, 15, -2, 92, 11]) == [-2, 92, 6, 21.833333333333332]
    assert stats([30, 43, 20, 92, 3, 74]) == [3, 92, 6, 43.666666666666664]
    assert stats([4.54, 8.32, 5.20]) == [4.54, 8.32, 3, 6.02]


if __name__ == '__main__':
    pytest.main(['-q', __file__])
    doctest.testmod()
    system(f'mypy {__file__} --ignore-missing-imports')
    system(f'flake8 {__file__}')