0

Is there any function that can take a "string" and yield all situation of small and Capital letters every time i call function in a loop which is possible.for example:

i give "string" to function

and it yield all of situation like:

"String"
"sTring"
"stRing"
"strIng"
"striNg"
"strinG"
"STring"
"StRing"
"StrIng"
"StriNg"
"StrinG"
...

I am new in python and i do not have any idea i though there might be a library or function that can do that.

guoard
  • 55
  • 1
  • 7
  • 3
    Please check the following: https://stackoverflow.com/questions/6792803/finding-all-possible-case-permutations-in-python – Jeries Haddad Jun 02 '20 at 14:14
  • Note that there are 2^N possibilities, where `N` is the length of the string - each additional character doubles the number of possibilities. This could get quite time-intensive for long strings. – EJoshuaS - Stand with Ukraine Jun 02 '20 at 14:18
  • 3
    Does this answer your question? [Finding all possible case permutations in Python](https://stackoverflow.com/questions/6792803/finding-all-possible-case-permutations-in-python) – EJoshuaS - Stand with Ukraine Jun 02 '20 at 14:18
  • @JeriesHaddad Nice find. For reference, don't forget that you're able to flag questions as duplicates in cases like this so that people can review it in the close vote queue. – EJoshuaS - Stand with Ukraine Jun 02 '20 at 14:19
  • thanks @JeriesHaddad, but this function is too slow when i give "my name is bob and 123456 is my number" and it can not dodge number and cuz error – guoard Jun 02 '20 at 14:28
  • It's slow because it has to produce an exponential number of values, not because it could be made more efficient. – chepner Jun 02 '20 at 14:31
  • this has to produce 2**24 (16777216) different results. Of course this is slow. (24 is the number of letters in your string, which have to be permuted. white space and numbers are not counted) – gelonida Jun 02 '20 at 14:33
  • it seems my answer is quiet close to this [answer](https://stackoverflow.com/questions/6792803/finding-all-possible-case-permutations-in-python),but has some difference – guoard Jun 02 '20 at 14:51
  • *this function is too slow when i give "my name is bob and 123456 is my number"* This is exactly what I said in my comment - there are 2^N permutations, which is going to be extremely time-consuming for long strings. – EJoshuaS - Stand with Ukraine Jun 02 '20 at 15:37
  • @EJoshuaS-ReinstateMonica is there any way that this function does not make all possibilities and make them one by one then i use it if it didnt help me i use the next one???, so this will not cuz MemoryError – guoard Jun 02 '20 at 18:55
  • What's the criteria for when they help you or not? Iterating through 2^N items one at a time isn't going to be very time-effective. For a long string, that'll be like looking for a needle in a haystack. If anything, that'll probably be even worse than just generating all of them up front. – EJoshuaS - Stand with Ukraine Jun 04 '20 at 18:46

1 Answers1

0

Regarding 'it can not dodge number and cuz error', there are two ways around it (Which you can modify the function to):

  1. Add a try / except in each iteration as in:
try:

except:
    continue
  1. Check if the 'char' is a numeric value, using the isnumeric() function as in:
> if char.isnumeric():
>         continue
Jeries Haddad
  • 79
  • 1
  • 1
  • 9
  • it seems i made a mistake this algorithm can dodge numbers and whitespace with this part `if first.lower() == first.upper():` this is always False for letter but True for others. any way it is steel slow i need do some thing that this function does not make all possibilities and make them one by one then i use it if it didn't help me i use the next one, so this will not cuz MemoryError – guoard Jun 03 '20 at 05:28