Python Forum
Making a function more efficient
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a function more efficient
#9
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.


#! /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$
Reply


Messages In This Thread
Making a function more efficient - by CatorCanulis - Oct-02-2022, 07:24 AM
RE: Making a function more efficient - by DPaul - Oct-03-2022, 06:00 AM
RE: Making a function more efficient - by DPaul - Oct-04-2022, 07:10 AM
RE: Making a function more efficient - by DPaul - Oct-05-2022, 06:30 AM
RE: Making a function more efficient - by Pedroski55 - Oct-06-2022, 12:59 AM
RE: Making a function more efficient - by DPaul - Oct-06-2022, 07:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A more efficient code titanif 2 529 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  Cleaning my code to make it more efficient BSDevo 13 1,549 Sep-27-2023, 10:39 PM
Last Post: BSDevo
Question Making a copy list in a function RuyCab 1 1,847 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  Making a code.py file to function, does not run hobbyist 6 2,987 Jan-27-2021, 07:50 AM
Last Post: DeaD_EyE
  I there a more efficient way of printing ? Capitaine_Flam 7 3,615 Dec-01-2020, 10:37 AM
Last Post: buran
  A more efficient way of comparing two images in a python vukan 0 2,055 Mar-17-2020, 11:39 AM
Last Post: vukan
  making a function that writes variables (is possible?) miker2808 3 2,407 Jan-30-2020, 06:27 PM
Last Post: buran
  how to make iterative search more efficient renergy 2 2,301 Jan-03-2020, 03:43 PM
Last Post: stullis
  Simple problem. looking for an efficient way silverchicken24 3 2,397 Oct-14-2019, 07:13 PM
Last Post: Larz60+
  need help with making a validating function drasil 8 3,835 Mar-28-2019, 10:38 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020