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
Derezo
Member #1,666
April 2001
avatar

It's official: I now hate perl.
What the heck is up with Colin's code? That's crazy.

"He who controls the stuffing controls the Universe"

kazzmir
Member #1,786
December 2001
avatar

Quote:

It's official: I now hate perl.

I used to hate perl as well until I was forced to learn it a little while ago.

Dude. its. awesome.

Its such a great language to manipulating text in. I havent found an language that can manipulate text in better, including c,c++,java,python,scheme,shell.

ReyBrujo
Moderator
January 2001
avatar

Well, maybe because PERL was created exactly for that (Practical Extraction and Report Language). And Colin is doing a very clever thing in push @{$h{$_[1]}}, $_[0];: he is using a hashed list in which every value is an array, and then using the $" variable (which I did learn now it keeps the default separator to be used in an array when printing it whole; by default it is a space, but he changes it to "\n\t") to make Perl output it directly. In my code, I use a hashed list of strings instead, so I need an extra split to convert the string into an array.

I guess it is still possible to make it < 100 characters.

Here is my possibly last version, using his suggestion to turn the while into a map (both basically do the same, but the later needs less characters):

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

107.

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

Billybob
Member #3,136
January 2003

/me looks at perl.
/me looks at Derezo's avatar.
Yep, that's what I look like now.

23yrold3yrold
Member #1,134
March 2001
avatar

It's true; Perl really does look like a cartoon character swearing. :)

Anyway, good job getting it down to 107. Holy crap.

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

Colin O'Leary
Member #3,233
February 2003

Perl's not so bad if you're not using one character variables, (ab)using map instead of while, and using default variables everywhere.

Here's a more readable version (assuming you know Perl :))

1open FILE, '<', "names.txt";
2 
3while($line = <FILE>)
4{
5 ($first, $last) = split / /, $line;
6 push @{$names{$last}}, $first;
7}
8 
9foreach $last (keys %names)
10{
11 @first = @{$names{$last}};
12 
13 if(@first == 1)
14 {
15 print "$last $first[0]\n";
16 }
17 else
18 {
19 print "$last\n";
20 
21 foreach $first (@first) {
22 print "\t$first\n";
23 }
24 }
25}

And by the way, 99 characters (not including whitespace):

1open F, 0;
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

Naming the file "0" saves a pair of quotes.

Dustin Dettmer
Member #3,935
October 2003
avatar

#1

Quote:

\\\\\\\\\\\\\\\\\\\\\\\\\\\` \\\\\\\\\\\\\\\
\||||||||||||||||||||||||||| \|||||||||||||||
\|||@@@@||@@@@||@@@@||@||||| \|||@@@@||@@@@||
\|||@||@||@|||||@||@||@||||| \|||@||@||@||@||
\|||@@@@||@@@|||@@@@||@||||| \|||@@@@||@@@@||
\|||@|||||@|||||@|@|||@||||| \||||||@|||||@||
\|||@|||||@@@@||@||@||@@@@|| \||||||@|||||@||
\||||||||||||||||||||||||||| \|||||||||||||||

- - - - Colin O'Leary
#2 107 <Perl> ReyBrujo
#3 124 <Mathematica> Oscar Giner4
#4 129 <Python> Elias
#5 132 <Php> Matthew Leverton
#6 143 <Haskell> X-G
#7 167 <Python> Mars
#8 178 <Python> Joel Davis
#9 195 <Lua> Winston Ewert
#10 244 <Perl> kazzmir
#11 252 <C++> ImLeftFooted
#12 364 <C++> Winston Ewert
#13 377 <C++> William Heatley
#14 400 <C++> Oscar Giner
#15 414 <.NET> Rick

Thomas Fjellstrom
Member #476
June 2000
avatar

Damn you Colin, steal my thunder >:E ;)

Perl:

map {
  split;
  push @{$h{$_[1]}}, $_[0]
} `cat n`;
for $l (keys %h) {
  print "$l " . shift(@{$h{$l}}) . "\n";
  print "\t$_\n" for (@{$h{$l}});
}

114 chars (give or take). And thats without resorting to that evil crud :P

edit: moding for that last while loop...

map {
  split;
  push @{$h{$_[1]}}, $_[0]
} `cat n`;
$"="
  ";
print @$a < 2 ? "$k @$a[0]
" : "$k
  @$a
" while ($k, $a) = each %h

Now to count...

woo hoo, 95 :)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

ReyBrujo
Moderator
January 2001
avatar

But it forces you to have cat...

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

Thomas Fjellstrom
Member #476
June 2000
avatar

I don't see a problem with that. If you have perl on windows, you're going to know how to fix it ;)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

CascoOscuro
Member #4,966
August 2004
avatar

Quote:

jane fonda
chuck norris
ted fonda
kyle boop

In prolog:

surname(jane,fonda).
surname(chuck,norris).
surname(ted,fonda).
surname(kyle,boop).

And then in the interpreter command line write

surname(X,fonda).

(for example).

Rash
Member #2,374
May 2002
avatar

Only Oscar Giner and I have posted correct C++ code.

Dustin Dettmer
Member #3,935
October 2003
avatar

Quote:

map {
split;
push @{$h{$_[1]}}, $_[0]
} `cat n`;
$"="
";
print @$a < 2 ? "$k @$a[0]
" : "$k
@$a
" while ($k, $a) = each %h

Now to count...

woo hoo, 95 :)

Curse you:P. Now i have to change the 99 to 95. By the way, did you purposely leave off the last " there, or was it a copying error?

ReyBrujo
Moderator
January 2001
avatar

Hmm... well, now that we are cheating ;)

map {
  split;
  push @{$h{$_[1]}}, $_[0]
} <>;
$"="
  ";
print @$a < 2 ? "$k @$a[0]
" : "$k
  @$a
" while ($k, $a) = each %h

Execute it like cat file.txt | perl test.pl. Here is the 91 characters entry Colins' was talking about.

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

Dustin Dettmer
Member #3,935
October 2003
avatar

I think that shouldnt be allowed...

main(int, char **v) {
 printf(v[1]);
}

Comes in at 32

Edit:
execute it using some weird bash command
prog "`<something> \"\`cat file.txt\`\"`"

ReyBrujo
Moderator
January 2001
avatar

:D Well, I just don't agree with the idea of using external aid, like cat. He might later change `cat n` with `c n` and say you need to rename cat to c for it to work ;)

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

Dustin Dettmer
Member #3,935
October 2003
avatar

Got it down to 11, using a custom compile command:

[code entry.cpp]
#include "c"
</code>

The compile command is:
g++ entry.cpp -o entry `echo -e "#define S string\n#include <iostream>\n#include <fstream>\n#include <map>\nusing namespace std;main ( ) {map < S, S > p ;fstream s ( \"t\" ) ;for ( S e, x ; s.good ( ) ; p [ x ] += \"\t\" + e + \"\\\\\\\\n\" )s >> e >> x;S y;for ( map < S, S > :: iterator P = p.begin ( ) ; ; cout << P -> first << ( y . rfind ( \"\t\" ) != 0 ? \"\\\\\\\\n\" : \"\" ) << y, ++ P )y = P -> second;}" > c`

Edit:
Made the compile command actually one command

Edit2: Make that 10 characters
[code entry.cpp]
#include " "
</code>
g++ entry.cpp -o entry `echo -e "#define S string\n#include <iostream>\n#include <fstream>\n#include <map>\nusing namespace std;main ( ) {map < S, S > p ;fstream s ( \"t\" ) ;for ( S e, x ; s.good ( ) ; p [ x ] += \"\t\" + e + \"\\\\\\\\n\" )s >> e >> x;S y;for ( map < S, S > :: iterator P = p.begin ( ) ; ; cout << P -> first << ( y . rfind ( \"\t\" ) != 0 ? \"\\\\\\\\n\" : \"\" ) << y, ++ P )y = P -> second;}" > " "`

Wetimer
Member #1,622
November 2001

Actually...

While we are doing wierd cheats. Define the entire program as a preprocessor symbol. You won't be able to include, so you'll have to provide your own prototypes and such. But you should be able to define the symbol a as your entire program, there by doing it in one letter.

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

Thomas Fjellstrom
Member #476
June 2000
avatar

Oh come on, cat in perl is not a weird cheat :P

Quote:

By the way, did you purposely leave off the last " there, or was it a copying error?

no error, just changed the formatting, colin's dsiplays wrong too. Has to do with that $" variable. the highligter thinks it starts a string.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

 1   2   3   4 


Go to: