There are some perfect squares with a particular property.
For example the number ```n = 256``` is a perfect square, its square root is ```16```. If we change the position of the digits of n, we may obtain another perfect square``` 625``` (square root = 25).
With these three digits ```2```,```5``` and ```6``` we can get two perfect squares: ```[256,625]```

The number ```1354896``` may generate another ```4``` perfect squares, having with the number itself, a total of five perfect squares: ```[1354896, 3594816, 3481956, 5391684, 6395841]```, being the last one in the list, ```6395841```, the highest value of the set.

Your task is to find the first perfect square above the given lower_limit, that can generate the given k number of perfect squares, and it doesn't contain the digit 0. Then return the maximum perfect square that can be obtained from its digits.

Example with the cases seen above:
```
lower_limit = 200
k = 2 (amount of perfect squares)
result = 625

lower_limit = 3550000
k = 5 (amount of perfect squares)
result  = 6395841
```
Features of the random tests:
```
100 <= lower_limit <= 1e6
2 <= k <= 5
number of tests = 45
```
Have a good time!

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Combinatorics

Data structures

Mathematics

Starter code: def next_perfectsq_perm(lower_limit, k):
	

Solutions:
from itertools import count, permutations

def next_perfectsq_perm(limit_below, k):
	for n in count(int(limit_below ** 0.5) + 1):
		s = str(n ** 2)
		if '0' not in s:
			sq_set = {x for x in (int(''.join(p)) for p in permutations(s)) if (x ** 0.5).is_integer()}
			if len(sq_set) == k:
				return max(sq_set)


from itertools import permutations
from math import sqrt

def is_per(n):
	return str(sqrt(int(n)))[-1] == '0'

def next_perfectsq_perm(lower_limit, k):
	perfect = [i ** 2 for i in range(2000) if '0' not in str(i ** 2) if i ** 2 > lower_limit]
	for i in perfect:
		if i > lower_limit:
			num = set()
			per = set(permutations(str(i)))
			tem = [int(''.join(j)) for j in per if is_per(''.join(j))]
			if len(tem) == k:
				num.add(i)
				ma = max(tem)
				break
	return max(tem)


from itertools import permutations

def next_perfectsq_perm(lower_limit, k):
	while 1:
		lower_limit += 1
		if '0' not in str(lower_limit) and int(lower_limit ** 0.5 + 0.5) ** 2 == lower_limit:
			perms = list([''.join(x) for x in set(permutations(str(lower_limit)))])
			perms = list(map(int, perms))
			perfects = 0
			perfect_list = []
			for i in perms:
				if int(i ** 0.5 + 0.5) ** 2 == i:
					perfects += 1
					perfect_list.append(i)
			if perfects == k:
				return max(perfect_list)


candidates = ((sq, str(sq)) for sq in (i * i for i in range(12, 3000)))
candidates = ((sq, s) for (sq, s) in candidates if '0' not in s)
d = {}
sqs = {}
for (sq, s) in candidates:
	sqs[sq] = d.setdefault(int(''.join(sorted(s))), [])
	sqs[sq].append(sq)

def next_perfectsq_perm(lower_limit, k):
	return sqs[min((x for x in sqs if x > lower_limit and len(sqs[x]) == k))][-1]


from itertools import permutations

def is_perfect(square):
	return round(square ** 0.5) ** 2 == square

def permuted(n):
	return (int(''.join(p)) for p in set(permutations(str(n))))

def perfect_permutations(n):
	return tuple((p for p in permuted(n) if is_perfect(p)))

def next_perfectsq_perm(lower_limit, k):
	root = int(lower_limit ** 0.5) + 1
	while True:
		square = root ** 2
		if not '0' in str(square):
			p = perfect_permutations(square)
			if len(p) == k:
				return max(p)
		root += 1


from collections import *
NUM_DIG = 8
palindromes = OrderedDict()
for n in range(int(10 ** (NUM_DIG / 2)) + 1):
	key = Counter(str(n ** 2))
	if '0' not in key:
		palindromes.setdefault(frozenset(key.most_common()), []).append(n ** 2)
(lasts, lengths) = ({}, defaultdict(list))
for seq in palindromes.values():
	for n in seq:
		lengths[len(seq)].append(n)
		lasts[n] = seq[-1]
for k in lengths:
	lengths[k].sort()
from bisect import bisect

def next_perfectsq_perm(lower_limit, k):
	return lasts[lengths[k][bisect(lengths[k], lower_limit)]]


K_PERMS = {2: [144, 256, 441, 625, 1369, 1764, 1936, 4761, 11236, 12769, 14884, 21316, 24649, 24964, 27556, 29584, 34596, 36864, 43264, 45369, 46656, 48841, 51984, 54289, 66564, 74529, 75625, 79524, 86436, 95481, 96721, 99856, 112225, 113569, 118336, 133956, 139876, 142884, 147456, 148225, 148996, 154449, 155236, 166464, 167281, 169744, 173889, 174724, 175561, 194481, 198916, 212521, 216225, 224676, 225625, 227529, 233289, 237169, 238144, 264196, 265225, 267289, 272484, 287296, 293764, 314721, 316969, 323761, 328329, 329476, 344569, 345744, 355216, 368449, 369664, 374544, 383161, 391876, 398161, 427716, 432964, 434281, 447561, 456976, 459684, 463761, 467856, 474721, 498436, 521284, 522729, 546121, 591361, 594441, 611524, 619369, 622521, 627264, 646416, 649636, 654481, 659344, 667489, 669124, 675684, 678976, 712336, 729316, 736164, 741321, 748225, 755161, 758641, 769129, 772641, 779689, 781456, 786769, 793881, 795664, 799236, 817216, 835396, 842724, 844561, 848241, 874225, 877969, 891136, 894916, 919681, 927369, 933156, 948676, 956484, 964324, 972196, 976144, 9114361], 3: [961, 9216, 11664, 12544, 16641, 18225, 23716, 25281, 32761, 41616, 42849, 44521, 49284, 52441, 72361, 81225, 82944, 131769, 141376, 149769, 164836, 165649, 197136, 319225, 349281, 364816, 378225, 381924, 417316, 471969, 481636, 541696, 837225, 931225, 974169, 7112889], 4: [81796, 112896, 132496, 168921, 195364, 214369, 269361, 298116, 346921, 395641, 436921, 543169, 962361, 4511376], 5: [43681, 6395841], 6: [7241481], 7: [4532641]}

def next_perfectsq_perm(lower_limit, k, answer=0):
	for number in K_PERMS[k]:
		if not answer and number > lower_limit:
			answer = number
		elif answer and sorted(str(number)) == sorted(str(answer)):
			answer = number
	return answer


from itertools import permutations, count

def next_perfectsq_perm(lower_limit, k):
	for i in count(int(lower_limit ** 0.5) + 1):
		j = i ** 2
		if '0' in str(j):
			continue
		k_count = 0
		matches = [j]
		perms = set(permutations(str(j))) - set(matches)
		for p in perms:
			num = int(''.join(p))
			if int(num ** 0.5) ** 2 == num:
				matches.append(num)
				k_count += 1
		if k_count == k:
			return sorted(matches)[-1]


(M, U) = ({}, [])

def H(Q):
	R = 0
	while Q:
		if Q % 10 < 1:
			return 0
		R += 1 << Q % 10 * 3
		Q //= 10
	return R
for F in range(1, 10000):
	F = F * F
	R = H(F)
	if R:
		U.append(F)
		M.setdefault(R, []).append(F)

def next_perfectsq_perm(Q, S):
	for V in U:
		if Q < V:
			T = M.get(H(V))
			if T and S == len(T):
				return T[-1]


import itertools

def next_perfectsq_perm(l, k):
	perf = lambda n: n ** 0.5 % 1 == 0
	while True:
		l += 1
		if perf(l) and '0' not in str(l):
			r = [x for x in set([int(''.join(x)) for x in list(itertools.permutations(str(l)))]) if perf(x)]
			if len(r) == k:
				return max(r)