![]() |
|
[challenge] String Permutations - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Coding (https://sinister.li/Forum-Coding--71) +--- Thread: [challenge] String Permutations (/Thread-challenge-String-Permutations) |
[challenge] String Permutations - Inori - 06-03-2015 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
ighOutput: Code: abc,acb,bac,bca,cab,cba
def,dfe,edf,efd,fde,fed
ghi,gih,hgi,hig,igh,ihgGuidelines: The code you will need to permute (one set per line) is: Code: AV7
A2La
faHd
7peP
eAg
y68B
HgAf
mt06p
V2c4
FfNhRules: - 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 ![]() [/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. RE: [challenge] String Permutations - Lysergide - 06-21-2015 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) RE: [challenge] String Permutations - phyrrus9 - 06-21-2015 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: ![]() Where's my reward... @"Chitoge" try this one: https://sinister.li/Thread-Dysfunctional-challenge-light-bulbs RE: [challenge] String Permutations - Inori - 06-21-2015 (06-21-2015, 12:45 PM)phyrrus9 Wrote: Is there a way to snip the comma off the end? If not, it doesn't follow the format. Soz D: RE: [challenge] String Permutations - Eclipse - 06-21-2015 (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? RE: [challenge] String Permutations - phyrrus9 - 06-21-2015 (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 ![]() And yes, you could trim it, but it would add characters. C makes it near impossible to do in under 250. RE: [challenge] String Permutations - Inori - 06-21-2015 (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 Hmm.. I might still give you a good chunk of the prize RE: [challenge] String Permutations - Shebang - 08-19-2015 I know I'm months late, but here's something that blows the other answers out of the water ![]() 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())![]() If I could take the strings space-separated through raw_input() this would be down to 139 chars. RE: [challenge] String Permutations - Inori - 08-19-2015 (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 Jesus, well done RE: [challenge] String Permutations - Eclipse - 08-19-2015 (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 #golfingpro OT: Wow, I forgot all about this challenge. I might yet attempt it. |