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
|
#!/usr/bin/env python
def comma_join(arr):
return ', '.join(map(lambda i: str(i), arr))
def specialElementIndices(n):
return list( filter(lambda x: n % x == 0, range(1, n+1)) )
def english_list (strlist):
# given a list, join it in a way that makes sense
# to english speakers
last = strlist.pop(-1) # last element in array
if (len(strlist) == 0):
# using an array in a scalar context returns
# the number of elements in the array
# there was only one element in the list
return last
joined = ',\n'.join(strlist)
if (len(strlist) > 1):
# if there's more than element, add an Oxford comma
joined += ','
return f'{joined} and\n{last}'
def specialNumberSquareSum(ints):
n = len(ints)
# find the list of indices for "special" numbers
specialIndices = specialElementIndices(n)
count = len(specialIndices)
explain_list = [
f"$ints[{x}] since {x} divides {n}"
for x in specialIndices
]
explain = (
"There are exactly $count special elements " +
"in the given array:\n" + english_list(explain_list)
)
# find the special numbers themselves
special = [ ints[x - 1] for x in specialIndices ]
# find the sum of the squares
sumval = sum([ x ** 2 for x in special ])
explain += '\nHence, the sum of the squares of all special '
explain += 'elements of given array:\n'
explain += ' + '.join(map(lambda x: f'{x} * {x}', special))
explain += f' = {sumval}'
return (
sumval,
explain
)
def solution(ints):
print(f'Input: @ints = ({comma_join(ints)})')
(sumval, explain) = specialNumberSquareSum(ints)
print(f'Output: {sumval}')
print('')
print(explain)
print('Example 1:')
solution([1, 2, 3, 4])
print('\nExample 2:')
solution([2, 7, 1, 19, 18, 3])
|