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
| from random import seed, randint from collections import defaultdict import sys
def f(arg_for_seed, nb_of_elements, max_element): ''' >>> f(1, 12, 1) Here is L: [0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0] Here is R: [[1], [0]] >>> f(3, 15, 8) Here is L: [3, 8, 2, 5, 7, 1, 0, 7, 4, 8, 3, 3, 7, 8, 8] Here is R: [[8], [7], [5], [4], [3], [2], [1], [0]] >>> f(6, 12, 50) Here is L: [50, 36, 5, 31, 48, 16, 2, 0, 9, 42, 37, 30] Here is R: [[50, 30, 0], [36, 16], [42, 2], [9], [48], [37], [5], [31]] >>> f(9, 8, 5000) Here is L: [3792, 3058, 2188, 1134, 1524, 52, 2771, 4118] Here is R: [[3058, 2188, 4118], [1134, 1524], [3792, 52], [2771]] >>> f(12, 15, 30) Here is L: [15, 8, 21, 16, 21, 11, 4, 12, 0, 11, 15, 8, 20, 25, 14] Here is R: [[15, 25], [14, 4], [21, 11], [20, 0], [8], [16], [12]] >>> f(15, 13, 100) Here is L: [26, 1, 66, 94, 4, 20, 30, 2, 7, 87, 18, 88, 47] Here is R: [[87, 47, 7], [18, 88], [26, 66], [94, 4], [20, 30], [2], [1]] ''' if nb_of_elements < 1: sys.exit(0) seed(arg_for_seed) L = [randint(0, max_element) for _ in range(nb_of_elements)]
R = [] print(f'Here is L: {L}')
mapping = defaultdict(list)
for item in L: if item not in mapping[str(item)[-1]]: mapping[str(item)[-1]].append(item)
mapping = dict(sorted(mapping.items(), key=lambda x: (len(x[1]), x[0])))
for key, item in mapping.items(): mapping[key] = sorted(item, key=lambda x: (len(str(x))), reverse=True)
print(f'Here is R: {list(mapping.values())[::-1]}')
if __name__ == '__main__': import doctest
doctest.testmod()
|