-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFind_Product_Partitions.py
57 lines (50 loc) · 1.38 KB
/
Find_Product_Partitions.py
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
# Kata link:
# https://www.codewars.com/kata/571dd22c2b97f2ce400010d4/
# -------------------------------------
# Solution:
from itertools import permutations
def part(n):
par = []
prime = 2
while prime * prime <= n:
while n % prime == 0:
par.append(prime)
n //= prime
prime += 1
if n > 1:
par.append(n)
return par[::-1]
def numb(ar):
res = {sum(tp ** ar.count(tp) for tp in set(ar)) * len(ar): ar}
contr = 2
while contr < len(ar):
for i in permutations(ar, r=contr):
nu = 1
temp = ar[:]
for j in i:
nu *= j
temp.remove(j)
prod_part = [nu, * temp]
prod_part.sort(reverse=True)
score = 0
for t in set(prod_part):
score += t ** prod_part.count(t)
score *= len(prod_part)
res[score] = prod_part
contr += 1
return res
def find_spec_prod_part(n, com):
partt = part(n)
if len(partt) == 1:
return "It is a prime number"
result = numb(partt)
elem = result.copy()
for r, s in elem.items():
for it, st in numb(s).items():
result[it] = st
if com == "max":
maxP = max(result.keys())
return [result[maxP], maxP]
else:
minP = min(result.keys())
return [result[minP], minP]