aboutsummaryrefslogtreecommitdiff
path: root/wqflask/utility/chunks.py
blob: 9565fb960bc4cee3531ba48b85a3ce591246f636 (plain)
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
from __future__ import absolute_import, print_function, division

import math
import time


def divide_into_chunks(the_list, number_chunks):
    """Divides a list into approximately number_chunks smaller lists
    
    >>> divide_into_chunks([1, 2, 7, 3, 22, 8, 5, 22, 333], 3)
    [[1, 2, 7], [3, 22, 8], [5, 22, 333]]
    >>> divide_into_chunks([1, 2, 7, 3, 22, 8, 5, 22, 333], 4)                                                                                                                                                     
    [[1, 2, 7], [3, 22, 8], [5, 22, 333]]
    >>> divide_into_chunks([1, 2, 7, 3, 22, 8, 5, 22, 333], 5)                                                                                                                                                     
    [[1, 2], [7, 3], [22, 8], [5, 22], [333]]
    >>>
    
    """
    length = len(the_list)

    if length == 0:
        return [[]]
    
    if length <= number_chunks:
        number_chunks = length

    chunksize = int(math.ceil(length / number_chunks))

    chunks = []
    for counter in range(0, length, chunksize):
        chunks.append(the_list[counter:counter+chunksize])

    return chunks

def _confirm_chunk(original, result):
    all_chunked = []
    for chunk in result:
        all_chunked.extend(chunk)
    print("length of all chunked:", len(all_chunked))
    assert original == all_chunked, "You didn't chunk right"


def _chunk_test(divide_func):
    import random
    random.seed(7)

    number_exact = 0
    total_amount_off = 0

    for test in range(1, 1001):
        print("\n\ntest:", test)
        number_chunks = random.randint(1, 20)
        number_elements = random.randint(0, 100)
        the_list = list(range(1, number_elements))
        result = divide_func(the_list, number_chunks)

        print("Dividing list of length {} into approximately {} chunks - got {} chunks".format(
            len(the_list), number_chunks, len(result)))
        print("result:", result)

        _confirm_chunk(the_list, result)

        amount_off = abs(number_chunks - len(result))
        if amount_off == 0:
            number_exact += 1
        else:
            total_amount_off += amount_off


        print("\n{} exact out of {}    [Total amount off: {}]".format(number_exact,
                                                                      test,
                                                                      total_amount_off))
    assert number_exact == 558
    assert total_amount_off == 1580
    return number_exact, total_amount_off


def _main():
    info = dict()
    #funcs = (("sam", sam_divide_into_chunks), ("zach", zach_divide_into_chunks))
    funcs = (("only one", divide_into_chunks),)
    for name, func in funcs:
        start = time.time()
        number_exact, total_amount_off = _chunk_test(func)
        took = time.time() - start
        info[name] = dict(number_exact=number_exact,
                          total_amount_off=total_amount_off,
                          took=took)

    print("info is:", info)

if __name__ == '__main__':
    _main()
    print("\nConfirming doctests...")
    import doctest
    doctest.testmod()