Our server costs ~$56 per month to run. Please consider donating or becoming a Patron to help keep the site running. Help us gain new members by following us on Twitter and liking our page on Facebook!
Current time: November 22, 2024, 12:25 pm

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bruteforce
#1
Bruteforce
Basically what i want to do is this:

Input Example 1:

abc

Output:

abc
acb
bac
bca
cba
cab

Input Example 2:

abcd

Output:

abcd
abdc
acdb
acbd
adbc
adcb
bacd
badc
bcda
bcad
bdac
bdca
cabd
cadb
cbda
cbad
cdab
cdba
dacb
dabc
dbca
dbac
dcda
dcad


What i want to do is to print all the different possibilities of a given string.


The current method i'm following:

*Swap the first character with the second character
*Swap the second character with the third character
*Repeat swapping until at least one of the character involved in swapping is the null character.


The problem with my current method:

*Hell lot of duplications,i.e,"input" outputting atleast more than once.
*Doesn't output all the combinations.

What i'd like for you to do is give me some hints as to what i'm doing wrong and give me a working pseudo code to solve my crisis(not a direct solution,please).
Yes,i could've posted this in some programmer forum,but i'm not sure how i'm supposed to form my question.I'd go there and indefinitely make myself look a fool Rolleyes This forum is more safe since we've already established i'm a moron.

P.S. 
I spend all my last years vacation trying to find a solution to this problem and i did.The problem is,it was kind of like a fluke,i think.
I spend so much time on it,it worked at last,i slept and i woke up,i tried to read my code and i was like wtf is this.
Short story short: I solved the problem but after a good night's sleep i no longer remembered how i solved it,and i couldn't even figure what i did in my code either,yeah my code was that ugly.
I've tested that program,and it works,but although it works i'm not sure if its the most efficient,also there are a lot of duplications,so instead of my cave man problem solving skills,i was wondering if someone could provide me with a methodical approach to the problem.
If its possible i'd like a Recursive as well as an iterative approach to solve the problem(i prefer iterative solution though).
Reply
#2
RE: Bruteforce
What platform were you wanting to run this on ?

There is a routine for the HP41 somewhere in my collection of PPC Journal back issues that does what you want.
 The granting of a pardon is an imputation of guilt, and the acceptance a confession of it. 




Reply
#3
RE: Bruteforce
I would use a container method. For word size n, for example where n=4:

ABCD

n1(n2(n3(n4))) ... n1(n4( ... )) ... n2 ... n4
For Religion & Health see:[/b][/size] Williams & Sternthal. (2007). Spirituality, religion and health: Evidence and research directions. Med. J. Aust., 186(10), S47-S50. -LINK

The WIN/Gallup End of Year Survey 2013 found the US was perceived to be the greatest threat to world peace by a huge margin, with 24% of respondents fearful of the US followed by: 8% for Pakistan, and 6% for China. This was followed by 5% each for: Afghanistan, Iran, Israel, North Korea. -LINK


"That's disgusting. There were clean athletes out there that have had their whole careers ruined by people like Lance Armstrong who just bended thoughts to fit their circumstances. He didn't look up cheating because he wanted to stop, he wanted to justify what he was doing and to keep that continuing on." - Nicole Cooke
Reply
#4
RE: Bruteforce
Found this, if you change the 'digit map' it will do what you want apparently. (I'm not familiar with 'Python', BTW)

digit_map = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz',
}

def word_numbers(input):
input = str(input)
ret = ['']
for char in input:
letters = digit_map.get(char, '')
ret = [prefix+letter for prefix in ret for letter in letters]
return ret
 The granting of a pardon is an imputation of guilt, and the acceptance a confession of it. 




Reply
#5
RE: Bruteforce
(August 2, 2015 at 9:18 am)Aractus Wrote: I would use a container method. For word size n, for example where n=4:

ABCD

n1(n2(n3(n4))) ... n1(n4( ... )) ... n2 ... n4

I'm sorry,i'm not familiar with the concept of a container method.Anyhow i'd be much appreciative if you could provide me with a pseudo code or an algorithm.

(August 2, 2015 at 9:21 am)vorlon13 Wrote: Found this, if you change the 'digit map' it will do what you want apparently. (I'm not familiar with 'Python', BTW)

digit_map = {
   '2': 'abc',
   '3': 'def',
   '4': 'ghi',
   '5': 'jkl',
   '6': 'mno',
   '7': 'pqrs',
   '8': 'tuv',
   '9': 'wxyz',
}

def word_numbers(input):
 input = str(input)
 ret = ['']
 for char in input:
   letters = digit_map.get(char, '')
   ret = [prefix+letter for prefix in ret for letter in letters]
 return ret

Thanks,but god that looks ugly,i can't make sense of the code probably because i'm not familiar with that language,i don't really want the code,i just want the algorithm or pseudo code.

P.S. I'm using c,not that it helps,just saying.
Reply
#6
RE: Bruteforce
Technically, what you want to do is generate all permutations of a given ordered tuple. Why don't you read the wiki on "permutations" and see if something useful comes out...
The fool hath said in his heart, There is a God. They are corrupt, they have done abominable works, there is none that doeth good.
Psalm 14, KJV revised edition

Reply
#7
RE: Bruteforce
Read this: http://www.cs.princeton.edu/~rs/talks/perms.pdf
Quote:To know yet to think that one does not know is best; Not to know yet to think that one knows will lead to difficulty.
- Lau Tzu

Join me on atheistforums Slack Cool Shades (pester tibs via pm if you need invite) Tongue

Reply
#8
RE: Bruteforce
(August 2, 2015 at 10:15 am)Aoi Magi Wrote: Read this: http://www.cs.princeton.edu/~rs/talks/perms.pdf

Looks nice,i'll give it a look.
Reply
#9
RE: Bruteforce
In any coder forum that I use, people usually frown upon giving others the sollution for the homework given to a student, but I will give a clue: It can be done iteratively or recursively. Here is the code:




I hate lazy people Dodgy
Reply
#10
RE: Bruteforce
(August 2, 2015 at 10:55 am)LastPoet Wrote: In any coder forum that I use, people usually frown upon giving others the sollution for the homework given to a student, but I will give a clue: It can be done iteratively or recursively. Here is the code:




I hate lazy people Dodgy

Hahahaha nice one. Clap
You have no idea how much time i've had spend on this problem,i had to give in to my temptation.Sawry  Angel

Edit: I love how you used "//"
Reply





Users browsing this thread: 1 Guest(s)