Jump to content

Putting a Limit On Output in Python

LtStaffel

Hello,

I've made this program in Python using a permutations tool, to try to decode a scrambled message I was sent.  It finds every possible combination of characters you give it.  Thing is, it uses all of the characters.  Is there a way to put a limit on the amount of characters it can use?

 

EDIT: I accidentally put this in OSes, can a mod please move it to programming?

from itertools import permutationsthingInput = input("Enter the message you want to see every possible combination of: ")things = []i = 0while i < len(thingInput):    things.append(thingInput[i])    i+=1for p in permutations(things):    print(p)

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

Hello,

I've made this program in Python using a permutations tool, to try to decode a scrambled message I was sent.  It finds every possible combination of characters you give it.  Thing is, it uses all of the characters.  Is there a way to put a limit on the amount of characters it can use?

 

EDIT: I accidentally put this in OSes, can a mod please move it to programming?

from itertools import permutationsthingInput = input("Enter the message you want to see every possible combination of: ")things = []i = 0while i < len(thingInput):    things.append(thingInput[i])    i+=1for p in permutations(things):    print(p)

What do you mean by putting a limit on the characters you use? In the user input? In the response from the program?

CPU: AMD FX-6300 4GHz @ 1.3 volts | CPU Cooler: Cooler Master Hyper 212 EVO | RAM: 8GB DDR3

Motherboard: Gigabyte 970A-DS3P | GPU: EVGA GTX 960 SSC | SSD: 250GB Samsung 850 EVO

HDD: 1TB WD Caviar Green | Case: Fractal Design Core 2500 | OS: Windows 10 Home

Link to comment
Share on other sites

Link to post
Share on other sites

What do you mean by putting a limit on the characters you use? In the user input? In the response from the program?

If you run it an input something, for example "test" it will return something like:

test

tets

etst

etts

 

etc.  you put four characters in it output four characters each time.  Is there a way to make it output, for example two, letters at a time and return something like:

te

st

et

ts

 

etc?

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

snip

A string in python is just a list of characters. You can use this to achieve your desired result.

 

my_string = 'bananas' #Prints the first character in my_string (0 is the index of the first character, computers start counting at 0)print(my_string[0]) #Prints the first two characters in my_string (this will not include the character at index 2, it will return the characters at index 0 and index 1)print(my_string[:2]) #Prints the last two characters in my_string#("len()" returns the length of a string if you count starting at 1, which is not how computers normally count; this does include the character at index 5)print(my_string[len(my_string) - 2:])

CPU: AMD FX-6300 4GHz @ 1.3 volts | CPU Cooler: Cooler Master Hyper 212 EVO | RAM: 8GB DDR3

Motherboard: Gigabyte 970A-DS3P | GPU: EVGA GTX 960 SSC | SSD: 250GB Samsung 850 EVO

HDD: 1TB WD Caviar Green | Case: Fractal Design Core 2500 | OS: Windows 10 Home

Link to comment
Share on other sites

Link to post
Share on other sites

 

A string in python is just a list of characters. You can use this to achieve your desired result.

my_string = 'bananas' #Prints the first character in my_string (0 is the index of the first character, computers start counting at 0)print(my_string[0]) #Prints the first two characters in my_string (this will not include the character at index 2, it will return the characters at index 0 and index 1)print(my_string[:2]) #Prints the last two characters in my_string#("len()" returns the length of a string if you count starting at 1, which is not how computers normally count; this does include the character at index 5)print(my_string[len(my_string) - 2:])

I'm not sure you quite get what I mean.  Here, let me try to explain it more clearly, I'm not very good at explaining problems a lot of the time.  Here is an example run of my program:

Enter the message you want to see every possible combination of: stuff
('s', 't', 'u', 'f', 'f')
('s', 't', 'u', 'f', 'f')
('s', 't', 'f', 'u', 'f')
('s', 't', 'f', 'f', 'u')
('s', 't', 'f', 'u', 'f')
('s', 't', 'f', 'f', 'u')
('s', 'u', 't', 'f', 'f')
('s', 'u', 't', 'f', 'f')
('s', 'u', 'f', 't', 'f')
('s', 'u', 'f', 'f', 't')
('s', 'u', 'f', 't', 'f')
('s', 'u', 'f', 'f', 't')
('s', 'f', 't', 'u', 'f')
('s', 'f', 't', 'f', 'u')
('s', 'f', 'u', 't', 'f')
('s', 'f', 'u', 'f', 't')
('s', 'f', 'f', 't', 'u')
('s', 'f', 'f', 'u', 't')
('s', 'f', 't', 'u', 'f')
('s', 'f', 't', 'f', 'u')
('s', 'f', 'u', 't', 'f')
('s', 'f', 'u', 'f', 't')
('s', 'f', 'f', 't', 'u')
('s', 'f', 'f', 'u', 't')
('t', 's', 'u', 'f', 'f')
('t', 's', 'u', 'f', 'f')
('t', 's', 'f', 'u', 'f')
('t', 's', 'f', 'f', 'u')
('t', 's', 'f', 'u', 'f')
('t', 's', 'f', 'f', 'u')
('t', 'u', 's', 'f', 'f')
('t', 'u', 's', 'f', 'f')
('t', 'u', 'f', 's', 'f')
('t', 'u', 'f', 'f', 's')
('t', 'u', 'f', 's', 'f')
('t', 'u', 'f', 'f', 's')
('t', 'f', 's', 'u', 'f')
('t', 'f', 's', 'f', 'u')
('t', 'f', 'u', 's', 'f')
('t', 'f', 'u', 'f', 's')
('t', 'f', 'f', 's', 'u')
('t', 'f', 'f', 'u', 's')
('t', 'f', 's', 'u', 'f')
('t', 'f', 's', 'f', 'u')
('t', 'f', 'u', 's', 'f')
('t', 'f', 'u', 'f', 's')
('t', 'f', 'f', 's', 'u')
('t', 'f', 'f', 'u', 's')
('u', 's', 't', 'f', 'f')
('u', 's', 't', 'f', 'f')
('u', 's', 'f', 't', 'f')
('u', 's', 'f', 'f', 't')
('u', 's', 'f', 't', 'f')
('u', 's', 'f', 'f', 't')
('u', 't', 's', 'f', 'f')
('u', 't', 's', 'f', 'f')
('u', 't', 'f', 's', 'f')
('u', 't', 'f', 'f', 's')
('u', 't', 'f', 's', 'f')
('u', 't', 'f', 'f', 's')
('u', 'f', 's', 't', 'f')
('u', 'f', 's', 'f', 't')
('u', 'f', 't', 's', 'f')
('u', 'f', 't', 'f', 's')
('u', 'f', 'f', 's', 't')
('u', 'f', 'f', 't', 's')
('u', 'f', 's', 't', 'f')
('u', 'f', 's', 'f', 't')
('u', 'f', 't', 's', 'f')
('u', 'f', 't', 'f', 's')
('u', 'f', 'f', 's', 't')
('u', 'f', 'f', 't', 's')
('f', 's', 't', 'u', 'f')
('f', 's', 't', 'f', 'u')
('f', 's', 'u', 't', 'f')
('f', 's', 'u', 'f', 't')
('f', 's', 'f', 't', 'u')
('f', 's', 'f', 'u', 't')
('f', 't', 's', 'u', 'f')
('f', 't', 's', 'f', 'u')
('f', 't', 'u', 's', 'f')
('f', 't', 'u', 'f', 's')
('f', 't', 'f', 's', 'u')
('f', 't', 'f', 'u', 's')
('f', 'u', 's', 't', 'f')
('f', 'u', 's', 'f', 't')
('f', 'u', 't', 's', 'f')
('f', 'u', 't', 'f', 's')
('f', 'u', 'f', 's', 't')
('f', 'u', 'f', 't', 's')
('f', 'f', 's', 't', 'u')
('f', 'f', 's', 'u', 't')
('f', 'f', 't', 's', 'u')
('f', 'f', 't', 'u', 's')
('f', 'f', 'u', 's', 't')
('f', 'f', 'u', 't', 's')
('f', 's', 't', 'u', 'f')
('f', 's', 't', 'f', 'u')
('f', 's', 'u', 't', 'f')
('f', 's', 'u', 'f', 't')
('f', 's', 'f', 't', 'u')
('f', 's', 'f', 'u', 't')
('f', 't', 's', 'u', 'f')
('f', 't', 's', 'f', 'u')
('f', 't', 'u', 's', 'f')
('f', 't', 'u', 'f', 's')
('f', 't', 'f', 's', 'u')
('f', 't', 'f', 'u', 's')
('f', 'u', 's', 't', 'f')
('f', 'u', 's', 'f', 't')
('f', 'u', 't', 's', 'f')
('f', 'u', 't', 'f', 's')
('f', 'u', 'f', 's', 't')
('f', 'u', 'f', 't', 's')
('f', 'f', 's', 't', 'u')
('f', 'f', 's', 'u', 't')
('f', 'f', 't', 's', 'u')
('f', 'f', 't', 'u', 's')
('f', 'f', 'u', 's', 't')
('f', 'f', 'u', 't', 's')
 
Process finished with exit code 0
Now if I run your code with "stuff":
s
st
ff
 
Process finished with exit code 0
Notice how mine actually gave all the possible combinations of the characters I gave it.  Your's didn't, it never used the "u", and it also gave inconsistent amounts of output (the single "s" vs the two "st" and "ff").  What I'd like is for the program to also ask another question, that being "How long do you want the results to be?" and then you could put in 3.  It would the take stuff, and use it as a word bank and make every possible combination of characters, with a max string length of 3.  You see what I mean?  By the way, thanks for helping me out :)

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

permutations can take a parameter 'r'. to specify the length of the resulting output. If it's not supplied, or if you supply r=None, then it defaults to the length of the input.

input = ...length = 2for p in permutations(input, r=length):    # Do something

Example

Link to comment
Share on other sites

Link to post
Share on other sites

snip

What I was trying to do is demonstrate the code you can use in your program to achieve your desired result. I was attempting to give you the tools to solve your problem. Like @madknight3 said, you can ask the user to specify the length and then use what I showed you.

 

I didn't want to just do it for you, because that ruins the fun of coding :)

CPU: AMD FX-6300 4GHz @ 1.3 volts | CPU Cooler: Cooler Master Hyper 212 EVO | RAM: 8GB DDR3

Motherboard: Gigabyte 970A-DS3P | GPU: EVGA GTX 960 SSC | SSD: 250GB Samsung 850 EVO

HDD: 1TB WD Caviar Green | Case: Fractal Design Core 2500 | OS: Windows 10 Home

Link to comment
Share on other sites

Link to post
Share on other sites

permutations can take a parameter 'r'. to specify the length of the resulting output. If it's not supplied, or if you supply r=None, then it defaults to the length of the input.

input = ...length = 2for p in permutations(input, r=length):    # Do something
Thanks, I didn't know about that.

Example

  

What I was trying to do is demonstrate the code you can use in your program to achieve your desired result. I was attempting to give you the tools to solve your problem. Like @madknight3 said, you can ask the user to specify the length and then use what I showed you.

 

I didn't want to just do it for you, because that ruins the fun of coding :)

In hindsight, I better see what you were saying, thanks!

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

   In hindsight, I better see what you were saying, thanks!

Glad to help!

CPU: AMD FX-6300 4GHz @ 1.3 volts | CPU Cooler: Cooler Master Hyper 212 EVO | RAM: 8GB DDR3

Motherboard: Gigabyte 970A-DS3P | GPU: EVGA GTX 960 SSC | SSD: 250GB Samsung 850 EVO

HDD: 1TB WD Caviar Green | Case: Fractal Design Core 2500 | OS: Windows 10 Home

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×