Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » programming challenge: fewest characters

This thread is locked; no one can reply to it. rss feed Print
 1   2   3   4 
programming challenge: fewest characters
ReyBrujo
Moderator
January 2001
avatar

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 :D Much better than my 240 original one, nevertheless :)

I don't like the code, but works (don't try enabling warnings ;))

1open(F, "t");
2@_ = <F>;
3 
4while ($_ = pop @_) {
5 ($a,$b) = split;
6 $f = ' ';
7 pop;
8 
9 if (! @h{$b}) {
10 print $b;
11 for (@_) {
12 ($c,$d) = split;
13 if ($b eq $d) {
14 $f = "\n\t";
15 print $f.$c;
16 }
17 }
18 
19 print "$f$a\n";
20 @h{$b} = 1;
21 }
22}

(Edited: Posted a new version, with only 161 characters... I still don't like all those print).

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Oscar Giner
Member #2,207
April 2002
avatar

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
avatar

134 <Php> Matthew Leverton
143 <Haskell> X-G
160 <Perl> ReyBrujo
166 <Mathematica> Oscar Giner
167 <Python> Mars
178 <Python> Joel Davis
244 <Perl?> kazzmir
252 <C++> ImLeftFooted
364 <C++> Winston Ewert
377 <C++> William Heatley
400 <C++> Oscar Giner
414 <.NET> Rick

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:

1#define S string
2 
3#include <iostream>
4#include <fstream>
5#include <map>
6using namespace std;
7 
8main ( ) {
9 map < S, S > p ;
10
11 fstream s ( "t" ) ;
12
13 for ( S e, x ; s.good ( ) ; p [ x ] += " " + e + "\n" )
14 s >> e >> x;
15
16 S y;
17
18 for ( map < S, S > :: iterator P = p.begin ( ) ; ; cout << P -> first << ( y . rfind ( " " ) != 0 ? "\n" : "" ) << y, ++ P )
19 y = P -> second;
20}

Oscar Giner said:

Neither yours does (and it crashes) :P

The rules didnt say how the program had to exit, i chose to use a segfault;D

Rash
Member #2,374
May 2002
avatar

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
avatar

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]
Oh, if the file extension is ".dat", Import mode defaults to "Table". The rules don't say anything about the file name, so now it's only 153.

[edit]
3 characters more saved. Now it's 150.

[edit]
Namig the file just ".dat" saves 1 character. Windows (at least 98) won't let you use this filename, but renaming it with DOS does the job :) 149 characters.

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

1s = string
2p = s.sub
3n = {}
4t = table
5r = t.foreach
6o = print
7 
8for l in io.lines("f") do
9 v = s.find(l," ")
10 f = p(l,v+1)
11
12 if(type(n[f]) == "nil") then n[f] = {} end
13 l = p(l,1,v);
14 n[f][l] = l;
15end
16 
17for l,t in n do
18o(l)
19for a in t do
20 o("",a)
21end
22end

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
avatar

Quote:

But really, there should be a limitation on language used, or else there's no fair way to compare entries.

Agreed.
I could make a programming language which just happens to have a built in function to do the whole thing.
The code would then just be:
go;
or something like that.

-----------

Matthew Leverton
Supreme Loser
January 1999
avatar

You would be missing the entire point of the thing, failing to impress anyone.

ReyBrujo
Moderator
January 2001
avatar

kazzmir entry is also in Perl. I tried doing it in Ruby, but didn't work too well. Trying to fix it now :P 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 :/)

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Matthew Leverton
Supreme Loser
January 1999
avatar

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
avatar

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
avatar

It is just something to spend time, nothing serious. This is what Allegro programmers do when they are too tired :P We are not getting rewards (at least, I had not been notified :D).

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Wetimer
Member #1,622
November 2001

Lua again. By changing the way I was doing things I saved about ten characters.

1n = {}
2s = string
3t = table
4z = print
5y = s.sub
6 
7for a in io.lines("f") do
8 l = s.find(a," ")
9 t.insert(n,{y(a,1,l),y(a,l+1)})
10end
11 
12t.sort(n, function (a,b) return a[2] < b[2] end)
13 
14for l,y in n do
15 
16 x = y[2]
17 if(p ~= x) then z(x) end
18 p = x
19
20 z("",y[1])
21end

<code>if(Windows.State = Crash) Computer.halt();</code>

ImLeftFooted
Member #3,935
October 2003
avatar

134 <Php> Matthew Leverton
143 <Haskell> X-G
149 <Mathematica> Oscar Giner
160 <Perl> ReyBrujo
167 <Python> Mars
178 <Python> Joel Davis
195 <Lua> Winston Ewert
244 <Perl> kazzmir
252 <C++> ImLeftFooted
364 <C++> Winston Ewert
377 <C++> William Heatley
400 <C++> Oscar Giner
414 <.NET> Rick

By the way its looking i think Matt with php is gonna take it.

[Edit:]
Or by language:

  • Php - 134

  • Haskell - 143

  • Mathematica - 149

  • Perl - 160

  • Python - 167

  • Lua - 195

  • C++ - 252

  • .NET - 414

Mars
Member #971
February 2001
avatar

You should remove the entries that don't get the formatting right...

--
This posting is a natural product. The slight variations in spelling and grammar enhance its individual character and beauty and in no way are to be considered flaws or defects.

Oscar Giner
Member #2,207
April 2002
avatar

Quote:

By the way its looking i think Matt with php is gonna take it.

Not too fast 8-). 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
avatar

<?
        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
avatar

.. Perl-fu didn't come through at first yet?
Surely somebody with enough perl knowledge can do this in much less, right? There's so many different ways to do things in perl, I'd think it would always come in first.. especially for problems like this.

Oh well, good job (so far) ML. :)

Oscar Giner
Member #2,207
April 2002
avatar

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 ;D

ReyBrujo
Moderator
January 2001
avatar

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.

1open F, "t";
2 
3while (<F>) {
4 split;
5 @f{@_[1]} .= " @_[0]"
6}
7 
8for (%f) {
9 ++$c;
10 
11 if (split == 1) {
12 print $c % 2? "
13$_" : $_;
14 next
15 }
16 
17 print "
18 $_" for @_
19}

(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).

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Colin O'Leary
Member #3,233
February 2003

More Perl:

1open F, "n";
2 
3map {
4 split;
5 push @{$h{$_[1]}}, $_[0];
6} <F>;
7 
8$"="
9 ";
10
11print @$a < 2 ? "$k @$a[0]
12" : "$k
13 @$a
14"
15 while ($k, $a) = each %h

109 :P

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
avatar

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!

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Rash
Member #2,374
May 2002
avatar

1#include <fstream>
2#include <map>
3#include <iostream>
4#define P <string,string>
5#define M multimap P
6#define C cout<<
7#define S ->second
8#define N <<'\n'
9using namespace std;
10 
11int main(int,char* a[])
12{
13 ifstream f(a[1]);
14 M m;
15 string i,l;
16 while (f>>i>>l)
17 m.insert(pair P(l,i));
18 for (M::iterator t=m.begin();t!=m.end();) {
19 int c=m.count(i=t->first);
20 if (c>1) {
21 C i<<"\n\t"<<t S N;
22 ++t;
23 while (--c>0)
24 C"\t"<<(t++) S N;
25 }
26 else
27 C i<<' '<<(t++) S N;
28 }
29}

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

--
"Either help out or stop whining" - Evert

Oscar Giner
Member #2,207
April 2002
avatar

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 :o

 1   2   3   4 


Go to: