#🔒 String permutations

73 messages · Page 1 of 1 (latest)

cyan shell
#

I have a string like james.robert and I want to get all posibilities of capitalized letters like James.robert or JAmes.robert also jameS.robert and so on.

sinful oxideBOT
#

@cyan shell

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

cyan shell
#
my_str = "james.robert"
for i in range(len(my_str)):
    if my_str[i] != ".":
        print(my_str[:i] + chr(ord(my_str[i])-32) + my_str[i+1:]) 
``` I tried doing for a single iteration, but rn I cannot think of how to make all the possible combinations
ocean lance
#

I would skip ord and do something more like my_str.upper()[i] or my_str.lower()[i] to get the different cases

#

You could leverage itertools.product here

edgy harbor
#

You actually want the permutations or do you just want to calculate how many there are?

cyan shell
cyan shell
eager urchin
cyan shell
cyan shell
eager urchin
#

yes, you'd of course not count that because it can't be capital

rugged tundra
#

turn each character into a list of both possible capitalizations (or the only possible capitalization, for characters like .). feed all these lists to product.

#

note that there'll be like 2048 capitalizations.

cyan shell
#

Is it possible to get the code? I am not trying to achieve a big project with this.

eager urchin
#

whatever you do, i reckon to account for the dot not changing you'd need to generate this new string without special characters and then construct the proper version

crude quartz
#

can I ask why you're trying to achieve this?

cyan shell
cyan shell
crude quartz
#

meaning?

cyan shell
#

You are familiar with CTF challenges right?

crude quartz
#

Nope?

#

But also if it's for a challenge, then carry on

cyan shell
#

CTF means capture the flag

crude quartz
#

just making sure you weren't coming up with some sort of roundabout solution for something

#

Ahh yeah I'm familiar with CTF, but not sure how it relates to programming here

polar pilot
#

Are you brute forcing a password?

cyan shell
cyan shell
#

this is the challenge - as proof

#

and I need to guess a 1-4 digits pin in order to gain control, but after 3 attempts the user accounts get locked out, but the server thinks that james.robert is different than james.Robert but at the same time consideres it a valid username, which exists in their database

ocean lance
#

If this is an open/active competition, then asking for help here might constitute an unfair advantage.

cyan shell
#

Ah no, it is not on going.

#

The challenge was posted 1 month ago

ocean lance
cyan shell
#

Yep

ocean lance
cyan shell
#

so is there a chance that you could guide me more forwardly to get that script? I did not user itertools.product much

ocean lance
#

And we can help guide you from there

cyan shell
cyan shell
cyan shell
#

Like asmej

rugged tundra
#

product's outputs take exactly one item from each iterable and don't reorder them.

cyan shell
#

I see, I will try as soon as I finish eating, thanks

cyan shell
#

This has froze my pc for over 10 minutes

#

I should work with shorter strings

cyan shell
#

cuz I get an output like this and it changed its order

rugged tundra
cyan shell
rugged tundra
#

you're calculating all 12-length strings consisting of 24 possible characters at each position

#

that's not what you want - you want to, for each position, provide a list of up-to-2 possible characters.

#

so make a list consisting of a list for each character (e.g. for the first character, "j", the corresponding list would be ["j", "J"], ["."] for the dot), and unpack it into product.

cyan shell
#

oh, it takes a list of lists?

rugged tundra
#

no, but it does take any number of iterables

#

!e

import itertools
x = ["aA", "bB"]
print(list(itertools.product(*x))) # equivalent to itertools.product(x[0], x[1])
sinful oxideBOT
#

@rugged tundra :white_check_mark: Your 3.12 eval job has completed with return code 0.

[('a', 'b'), ('a', 'B'), ('A', 'b'), ('A', 'B')]
cyan shell
#

would a list comp work here?

my_list = [[a,b] for a,b in zip(list(my_str), list(my_str.capitalize())]
#

to get my list of lists?

cyan shell
rugged tundra
rugged tundra
#

i'd have written it as [[x.lower(), x.upper()] for x in my_str]

cyan shell
#

I managed to reach my goal, thank you

cyan shell
#

!close

sinful oxideBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.