Python Forum
Making a function more efficient
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a function more efficient
#1
Hi everyone,

I'm working on a function that avoids transposed numbers. Like if you have 12 you can't have 21 or if you have 126 you can't have 216 oder 621.

What I got so far:

#!/bin/env python3

import itertools
import itertools

from numba import jit, cuda
import numpy as np

#@jit(target_backend='cuda')
def getNonSwappables(iStart, iStop, verbose = False):

  idsClean = []

  ids = list(range(iStart, iStop))

  for i in ids:

    if verbose:
      print("Processing: ", i)
    # Create permutation for current string.
    sPermutations = [''.join(p) for p in itertools.permutations(str(i))]

    # Check if the clean ids list contains any of the permutations.
    if not any(item in str(idsClean) for item in sPermutations):

      # If not so, apped the current integer to the list of clean ids.
      idsClean.append(i)

  return(idsClean)

listIDsClean = getNonSwappables(1, 100000, True)
print(listIDsClean)
print(len(listIDsClean))
For numbers < 10.000 it works somewhat reasonable. But larger numbers take forever. Is there a way I can code this more efficient?

As you can see, I already tried to crunch it with my gpu but compilation failed. That's not really a sustainable option anyway.

Any help is greatly appreciated.

Markus
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 DPaul - Oct-06-2022, 07:47 AM

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