Oct-06-2022, 12:59 AM
(This post was last modified: Oct-06-2022, 10:17 AM by Pedroski55.)
I was interested, so I thought I'd try!
As I see it, the problem is simply, rearrange all numbers to find the smallest possible number using those digits.
Thus, 101 can't be made smaller, but 110 would become 101, 1100 becomes 1001
If the rearranged number is in the list, it won't be added.
As I see it, the problem is simply, rearrange all numbers to find the smallest possible number using those digits.
Thus, 101 can't be made smaller, but 110 would become 101, 1100 becomes 1001
If the rearranged number is in the list, it won't be added.
#! /usr/bin/python3 from time import time unums = {i:[10**(i-1)] for i in range(2, 6)} # from a given number rearrange it to create the smallest number using those digits def makeSmall(num): templist = [] for j in num: templist.append(j) smallest = '9' for j in range(len(templist)): if not templist[j] == '0': if templist[j] < smallest: smallest = templist[j] #print('smallest number is', smallest) newlist = [] count = 0 # how many times is the smallest number present? for j in templist: if j == smallest: count += 1 for j in range(count - 1): newlist.append(smallest) for j in templist: if not j == smallest: newlist.append(j) newlist.sort() #print(newlist) newnewlist = [smallest] + newlist #print(newnewlist) numstring = ''.join(newnewlist) return int(numstring) def getNums(start): key = len(str(start)) print('key is', key) for i in range(start + 1, start * 10): numstring = str(i) mynum = makeSmall(numstring) if not mynum in unums[key]: unums[key].append(mynum) def doit(): for key in unums.keys(): getNums(unums[key][0]) #unums[key].sort() if __name__ == '__main__': start = time() doit() path2text = '/home/pedro/myPython/sets/' with open(path2text + 'unique_numbers.txt', 'w') as result: for key in unums.keys(): stringlist = [] for d in unums[key]: stringlist.append(str(d)+ '\n') data = ''.join(stringlist) result.write(data) print('Result saved to', path2text + 'unique_numbers.txt') end = time() execution_time = end - start print('Execution time was:', execution_time)
Output:pedro@pedro-HP:~/myPython/sets$ ./find_unique_number_groups5.py
key is 2
key is 3
key is 4
key is 5
Execution time was: 0.6043331623077393
pedro@pedro-HP:~/myPython/sets$