Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

TMTOWTDI Puzzle: Generating a range of numbers

by Masem (Monsignor)
on Dec 19, 2001 at 03:23 UTC ( [id://132969]=perlmeditation: print w/replies, xml ) Need Help??

As a sort of different puzzler than golf, and thinking about a different problem today, I propose the following puzzle:

How many ways can one generate the list ( 1..10 ) in perl, with the following stipulations:

  • You cannot use any modules
  • You cannot use any digit [0-9] in your code.
  • You cannot use any mathematical operator (+, -, /, *, **, %, ++, --, and the equivalent +=, etc) or mathematical function (such as int, sqrt, sin, or atan2).
Any other aspect of perl is fair game, as this is an attempt to see how many ways, both unique and creative, that this can be done.

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important

Replies are listed 'Best First'.
(tye)Re: TMTOWTDI Puzzle: Generating a range of numbers
by tye (Sage) on Dec 19, 2001 at 05:35 UTC

    !$[..ord$/ Oops, you said this wasn't golf...

            - tye (but my friends call me "Tye")

      9 strokes?

      print s.. ...ord

      Update

      As tye notes this is really 10 strokes as the newline is a required element. It is of course simply a modification of tye's original theme above.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: TMTOWTDI Puzzle: Generating a range of numbers
by Juerd (Abbot) on Dec 19, 2001 at 03:56 UTC
    This one isn't too hard...
    print "$_\n" for length("x")..length("xxxxxxxxxx");
    No module, no digit, no mathematical function.

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: TMTOWTDI Puzzle: Generating a range of numbers
by larsen (Parson) on Dec 19, 2001 at 03:47 UTC
    Just a silly answer :). When men didn't have digits, they used fingers or sticks to count... ;-)

    my @numbers = ( ['|'], ['|', '|'], ['|', '|', '|'], ['|', '|', '|', '|'], ['|', '|', '|', '|', '|'], ['|', '|', '|', '|', '|', '|'], ['|', '|', '|', '|', '|', '|', '|'], ['|', '|', '|', '|', '|', '|', '|', '|'], ['|', '|', '|', '|', '|', '|', '|', '|', '|'], ['|', '|', '|', '|', '|', '|', '|', '|', '|', '|'], ); foreach( @numbers ) { print scalar @$_; print "\n"; }
      I just couldn't resist it... When I saw they used fingers to count, I immediately thought of counting candles...
      my @numbers = ( ['|'] , ['|','|'] , ['|','|','|'] , ['|','|','|','|'] , ['|','|','|','|','|'] , ['|','|','|','|','|','|'] , ['|','|','|','|','|','|','|'] , ['|','|','|','|','|','|','|','|'] , ['|','|','|','|','|','|','|','|','|'] , ['|','|','|','|','|','|','|','|','|','|'] , ) ; foreach ( @numbers ) { print scalar @$_; print "\n"; }

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: TMTOWTDI Puzzle: Generating a range of numbers
by FoxtrotUniform (Prior) on Dec 19, 2001 at 03:33 UTC

    Good one, Masem!

    my @list = (); my @dummy = qw(one two three four five six seven eight nine ten); while(scalar @dummy) { unshift @list, scalar @dummy; pop @dummy; }

    I was originally going to try something similar with a successor function, using a gibberish string to intify to zero, then I hit on the idea of using scalar @list for numbers, and, well....

    --
    :wq
Re: TMTOWTDI Puzzle: Generating a range of numbers
by danger (Priest) on Dec 19, 2001 at 03:58 UTC

    Here are a few variations:

    $_ = 'aaaaaaaaaa'; my @list; push @list, pos() while /a/g; print "@list\n"; my @list = (length'a' .. length'aaaaaaaaaa'); print "@list\n"; my @b = ('a'..'j'); my @a = ('a'); my @list = @a..@b; print "@list\n"; $_ = 'xaaaaaaaaaa'; my @list = y/x// .. y/a//; print "@list\n"; $_="\n\n\n\n\n\n\n\n\n\n"; open(OUT,"+>out.lines")||die "can't $!"; print OUT; seek OUT,$|,$|; my @list; push @list, $. while <OUT>; print "@list\n"; unlink "out.lines";

    UPDATE: chipmunk pointed out that I used 0 in the seek --- fixed.

Re: TMTOWTDI Puzzle: Generating a range of numbers
by IlyaM (Parson) on Dec 19, 2001 at 04:19 UTC
    My variant:
    $c = 'qwertyasdf'; ($a, $b, @a) = sort map { $a = $_; ord chr grep $_ le $a, split //, $c } split //, $c; print "$a @a $b\n";
    Update: Removed forbiden '+' operation. Updated code even more obfuscated than it was before.

    --
    Ilya Martynov (http://martynov.org/)

Re: TMTOWTDI Puzzle: Generating a range of numbers
by VSarkiss (Monsignor) on Dec 19, 2001 at 09:00 UTC

    Ovid's comment above on "Writing without the letter E" made me think of the Oulipo, a French group that does lots of word and language play. One of the games is a "snowball sentence", in which each word is one letter longer than the last. This one, which goes all the way to 20, is by Dmitri Borgmann, in Language on Vacation.

    my @snowball = qw( I do not know where family doctors acquired illegibly perplexing handwriting; nevertheless, extraordinary pharmaceutical intellectuality, counterbalancing indecipherability, transcendentalizes intercommunications' incomprehensibleness. ); s/\W// foreach @snowball; # punctuation doesn't count ;-) my @answer = map { length } @snowball;

      I once gave a talk to my local Perl Mongers group about the Oulipo, and ways that we could use their ideas in Perl. The slides from that talk are here.

      The Oulipo Compendium is a wonderful book, for anyone who's even vaguely interested in Oulipian ideas. (BooK is also interested in the Oulipo.)

(Ovid) Re: TMTOWTDI Puzzle: Generating a range of numbers
by Ovid (Cardinal) on Dec 19, 2001 at 05:04 UTC
    perl -e "push@_,$_ for reverse q.e...q.n.;for(@_){$_=unpack(q|c|,$_);@ +a=split//;shift@a;$_=join q||,@a};print $_+$l,$/ for reverse@_"

    This worked on Win2K 5.6.1, but dies on Cygwin :(

    Update: It turns out that it works on Cygwin if the double quotes are changed to single quotes. Also, I noticed that I accidentally snuck a + in there :( Kinda like typing a story without the letter 'e'. This will fix that:

    push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split// +; shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @ +A

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: TMTOWTDI Puzzle: Generating a range of numbers
by CubicSpline (Friar) on Dec 19, 2001 at 04:03 UTC
    @a = (); $_ = 'abbcccddddeeeeeffffffggggggghhhhhhhhiiiiiiiiijjjjjjjjjj'; foreach $digit (a..j) { push @a, eval "tr/$digit/x/"; }

    ~CS

Re: TMTOWTDI Puzzle: Generating a range of numbers
by wog (Curate) on Dec 19, 2001 at 04:38 UTC

    (update: the first one has been edited to fix problems/bad style since I first pressed submit. Oopsie.)

    use strict; my @result; my $one = "a" =~ /./; sub add { my($vA,$vB) = @_; ($vA,$vB) = ($vA^$vB,($vA&$vB)<<$one) while $vB; return $vA; } my $val = $one; do { push @result, $val; $val = add($val,$one); } until $one.$one eq $val;

    ... or, without any lines preceeding it in the file it's in ...

    sub f { die if $::f_A; die if $::f_B; die if $::f_C; die if $::f_D; die if $::f_E; die if $::f_F; die if $::f_G; die if $::f_H; die if $::f_I; die if $::f_J; } my @vars = map { "::f_$_" } qw(A B C D E F G H I J); my @result; foreach (@vars) { $$_ = "true"; eval { f() }; push @result, $@ =~ /line ([^.]+)\./; $$_ = undef; }

    update: A very, very cheap solution: (doesn't work on EBCDIC systems, sorry)

    my @result = split " ", "pasarauatawavayaxapq"^"AAAAAAAAAAAAAAAAAAAA";

Re (tilly) 1: TMTOWTDI Puzzle: Generating a range of numbers
by tilly (Archbishop) on Dec 19, 2001 at 11:05 UTC
    I am a bit surprised that nobody has tried this:
    my $start = unpack('b', 'a'); my $end = $start . unpack('b', 'b'); print "$_\n" foreach $start..$end;
Re: TMTOWTDI Puzzle: Generating a range of numbers
by IlyaM (Parson) on Dec 19, 2001 at 15:32 UTC
    Warning! Very fragile. Don't move.

    @list = ( __LINE__,,,,,,,, , __LINE__,,,,,,,,, , __LINE__ ,,,, , __LINE__ ,,,, , __LINE__,,,,,,,,, ,,,,, ,,,,,, , __LINE__,,,,,,,, , , , , , __LINE__ , , , , __LINE__ ,,,,,, , , __LINE__ , , , __LINE__ ,,,,, , ,, );

    --
    Ilya Martynov (http://martynov.org/)

Re: TMTOWTDI Puzzle: Generating a range of numbers
by Rudif (Hermit) on Dec 19, 2001 at 04:54 UTC
    Here is my first shot

    use strict; sub shoot{my@shot=split//,shift;shift@shot;return unless@shot;(shoot(j +oin'',@shot),scalar@shot)} print "@{[shoot('onetimesten')]}\n"
    Rudif
Re: TMTOWTDI Puzzle: Generating a range of numbers
by George_Sherston (Vicar) on Dec 19, 2001 at 05:18 UTC
    Not quite good enough for the poetry page, but it gets the job done:
    my $obsession = 'Perlmonks!'; my @day; while ($obsession) { push @day, $obsession; chop $obsession; } print length $_, "\n" for reverse @day;
    ... or actually combining this with what Rudif did maybe:
    my $obsession = 'Perlmonks!'; my @day; while ($obsession) { push @day, $obsession; chop $obsession; print scalar @day, "\n"; }


    § George Sherston
Re: TMTOWTDI Puzzle: Generating a range of numbers
by toma (Vicar) on Dec 19, 2001 at 11:36 UTC
    Are logical operators too obvious for this sort of challenge?
    #!/usr/bin/perl use strict; my ($one,$two,$three,$four,$five,$six,$seven,$eight,$nine,$ten); $one="x" eq "x"; $two= $one << $one; $three= $one | $two; $four= $two << $one; $five= $four | $one; $six= $four | $two; $seven= $four | $two | $one; $eight= $four << $one; $nine= $eight | $one; $ten= $eight | $two; print "$one $two $three $four $five $six $seven $eight $nine $ten\n";

    This sort of thing comes up in computer design. On the long path from transistors to programs, implementations are crushingly brute-force!

    It should work perfectly the first time! - toma

Re: TMTOWTDI Puzzle: Generating a range of numbers
by hossman (Prior) on Dec 19, 2001 at 05:42 UTC
    You cannot use any digit [0-9] in your code

    Which says nothing about the input. Sometimes the simplest solutions are the best...

    perl -le '@nToM=(shift..shift); print join(",",@nToM);' 1 10
(crazyinsomniac) Re: TMTOWTDI Puzzle: Generating a range of numbers
by crazyinsomniac (Prior) on Dec 19, 2001 at 10:11 UTC
    use the dl code link, as the nelines after __END__ matter!
    update: thanks to blakem, I added ord to correctly solve this "puzzle", d'oh!
    #!/usr/bin/perl -l # use dl code, important print while <DATA>; print for ord 'f'>>'o'..$.; __END__ I am smelling like a rose that somebody gave me on my birthday deathbed!

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Re: TMTOWTDI Puzzle: Generating a range of numbers
by andye (Curate) on Dec 19, 2001 at 16:59 UTC
    !$\..qw(P e r l M o n k s !)

    andy.

    update: works under 5.005_03 but not v5.6.1. The later version needs this:

    !$\..@{[qw(P e r l M o n k s !)]}
Re: TMTOWTDI Puzzle: Generating a range of numbers
by runrig (Abbot) on Dec 19, 2001 at 05:28 UTC
    In a roundabout way...:
    use strict; use warnings; open(FH, ">tmp.txt") or die "OH NOOO!: $!"; print FH "x"; close FH; my (@done, @array) = qw(Why the heck am I even writing this stupid program?); { open(FH, ">>tmp.txt") or die "OH NOOO!: $!"; push @array, -s FH; print FH "x"; close FH; last if @array eq @done; redo; } print "@array\n";
    Changed '==' to 'eq' since '==' might be considered a mathmatical operator :-)
Re: TMTOWTDI Puzzle: Generating a range of numbers
by premchai21 (Curate) on Dec 19, 2001 at 08:21 UTC
    my $foo = 'HelloWorld!'; my @bar; do { chop $foo; push @bar, length $foo } while ($foo); pop @bar; @bar = reverse @bar; @bar;

    Not the most original, probably, but it works.

Re: TMTOWTDI Puzzle: Generating a range of numbers
by jynx (Priest) on Dec 19, 2001 at 11:27 UTC

    How about a mysterious tack?

    The first is my favorite, and doesn't remotely work under warnings or strict. The second is a recusive solution for fun. The third is the trivialization of both solutions.

    #1 $$_=~/((((((((((.))))))))))/for length$"..length$"x($^F<<$^F).$"x$^ +F; print map$_.$",sort{chr($a)cmp chr($b)}grep!($|eq$_)&&/\d/,keys%:: #2 sub r{ ($_,$a)=@_;$;=join'',/(.)/,$_;print$",length;r($;,$a)if!(length$; +eq$a) } r$",length$"x($^F<<$^F).$"x$^F.$" #3 print $_ for length$"..length$"x($^F<<$^F).$"x$^F
    Thanx for the fun,
    jynx

    update: d'oh, atcroft beat me to recursion...
    update2: reformatted, hopefully it looks better

Re: TMTOWTDI Puzzle: Generating a range of numbers
by atcroft (Abbot) on Dec 19, 2001 at 10:58 UTC

    My first attempt at one of the puzzles on here (I think someone had a suggestion similar to this). Thought it might be appropriate, though.

    #!/usr/bin/perl -- -l -w my $msg = "happy b-day perl"; $msg =~ s/\s*|perl//g; &gen($msg); sub gen { my ($st) = @_; if (length($st)) { my @temp = split('', $st); shift(@temp); &gen(join('', @temp)); print(length($st), ' '); } }
Re (tilly) 1 (err): TMTOWTDI Puzzle: Generating a range of numbers
by tilly (Archbishop) on Jan 02, 2002 at 14:51 UTC
    If it is human to err, this is a very human approach:
    for ("a".."j") { eval "c("; $@ =~ /\d+/; print "$&\n"; }
Re: TMTOWTDI Puzzle: Generating a range of numbers
by petral (Curate) on Dec 24, 2001 at 21:43 UTC
    Here's some typographical nonsense (from a long week-end):
    s|^$|$^|s; s|.|.|g; s|\./\|\.$|"/".y/././.$"|e while/\./; s|/||; s|.$|$/|; print
    And only one word before the final print (which can be changed to split to comply literally with "generate a list".

    update: OK, here's one with no words that only works with 5.6.1 or later. (and now I really have to quit.)
    s|^$|$^.|s; s|.|.|g; s|.|$`=~y/.//.$"|ge; s|..||; s|.$|$/|; print
    update too Well, one more, now I really will quit:
    s|^$|$^|s; s|.|"$\"$`"=~y/M_A-T_H OUT//.$"|ge; s|.$|$/|s; print


      p
Re: TMTOWTDI Puzzle: Generating a range of numbers
by ravendarke (Beadle) on Dec 21, 2001 at 00:27 UTC
    I don't believe I've seen this one yet:
    $a="lalalalala"; while($a=~m/./g){ print pos $a; }
    If I am mistaken, I apologize. If not, I apologize for the apology.
    Marty
Re: TMTOWTDI Puzzle: Generating a range of numbers
by petral (Curate) on Dec 22, 2001 at 00:42 UTC
    Nobody's done this:
    perl -e 'print "@{[ map{ ($/.=$_)=~s/././g } $^=~/./g ]}\n"'
    ($^ is "STDOUT_TOP")

      p
Re: TMTOWTDI Puzzle: Generating a range of numbers
by Anonymous Monk on Dec 19, 2001 at 21:29 UTC
    perl -e 'foreach (a..j) { print index(" abcdefghij",$_) . "\n"; }'
Re: TMTOWTDI Puzzle: Generating a range of numbers
by Anonymous Monk on Dec 20, 2001 at 03:41 UTC
    OT - not really a Perl solution, and relies on default conditions. Probably breaks the 'no modules' rule as well.

    perl -e "`head /etc/group | grep -n . | sed -e 's/:.*//'`"

    Just my $.02

Re: TMTOWTDI Puzzle: Generating a range of numbers
by Anonymous Monk on Dec 20, 2001 at 16:20 UTC
    Another combination of some of the ideas already presented...
    perl -e 'print(length(O)..length(O).length())'
Re: TMTOWTDI Puzzle: Generating a range of numbers
by Anonymous Monk on Dec 20, 2001 at 22:27 UTC
    the obvious, probably
    foreach $Foo ('t','hi','isj','usta','funky','little','thingiw','roteto +do','thisthing','timtowtdi!') { print length($Foo),"\n"; }
Re: TMTOWTDI Puzzle: Generating a range of numbers
by ixo111 (Acolyte) on Dec 20, 2001 at 22:33 UTC
    or
    do { $Foo .= "x"; print length($Foo),"\n"; } until (length($Foo) eq length('timtowtdi!'));

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://132969]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found