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
kazzmir
Member #1,786
December 2001
avatar

Its time for another programming challenge. The idea is to solve the given question with the fewest number of characters, in any language. Whitespace will be removed for counting, so dont bother to make your code look clean in the thread.

The problem:
Given an ascii text file with a list of names( first, last ), 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. Order doesnt matter
If the text file is this:

jane fonda
chuck norris
ted fonda
kyle boop

The result should look like

fonda
   jane
   ted
boop kyle
norris chuck

Ordering of last names is arbitrary. Ill post an answer later on.

Remember, any language and fewest number of characters. Or feel free to come up with a better problem.

spellcaster
Member #1,493
September 2001
avatar

Quote:

Whitespace will be removed for counting, so dont bother to make your code look clean in the thread.

Here's my whitespace version of the code:



Since whitespace are not counted, I think I won.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Kris Allen
Member #4,639
May 2004
avatar

Quote:

Since whitespace are not counted, I think I won.

Not if your whitespace program doesn't do what it needs to, which I don't think it does!

- Kris

Matthew Leverton
Supreme Loser
January 1999
avatar

I tried it out in my secret compiler, and it worked. :o

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

Rick
Member #3,572
June 2003
avatar

I agree. You could use VB.NET and it would kill most C/C++ programs due to the fact VB.NET does most of the work for you. It's not really fair. When you say table, what do you mean? You want this in a database, or just back into another file?

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

Billybob
Member #3,136
January 2003

I dunno, i guess I'll try:

1#include <map>
2#include <string>
3#include <vector>
4using namespace std;
5map<string,vector<string>> a;
6void 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->second.c_str());
19for(vector<string>::iterator o = q->second.begin(); o != q->second.end(); ++o)
20printf("\t%s\n", o->c_str());
21}
22}

ReyBrujo
Moderator
January 2001
avatar

I can do it in Ruby, Perl or VB. In C or C++ would take too much. Off home right now, will post those solutions in around 3 hours (remember, I voted the over 4 hours of travelling to work in a poll here time ago :P).

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

X-G
Member #856
December 2000
avatar

Obfuscated Haskell for everyone!

1s b (q:r) = s (z q b) r
2s a [] = a
3z (f:l:_) ((a,c):b)
4 | a == l = ((a,f:c):b)
5 | True = (a,c):(z (f:[l]) b)
6z (f:l:_) [] = [(l,[f])]
7 
8o ((l,f):r) = (l++(y f)++"\n")++(o r)
9o [] = []
10y (a:[]) = ' ':a
11y (a:b) = m (a:b)
12m (a:b) = ("\n\t"++a)++(m b)
13m [] = []
14 
15g = o . (s []) . (map words) . lines
16 
17main = do
18 x <- readFile "foo.txt"
19 putStr (g x)

EDIT: Made it somewhat shorter.
EDIT2: Made it even shorter...

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

Matthew Leverton
Supreme Loser
January 1999
avatar

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

Note: disable debug "notices" when running.

BAF
Member #2,981
December 2002
avatar

baf langauge

(*%)#*)R(#*()#F*I()R#)T#*%()#*)%&(#*&(*)R#^T#@^#T)"|::|:}|:}{|#58927389u*(&*(#%&(*35

and in elite c++: (care of l33t-sp34k g3n3r4t0r)

1#INCluDe <m4P>
2#1nClUD3 <S+RiN9>
3#iNCLuDE <veCtOr>
4uS1N9 NAm35P@C3 5tD;
5m4P<StR1NG,V3C+0r<S+R1N9>> 4;
6v0ID m@1N()
7{
8F1lE *f = PhOpEN("PHiLe.TXT","RB");
9wH1lE(!Ph3oPh(ph))
10{
11Ch4r t[9],h[9];
12Ph$C@nF(ph, "%5 %S\N",T,h);
134[H].pU5H_BaCK(+);
14}
15 
16PH0R(m4p<STrING,vEc+0r<$+riN9>>::1+3R4T0R Q = A.B3gIn(); Q != A.3nd(); ++Q)
17{
18Pr1N+f("%S\n", q->5eC0nd.c_$+R());
19fOR(veC+0r<$Tr1N9>::1T3R4tor 0 = Q->53C0nd.b391n(); o != Q->5Ec0ND.3nD(); ++o)
20pRInTf("\+%5\n", O->C_5TR());
21}
22}

remember, you have to use the elite g++ port...

LSd016
Member #3,561
May 2003

#/bin/bash
cat file.txt|decapitalise|sort|dostuff|playwith > /dev/tty1

____________________________________________
[update soon]

Wetimer
Member #1,622
November 2001

Well, I thought about doing it in C++, but William Heatley already did so, I took his code and chopped it down a little.

Wacko C++

1#include <map>
2#include <string>
3#include <vector>
4using namespace std;
5#define f(t,x,y) for(t::iterator x = y.begin();x != y.end();x++) {
6#define l typedef;
7l vector<string> v;
8l map<string,v> z;
9z a;
10int main()
11{
12FILE *f = fopen("f","r");
13while(!feof(f))
14{
15char t[9],h[9];
16fscanf(f, "%s %s\n",t,h);
17a[h].push_back(t);
18}
19 
20f(z,q,a)
21printf("%s\n", q->first.c_str());
22f(v,o,q->second)
23printf("\t%s\n", o->c_str());
24}
25}
26}

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

Goodbytes
Member #448
June 2000
avatar

Wetimer
Member #1,622
November 2001

Except while Whitespace can deal with input, it cannot read from files, which is a requirement.

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

Oscar Giner
Member #2,207
April 2002
avatar

Willam: your code doesn't compile :P I made it compile but the output generated is not the one the problem defines (same would apply to derivative work :P).

Here's mine. Base on William's, but it compiles and generates correct output:

1#include <map>
2#include <string>
3#include <vector>
4#include <fstream>
5#include <iostream>
6 
7using namespace std;
8 
9#define F(a, b, c) for(a::iterator b = c.begin(); b != c.end(); ++b)
10#define M map<S,V >
11#define V vector<S>
12#define S string
13#define C cout <<
14#define D ->second
15 
16M m;
17main()
18{
19 ifstream f("file.txt");
20 while(!f.eof())
21 {
22 S a,b;
23 f >> a >> b;
24 m<b>.push_back(a);
25 }
26 
27 F(M, q, m)
28 {
29 C q->first;
30 if (q D.size() > 1)
31 {
32 C endl;
33 F(V, o, q D)
34 C "\t" + *o << endl;
35 }
36 else
37 C " " + q D[0] << endl;
38 }
39}

[edit]
#define I ::iterator wasn't good anymore.

Joel Davis
Member #3,425
April 2003
avatar

Python's list comprehensions rock!

import string
n = dict([string.split(i) for i in open('names.txt').readlines()])
for x,y in dict([(v,string.join( ['']+[k for k in n.keys()
    if v==n[k] ],"\n   "))for v in n.values() ]).iteritems(): print x,y

And so clear and readable.. heh

--Joel

Matthew Leverton
Supreme Loser
January 1999
avatar

Bah, C++ is so primitive compared to PHP. 8-)

I would think a Perl nerd could get the shortest working code.

Joel: I get invalid syntax on yours...

spellcaster
Member #1,493
September 2001
avatar

Quote:

Except while Whitespace can deal with input, it cannot read from files, which is a requirement.

Why? There's no requirement that you have to load that file, so a

type foo.txt > parse

does the trick of reading the names from that file.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Krzysztof Kluczek
Member #4,191
January 2004
avatar

Solution to this problem in Tytwggfdtyuqfef++++-- programming language:

Nobody said that inventing new high level language is forbidden - specification isn't complete yet, but it already can solve this problem. ;)

(and no, language name is not a polish word) ;)

Joel Davis
Member #3,425
April 2003
avatar

Hmm.. i'm using python 2.3.4, I think list comprehensions are pretty recent, anything above python 2.1 should work tho...

And perl could probably do way less characters, it's just sooo distasteful.

edit: oops, i'm using iteritem() so you need python 2.1 for that

edit2: attached the py file as an attachment so you don't have to worry about formatting cut/paste errors.

LSd016
Member #3,561
May 2003

You lie.

"Tytwggfdtyuqfef" == "barman, pour me another!"

____________________________________________
[update soon]

Rick
Member #3,572
June 2003
avatar

Quote:

The idea is to solve the given question with the fewest number of characters

Quote:

#define F(a, b, c) for(a::iterator b = c.begin(); b != c.end(); ++b)
#define M map<S,V >
#define V vector<S>
#define S string
#define C cout <<
#define D ->second

I'm assuming he didn't think about this as being whitespace. If that was the case you could do everything with 1 call. :)

[EDIT]
Anyone ever highlight the smiley face? It turns into an angry chinese person.

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

Matthew Leverton
Supreme Loser
January 1999
avatar

Yeah, yeah, yeah. Enough of the funnies.

Ok,

  File "test.py", line 2
    n = dict([string.split(i) for i in open('names.txt').readlines()])
                                ^
SyntaxError: invalid syntax

Disclaimer: I'm not familiar with Python.

Oscar Giner
Member #2,207
April 2002
avatar

Quote:

I'm assuming he didn't think about this as being whitespace. If that was the case you could do everything with 1 call.

No, that part also counts. But it's smaller doing that.

Rick
Member #3,572
June 2003
avatar

I would take out #define S string since you only use string once in your code, you are wasting 8 characters. That's why I thought you didn't think it counted. Trying to help.

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

 1   2   3   4 


Go to: