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
Joel Davis
Member #3,425
April 2003
avatar

I'd love to see someone solve this in J (link) or APL, the most concise programming language in existance. (J is like APL without the need for a special font)

Oscar Giner
Member #2,207
April 2002
avatar

Rick: look better. I use string three times.

Rick
Member #3,572
June 2003
avatar

Sorry, missed it in those #defines.

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

BAF
Member #2,981
December 2002
avatar

oh yeah? I just invented baf++. The compiler is still under development, but it has built in support for everything :)

Its syntax is similar to c, but is as highlevel as anything can get. I have alraedy ported allegro 5 to it.

#include <allegro5.h>
read_mind(do_first_last_name_sorting_stuff());
END_OF_PROGRAM(())

Kitty Cat
Member #2,815
October 2002
avatar

Bah. I stole an advanced copy of Allegro 5 from Bob when he moved from Canada. It works in plain C. BTW, you're not taking advantage of hardware acceleration that way. :P
#include <allegro/allegro5.h>*thinks*

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Rick
Member #3,572
June 2003
avatar

Had to find the right .NET class that simulated a stl map

1Imports System.IO
2Imports System.Collections.Specialized
3 
4Module Module1
5 Sub Main()
6 Dim f As StreamReader = File.OpenText("File.txt")
7 Dim a As New NameValueCollection
8 Dim i, j As Integer
9 Do Until f.Peek = -1
10 Dim str() As String = f.ReadLine().Split(" ")
11 a.Add(str(1), str(0))
12 Loop
13 Dim s() As String
14 s = a.AllKeys
15 For i = 0 To s.Length - 1
16 Dim str() As String = a.GetValues(s(i))
17 If str.Length > 1 Then
18 Console.WriteLine(s(i))
19 For j = 0 To str.Length - 1
20 Console.WriteLine(Chr(9) & str(j))
21 Next
22 Else
23 Console.WriteLine(s(i) & " " & str(0))
24 End If
25 Next
26 f.Close()
27 End Sub
28End Module

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Billybob
Member #3,136
January 2003

BAH, My code is perfect!
Kay, I fixed the typos:

1#include <map>
2#include <string>
3#include <vector>
4using namespace std;
5map<string,vector<string> > a;
6int main()
7{
8FILE *f = fopen("file.txt","rb");
9while(!feof(f))
10{
11char t[9],h[9];
12fscanf(f, "%s %s\n",t,h);
13a[h].push_back(t);
14}
15 
16for(map<string,vector<string> >::iterator q = a.begin(); q != a.end(); ++q)
17{
18printf("%s\n", q->first.c_str());
19for(int i = 0; i < q->second.size(); ++i)
20printf("\t%s\n", q->second<i>.c_str());
21}
22}

Quote:

I made it compile but the output generated is not the one the problem defines

Just because the program doesn't output like the example, doesn't mean it isn't correct. The example given by the OP is incorrect:

Quote:

generate a table where anyone with the same last name is assumed to be related and the first names put underneath the last name, indented by one tab.

EDIT: Code updated as of...6:59 PM PST

ReyBrujo
Moderator
January 2001
avatar

Hmm... I didn't like this Perl code too much. I notice I am the only one that respect the output format (if only one name with a surname, print them in the same line, otherwise print the surname first, and then all the names below) :P I use a hash to prevent repeating surnames too.

1my %h;
2my @f;
3 
4open(F, "<test.txt") or die $!;
5@f = <F>;
6close(F);
7 
8while ($_ = shift(@f)) {
9 my @s = split;
10 my $f = " ";
11 my @i = @f;
12 shift @i;
13 next if (exists(%h->{$s[1]}));
14 
15 print $s[1];
16 for (@i) {
17 my @c = split;
18 if ($s[1] eq $c[1]) {
19 print "\n $c[0]";
20 $f = "\n ";
21 }
22 }
23 print "$f$s[0]\n";
24 %h->{$s[1]} = 1;
25}

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

Matthew Leverton
Supreme Loser
January 1999
avatar

Quote:

I notice I am the only one that respect the output format (if only one name with a surname, print them in the same line, otherwise print the surname first, and then all the names below)

Mine is right!

ReyBrujo
Moderator
January 2001
avatar

Awww... that is why I still don't understand PHP :P

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

X-G
Member #856
December 2000
avatar

Quote:

I notice I am the only one that respect the output format

Mine also respects that output format. Go back and read my code. What's that you say? You can't figure it out? :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

ImLeftFooted
Member #3,935
October 2003
avatar

154 <Php> Matthew Leverton
178 <Python> Joel Davis
219 <C++> ImLeftFooted
240 <Perl> ReyBrujo
254 <Haskell> X-G
364 <C++> Winston Ewert
377 <C++> William Heatley
400 <C++> Oscar Giner
414 <.NET> Rick

Values calculated using `cat foo | tr -d ' ' | tr -d '\t' | tr -d '\n' | wc -c`

My 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 for ( map < S, S > :: iterator P = p.begin ( ) ; ; ++ P )
17 cout << P -> first << P -> second ;
18}

I think I can safely say that mines the first thats trying to say something at the same time...

Also note that the instructions said nothing about how the program had to exit... ;D

Its also attatched to preserve the tabs if someone wants to compile it

kazzmir
Member #1,786
December 2001
avatar

meh, another losing entry.. ill try again in a different language.

1open( FILE, "<n" );
2while (<FILE>){
3 s/(\w+)\s*(\w+)/$f=$1;$l=$2;/e;
4 if ( ! defined( $tr{$l} ) ){
5 $tr{$l} = [ $f ];
6 } else {
7 $b = $tr{$l};
8 push @$b, $f;
9 }
10}
11while ( ($k,$h) = each(%tr) ){
12 print "$k ";
13 if ( $#$h == 0 ){
14 print $$h[0] . "\n";
15 } else {
16 print "\n";
17 for ( @$h ){
18 print "\t$_\n";
19 }
20 }
21}
22close( FILE );

X-G
Member #856
December 2000
avatar

Fear not! Me and Jolle are working on a compacted version of my Haskell entry above. So far we're down to 173 characters.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Oscar Giner
Member #2,207
April 2002
avatar

Quote:

I notice I am the only one that respect the output format (if only one name with a surname, print them in the same line, otherwise print the surname first, and then all the names below)

And mine also respects it :P

Quote:

364 <C++> Winston Ewert
377 <C++> William Heatley
400 <C++> Oscar Giner

Note that Winston and Williams's code doesn't produce the correct output. Neither yours does (and it crashes) :P

Mars
Member #971
February 2001
avatar

135 characters in Python, but the formating isn't right:

i = [a.split() for a in file("n").readlines()]
n = {}
for a in i:
    try:
        n[a[1]] += [a[0]]
    except:
        n[a[1]] = [a[0]]
for l,f in n.iteritems():
    print l, "\n\t".join(f)

167 characters in Python with correct formatting:

i = [a.split() for a in file("n").readlines()]
n = {}
for a in i:
    try:
        n[a[1]] += [a[0]]
    except:
        n[a[1]] = [a[0]]
for l,f in n.iteritems():
    if len(f) < 2:
        print l, f[0]
    else: 
        print l, "\n\t".join([""]+f)

The other Python entry doesn't get the formatting right and is unreadable and longer. ;D

Matthew, your Python interpreter is outdated.

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

Matthew Leverton
Supreme Loser
January 1999
avatar

Actually, my entry is 148 if you use my shorter method. It just spits out some notices if you have PHP on strict "debug" mode. My nonconforming entry (placing all first names below last names) is 128.

Surely someone do much better than that though... PHP has long names like "foreach", and I have to resort to using a trim(). It also doesn't have the shortcut stuff like: i = [a.split() for a in file("n").readlines()]

Marcello
Member #1,860
January 2002
avatar

Quote:

It also doesn't have the shortcut stuff like [...]

... Which is a good thing.

Marcello

X-G
Member #856
December 2000
avatar

Haskell is back again to save the day!

z x (n:b)
  | n!!0 == x!!1 = [n!!0,"\n  ",x!!0 ++ "\n  " ++ n!!2] : b
  | 0 < 1 = n : z x b
z [a,b] _ = [[b," ",a]]

main = readFile "f" >>= putStr . unlines . map concat . foldr (z . words) [] . lines

This little baby clocks in at 143 non-whitespace characters and gets the formatting right. Shortest so far, then. Co-produced by me and Jolle.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Matthew Leverton
Supreme Loser
January 1999
avatar

Mine's 144 now. If PHP just didn't require those <? ?> tags. :-/

However, I think essential white space (anything required for parsing or output) should count.

Mine runs like this:

<?foreach(file("n")as$l){$p=split(" |\n",$l);$n[$p[1]][]=$p[0];}foreach($n as$a=>$z)foreach($z as$i=>$p)echo($z[1]?$i?"\t":"$a\n\t":"$a ")."$p\n";?>

149 bytes short.

X-G: Does yours output a tab character?

ReyBrujo
Moderator
January 2001
avatar

If you turn warnings off, this version of Perl would work as well, and has only 205 characters:

1open(F, "t");
2@f = <F>;
3 
4while ($_ = shift(@f)) {
5 @s = split;
6 $f = " ";
7 @i = @f;
8 shift @i;
9 
10 if (! exists(%h->{$s[1]})) {
11 print $s[1];
12 for (@i) {
13 @c = split;
14 if ($s[1] eq $c[1]) {
15 $f = "\n ";
16 print "$f$c[0]";
17 }
18 }
19 
20 print "$f$s[0]\n";
21 %h->{$s[1]} = 1;
22 }
23}

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

X-G
Member #856
December 2000
avatar

Quote:

X-G: Does yours output a tab character?

I'd tell you, but I'm running out of posts. :P

(Yes.)

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

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
";
?>

Splendid! Mine's 134 now by the cat/wc count. 8-) I'm using literal tabs and newlines instead of \codes now. (Although, I still say it's no fair to discard required whitespace...)

I don't know if I can make it any shorter without rethinking. I better get back to some real work. :P

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

Splended!

I didn't know there was a splending contest ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Matthew Leverton
Supreme Loser
January 1999
avatar

Eh? Where's your C++ STL is teh foo code?

 1   2   3   4 


Go to: