You will be given an array of positive integers. The array should be sorted by the amount of distinct perfect squares and reversed, that can be generated from each number permuting its digits.

E.g.: ```arr = [715, 112, 136, 169, 144]``` 
``` 
Number   Perfect Squares w/ its Digits   Amount
 715                -                       0
 112               121                      1
 136               361                      1
 169           169, 196, 961                3
 144             144, 441                   2
``` 
So the output will have the following order:
```[169, 144, 112, 136, 715]``` 

When we have two or more numbers with the same amount of perfect squares in their permutations, we sorted by their values.

In the example given above, we can see that 112 and 136 both generate a perfect square. So 112 comes first.

Examples for this kata:
```python
sort_by_perfsq([715, 112, 136, 169, 144]) == [169, 144, 112, 136, 715]
# number of perfect squares:                   3    2    1    1    0
``` 
We may have in the array numbers that belongs to the same set of permutations.
```python
sort_by_perfsq([234, 61, 16, 441, 144, 728]) == [144, 441, 16, 61, 234, 728]
# number of perfect squares:                      2    2    1   0   0    0
```

Features of the random tests:
~~~if:ruby,python
- Number of tests: 80
- Arrays between 4 and 20 elements
- Integers having from 1 to 7 digits included
~~~
~~~if:js
- Number of tests: 30
- Arrays between 4 and 16 elements
- Integers having from 1 to 7 digits included
~~~

Enjoy it!!

Source: codewars | Difficulty: EASY
Tags: Number theory

Combinatorics

Mathematics

Starter code: def sort_by_perfsq(arr):
	

Solutions:
from collections import defaultdict
SQUARES = [x ** 2 for x in range(1, 3163)]
DIGITS = defaultdict(int)
for sqr in SQUARES:
	DIGITS[''.join(sorted(str(sqr)))] += 1

def sort_by_perfsq(arr):
	return sorted(arr, key=lambda n: (-DIGITS[''.join(sorted(str(n)))], n))


from itertools import permutations

def sort_by_perfsq(lst):
	return sorted(lst, key=perfsq)

def perfsq(n):
	perm = {int(''.join(p)) for p in permutations(str(n))}
	return (sum((-1 for n in perm if n ** 0.5 % 1 == 0)), n)


from itertools import permutations
square = lambda p: not int(''.join(p)) ** 0.5 % 1
count = lambda num: sum(map(square, set(permutations(str(num)))))

def sort_by_perfsq(arr):
	return sorted(arr, key=lambda x: (-count(x), x))


from itertools import permutations

def sort_by_perfsq(a):
	return [x for (_, x) in sorted([(-sum((1 for c in set(permutations(str(n))) if (float(''.join(c)) ** 0.5).is_integer())), n) for n in a])]


from math import sqrt
from itertools import permutations

def sort_by_perfsq(a):
	return sorted(a, key=lambda n: (-sum((str(sqrt(int(''.join(p))))[-2:] == '.0' for p in set(permutations(str(n), len(str(n)))))), n))


def sort_by_perfsq(arr):

	def get_pos_nums(digs, currn=''):
		if len(digs) == 1:
			nums.append(currn + digs[0])
		for d in digs:
			locd = digs[:]
			locd.remove(d)
			get_pos_nums(locd, currn + d)
	count_sq = []
	for x in arr:
		count_sq.append([x, 0])
		digs = [d for d in str(x)]
		nums = []
		get_pos_nums(digs)
		nums = set(map(int, nums))
		for n in nums:
			if n ** 0.5 % 1 == 0:
				count_sq[-1][1] += 1
	count_sq.sort()
	count_sq.sort(key=lambda num: num[1], reverse=True)
	return [num[0] for num in count_sq]


from itertools import permutations

def sort_by_perfsq(arr):
	isSquare = lambda n: int(n ** 0.5) == n ** 0.5
	return sorted(arr, key=lambda n: (sum((isSquare(int(''.join(nS))) for nS in set(permutations(str(n))))), -n), reverse=True)