|
programming challenge: fewest characters |
ReyBrujo
Moderator
January 2001
|
I first posted a 177 characters one, but after some more tweaklings, got it down to 169 (counted by hand). I agree with Matthew, though, required spaces should count as well... but well Much better than my 240 original one, nevertheless I don't like the code, but works (don't try enabling warnings )
(Edited: Posted a new version, with only 161 characters... I still don't like all those print). -- |
Oscar Giner
Member #2,207
April 2002
|
Here in Mathematica. 166 characters. It produces correct output: [edit]The only space required is the one in " ", so 167 characters using this rule[/edit] For[p = ReadList["f", {Word, Word}], p != {}, p = DeleteCases[p, {_, a}], c = Cases[p, {_, a = p[[1]][[2]]}]; {d, e} = c[[1]]; If[Length[c] < 2, Print[e, " ", d], Print[e]; Print["\t", #1[[1]]] & /@ c ] ] Simplified, not producing correct output, it's 133 characters: [edit]No spaces required here, so it stays at 133 characters[/edit] For[p = ReadList["f", {Word, Word}], p != {}, p = DeleteCases[p, {_, a}], c = Cases[p, {_, a = p[[1]][[2]]}]; Print[c[[1]][[2]]]; Print["\t", #1[[1]]] & /@ c ]
-- |
ImLeftFooted
Member #3,935
October 2003
|
134 <Php> Matthew Leverton ReyBrujo said: (Edited: Posted a new version, with only 161 characters... I still don't like all those print). Came in at 160 for me My (fixed) entry:
Oscar Giner said: Neither yours does (and it crashes) The rules didnt say how the program had to exit, i chose to use a segfault;D |
Rash
Member #2,374
May 2002
|
Quote: Whitespace will be removed for counting, so dont bother to make your code look clean in the thread. Bad choice for a rule IMO. Almost all languages force the use of at least one whitespace character to differentiate between identifiers. So it makes more sense to count a sequence of whitespace characters as at least one character for the purpose of your challenge. |
Oscar Giner
Member #2,207
April 2002
|
I improved mine. Now 157 I discovered that you can write v[[a]][<b>] as v[[a,b]], and the Import function does a better job than ReadList For[p = Import["f", "Table"], p != {}, p = DeleteCases[p, {_, a}], c = Cases[p, {_, a = p[[1, 2]]}]; {d, e} = c[[1]]; If[Length[c] < 2, Print[e, " ", d], Print[e]; Print["\t", #1[[1]]] & /@ c] ]
[edit] [edit] [edit] For[p = Import[".dat"], p != {}, p = DeleteCases[p, {_, a}], c = Cases[p, {_, a = p[[1, 2]]}]; {d, e} = c[[1]]; P := Print; If[Length[c] < 2, P[e, " ", d], P[e]; P["\t", #1[[1]]] & /@ c] ]
-- |
Wetimer
Member #1,622
November 2001
|
Okay here it is in lua
I've only used the standard lua libraries, I could make it shorter by adding a few functions, but after all, then I could add a function called a which did everthing. [edit] updated code, discovered that foreach syntax took more then simple for <code>if(Windows.State = Crash) Computer.halt();</code> |
Karadoc ~~
Member #2,749
September 2002
|
Quote: But really, there should be a limitation on language used, or else there's no fair way to compare entries.
Agreed. ----------- |
Matthew Leverton
Supreme Loser
January 1999
|
You would be missing the entire point of the thing, failing to impress anyone. |
ReyBrujo
Moderator
January 2001
|
kazzmir entry is also in Perl. I tried doing it in Ruby, but didn't work too well. Trying to fix it now And thanks, WE, I was wondering where the LUA version was (Edited: Can the text file be "included"? I will try to do it in Prolog, the rules are very easy, but I am not sure how to open and load a file at all :/) -- |
Matthew Leverton
Supreme Loser
January 1999
|
Quote: Can the text file be "included"? Anything should be OK, assuming it's part of the standard libs/langauge. |
Karadoc ~~
Member #2,749
September 2002
|
Quote: You would be missing the entire point of the thing, failing to impress anyone. Thank you for that insight. I'm just saying that in any good 'competition' there needs to be a few strong rules. I used that extreme example just to illustrate my point. ----------- |
ReyBrujo
Moderator
January 2001
|
It is just something to spend time, nothing serious. This is what Allegro programmers do when they are too tired We are not getting rewards (at least, I had not been notified ). -- |
Wetimer
Member #1,622
November 2001
|
Lua again. By changing the way I was doing things I saved about ten characters.
<code>if(Windows.State = Crash) Computer.halt();</code> |
ImLeftFooted
Member #3,935
October 2003
|
134 <Php> Matthew Leverton By the way its looking i think Matt with php is gonna take it. [Edit:]
|
Mars
Member #971
February 2001
|
You should remove the entries that don't get the formatting right... -- |
Oscar Giner
Member #2,207
April 2002
|
Quote: By the way its looking i think Matt with php is gonna take it. Not too fast . I improved my Mathematica entry. 133 characters: For[p = Import[".dat"], p != {}, p = Complement[p, c], c = Cases[p, {_, p[[1, 2]]}]; {d, e} = c[[1]]; Print; If[Length[c] < 2, %[e d], %[e]; %["\t"#[[1]]] & /@ c ] ]
-- |
Matthew Leverton
Supreme Loser
January 1999
|
<? foreach (file("n") as $l) { $p = split(" | ", $l); $n[$p[1]][] = $p[0]; } foreach ($n as $a=>$z) foreach ($z as $i=>$p) echo $z[1] ? $i?" ":"$a " : "$a " ,"$p "; ?> 132 |
Kanzure
Member #3,669
July 2003
|
.. Perl-fu didn't come through at first yet? Oh well, good job (so far) ML. |
Oscar Giner
Member #2,207
April 2002
|
Since you're printing spaces instead of tabs, I can do, too: Print; For[p = Import[".dat"], p != {}, p = Complement[p, c], c = Cases[p, {_, p[[1, 2]]}]; {d, e} = c[[1]]; If[Length[c] < 2, %[e d], %[e]; %[" "#[[1]]] & /@ c ] ] 131 -- |
ReyBrujo
Moderator
January 2001
|
Make room, make room. Here comes Perl and 128 characters Since you use the implicit enter and tab, I thought I could use them as well Had to rewrite the code completely, the previous one could be optimized only a few characters.
(Edited: The 128 char version had a bug, didn't work with a long name list. Replaced it with a 116 version that works ) (Edited 2: 115 characters, replaced " " . $a with " $a"). (Edited 3: 113 characters, () are optional in the open function). (Edited 4: Saved 2 more characters, 111 now). -- |
Colin O'Leary
Member #3,233
February 2003
|
More Perl:
109 ReyBrujo's beats mine if you change his while into a map. (Edit: down to 102) You can get it to 91 if you're allowed to read standard input instead of a file. |
ReyBrujo
Moderator
January 2001
|
There is the Perl guru you were asking for In his and my code, the problem is reporting the data: He creates a map in 48 characters, I create mine in 47. But I didn't know about the $" variable :/ By the way, remove extra semicolon in the push statement Way to go! -- |
Rash
Member #2,374
May 2002
|
|
Elias
Member #358
May 2000
|
Here's a shorter python version: a = [a.split() for a in file("f")] n = {} for i in a: n[i[1]] = 1 for i in n: f = [ l[0] for l in a if l[1] == i ] if len(f) < 2: print i, f[0] else: print "\n ".join(<i> + f)
> cat acc.py | python -c"import sys; print len([x for x in sys.stdin.read() if x != ' ' and x != '\n' ])" says: 129 -- |
Oscar Giner
Member #2,207
April 2002
|
124 characters: Print; For[p = Import[".dat"], p != {}, p = Complement[p, c], c = Cases[p, {_, p[[1, 2]]}]; If[{{d, e} = c[[1]]} == c, %[e d], %[e]; %[" "#[[1]]] & /@ c ] ] I don't think this can get even shorter -- |
|
|