Login Register






[challenge] String Permutations filter_list
Author
Message
[challenge] String Permutations #1
Warning: this challenge is a HUGE pain in the dick and took me 2+ hours to complete

The purpose of said pain in the dick challenge to write a program which prints all the permutations of a string in alphabetical order separated by a comma, considering that digits < upper case letters < lower case letters.

In case you're confused, a permutation is simply a way of arranging characters in a given set of data. This is done a lot in grade school math and science.

I/O example:

Input:
Code:
acb fde igh

Output:
Code:
abc,acb,bac,bca,cab,cba def,dfe,edf,efd,fde,fed ghi,gih,hgi,hig,igh,ihg

Guidelines:

The code you will need to permute (one set per line) is:
Code:
AV7 A2La faHd 7peP eAg y68B HgAf mt06p V2c4 FfNh

Rules:
- No imports unless required
- No use of semicolons when not absolutely necessary
- Code < 6 lines AND 250 characters or less = 20 NSP prize
- Don't bother copying all the text to show output, just post a screenshot

[spoiler=My Code:]
Code (omitted user from filepath):
7 lines, 364 characters
Code:
File.open("C:/Users/Kagetane/Desktop/Code/Ruby/test.txt").each_line { |line| perms = [] v = line.include?("\n") ? line.split("")[0..-2] : line.split("")[0..-1] v.permutation.map { |str| perm = [] str.include?("\n") and str != v[-1] ? str = nil : nil str.each { |u| perm << u } if str != nil perms << perm.join("") } puts perms.sort.join(",") }

Output:
fits perfectly in a default-sized windows cmd window, which is nice
[Image: 2f2Bd3d.png]
[/spoiler]

Some Encouragement:

This challenge is listed under "hard" on CodeEval, which, believe me, is really, really, fucking hard.
The only reason I got this done was because I had a few hours to kill during school, which is currently in moratorium.
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

Reply

RE: [challenge] String Permutations #2
God that was hard.
Thank you for your challenge, it made me learn how to use recursive functions. I almost gave up!
I also would like to thank you for letting me know CodeEval.
I am having tons of fun: 680/6k, but the best thing is that i am learning a lot.

Here is the code (I didn't manage to get under 6 lines, still thinking about it tho):
Spoiler:
6 lines, 228 characters
Code:
def perm(l,a): if len(l)==1: return l[0] for x in range(len(l)): for b in perm(l[:x]+l[x+1:],[]): a.append(l[x]+b) return a for a in open('ha.txt', 'r').read().split(): print ','.join(sorted(perm(a,[])))


And here is the screenshot:
http://i.imgur.com/ZlRmUnX.png
(not posting through img tags because it is big)

[+] 1 user Likes Lysergide's post
Reply

RE: [challenge] String Permutations #3
Hey, that isn't really fair to us C programmers...anyways, here is my 6 line, 250 character solution.

Spoiler:
Code:
#define y int #define x char void s(x*a,x*b){x t=*a;*a=*b;*b=t;} void p(x*a,y i,y n){if(i==n)printf("%s,",a);else for(y j=i;j<=n;j++){s(a+i,a+j);p(a,i+1,n);s(a+i,a+j);}} void main(y c,x**d){for(y i=1;i<c;i++){p(d[i],0,strlen(d[i])-1);printf("\n");}}


and a screenshot:
[Image: 3447WyE.png]

Where's my reward...

@"Chitoge" try this one: https://sinister.li/Thread-Dysfuncti...ight-bulbs
(This post was last modified: 06-21-2015, 12:57 PM by phyrrus9.)

[+] 1 user Likes phyrrus9's post
Reply

RE: [challenge] String Permutations #4
(06-21-2015, 12:45 PM)phyrrus9 Wrote: [Image: 3447WyE.png]

Is there a way to snip the comma off the end? If not, it doesn't follow the format. Soz D:
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

Reply

RE: [challenge] String Permutations #5
(06-21-2015, 05:27 PM)Chitoge Wrote: Is there a way to snip the comma off the end? If not, it doesn't follow the format. Soz D:

... srsly?

Reply

RE: [challenge] String Permutations #6
(06-21-2015, 05:27 PM)Chitoge Wrote: Is there a way to snip the comma off the end? If not, it doesn't follow the format. Soz D:

Actually, the initial problem says nothing about format Tongue

And yes, you could trim it, but it would add characters. C makes it near impossible to do in under 250.

Reply

RE: [challenge] String Permutations #7
(06-21-2015, 05:56 PM)Eclipse Wrote: ... srsly?

Hey, the challenge on CodeEval gave me 10/100 if I didn't follow the format

(06-21-2015, 06:27 PM)phyrrus9 Wrote: Actually, the initial problem says nothing about format Tongue

And yes, you could trim it, but it would add characters. C makes it near impossible to do in under 250.

Hmm.. I might still give you a good chunk of the prize
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

Reply

RE: [challenge] String Permutations #8
I know I'm months late, but here's something that blows the other answers out of the water Biggrin

148 Bytes / 2 Lines

Code:
p=lambda s:[[s[k]+m for k in range(len(s))for m in p(s[:k]+s[-~k:])],[s]][not s] print'\n'.join(','.join(p(x))for x in open('x','r').read().split())

[Image: PPueK6Y.png]

If I could take the strings space-separated through raw_input() this would be down to 139 chars.
[Image: CDUAq9d.png]

[+] 1 user Likes Shebang's post
Reply

RE: [challenge] String Permutations #9
(08-19-2015, 03:16 AM)Shebang Wrote: I know I'm months late, but here's something that blows the other answers out of the water Biggrin

148 Bytes / 2 Lines

Code:
p=lambda s:[[s[k]+m for k in range(len(s))for m in p(s[:k]+s[-~k:])],[s]][not s] print'\n'.join(','.join(p(x))for x in open('x','r').read().split())

[Image: PPueK6Y.png]

If I could take the strings space-separated through raw_input() this would be down to 139 chars.

Jesus, well done
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

[+] 1 user Likes Inori's post
Reply

RE: [challenge] String Permutations #10
(08-19-2015, 03:16 AM)Shebang Wrote: I know I'm months late, but here's something that blows the other answers out of the water Biggrin

148 Bytes / 2 Lines

Code:
p=lambda s:[[s[k]+m for k in range(len(s))for m in p(s[:k]+s[-~k:])],[s]][not s] print'\n'.join(','.join(p(x))for x in open('x','r').read().split())

[Image: PPueK6Y.png]

If I could take the strings space-separated through raw_input() this would be down to 139 chars.

#golfingpro

OT: Wow, I forgot all about this challenge. I might yet attempt it.

Reply