Find All Possible Fixed Size String Python

2024/11/19 16:24:27

Problem:
I want to generate all possible combination from 36 characters that consist of alphabet and numbers in a fixed length string. Assume that the term "fixed length" is the upper bound for my string length.
Some of these combinations are listed below:

a
b
c
.
.
z
0
1
.
.
9
.
.
.
aaaaaa
aaaaab
.
.
99999999999999999998
99999999999999999999 // end of the list

I don't want to store them in memory or disk because I calculate something on them and if they don't meet the goal result, well, I don't need it anymore.

Approaches:
1. For the first try, I choose recursion to create strings. But it's so awful in using memory.
2. For the second try, I use DFS algorithm because it's nearly optimal in using memory as it's around O(bm). But as time view, it's very time-consuming.

Is there any other way to do this more optimized?

UPDATE
Here is possible outputs for length 2 and 3 character:

a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc
Answer

You can use itertools.product for this. It returns a generator of sequences of fixed length.

As you order your strings first by length and then lexicographically, you may use something like this

import itertoolsfor l in range(1, 5):for seq in itertools.product("abc", repeat=l):print("".join(seq))
https://en.xdnf.cn/q/119932.html

Related Q&A

What is the concept of namespace when importing a function from another module?

main.py:from module1 import some_function x=10 some_function()module1.py:def some_function():print str(x)When I execute the main.py, it gives an error in the moduel1.py indicating that x is not availab…

How to pass a literal value to a kedro node? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.This po…

How to Loop a List and Extract required data (Beautiful Soup)

I need help in looping a list and extracting the src links. This is my list and the code: getimages = getDetails.find_all(img) #deleting the first image in the list getimages[0].decompose() print(getim…

square root without pre-defined function in python

How can one find the square root of a number without using any pre-defined functions in python?I need the main logic of how a square root of a program works. In general math we will do it using HCF bu…

How do I sort a text file by three columns with a specific order to those columns in Python?

How do I sort a text file by three columns with a specific order to those columns in Python?My text_file is in the following format with whitespaces between columns:Team_Name Team_Mascot Team_Color Te…

regular expression to search only one-digit number

Im trying to find sentences having only one digit number along with.sentence="Im 30 years old." print(re.match("[0-9]", sentence)then it returns<re.Match object; span=(0, 1), mat…

Automate adding new column and field names to all csv files in directories [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

Connect the python app to a database using centos 7

I am new to all this I have apython app already helo.mysql.py and need to Connect the python app to a database. I am using centos 7 and have it installed on a ec2 instance if anyone can help please he…

How do I restart my program in Python? (see code)

if option == C:radius = float(raw_input("Enter Radius: ")) area = pi * radius**2print "Working..."sleep(1)print ("Area: %.2f. \n%s" % (area, hint))elif option == T:base = …

how to create a list of elements from an XML file in python

my XML <root> - <Book category="Children"><title>Harry Potter</title> <author>J.K</author> <year>2005</year> <price>29.99</price> &…