-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpandig.py
118 lines (104 loc) · 3.04 KB
/
pandig.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf-8 -*-
"""
Pandigital and other digit-related helper functions
@author: dsisson
"""
def pandigital(number, n):
"""
Determines if a number of length "n" is 1 to n pandigital.
"""
if(n>9):
print("Numbers longer than 9 digits cannot be pandigital.")
return False
digits = [int(d) for d in str(number)]
if(len(digits)<n or len(digits)>n):
return False
count = [0]*n
for i in range(len(digits)):
if(digits[i]>n or digits[i]==0):
return False
count[digits[i]-1] += 1
for k in range(n):
if count[k]!=1:
return False
return True
def makePandig(digits):
"""
Recursively creates a list of all n-digit pandigital numbers, in ascending
order
digits - Number of digits in the pandigitals
"""
if(len(digits)==2):
return [digits[0]+digits[1],digits[1]+digits[0]]
elif(len(digits)<2):
print("Requires at least 2 digits.")
return digits
else:
panList = []
for i in range(len(digits)):
oneOut = list(digits)
del oneOut[i]
subList = makePandig(oneOut)
for number in subList:
number = digits[i] + number
panList.append(number)
return panList
def nConcat(base, n):
"""
Concatenates multiples of a number together
base - the number we are multiplying
n - the largest multiple to concatenate
out - a string of the concatenation
"""
out = ""
for i in range(1,n+1):
out = out+str(base*i)
return int(out)
def isUnique(number, n):
"""
Determines if a number has n unique digits
"""
digits = [int(d) for d in str(number)]
if(len(digits)<n-1):
return False
if(len(digits)==n-1):
digits.insert(0,0)
for i in range(len(digits)):
for j in range(i+1,len(digits)):
if(digits[i]==digits[j]):
return False
return True
def uniqueMult(num):
"""
Gives all 3-digit multiples of a number that possess unique digits
"""
out = [str(x*num) for x in range(1,int(1000/num)+1)]
i = 0
while(i < len(out)):
if(int(out[i])<10):
out[i] = '00' + out[i]
elif(int(out[i])<100):
out[i] = '0' + out[i]
if(isUnique(out[i],3)==False):
del out[i]
else:
i += 1
return out
def equalDigits(n1, n2):
"""
Checks if numbers n1 and n2 have the same digits, a.k.a are anagrams
"""
d1 = [int(d) for d in str(n1)]
d2 = [int(d) for d in str(n2)]
if(len(d1)!=len(d2)):
return False
length = len(d1)
count1 = [0]*10
count2 = [0]*10
for i in range(length):
count1[d1[i]] += 1
count2[d2[i]] += 1
for i in range(10):
if(count1[i]!=count2[i]):
return False
return True