The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

I was given a challenge for Perl; by caffine-iv 2003-01-19 02:34:14
to write a program that swapped two or more variables without using an intermediate variable. I replied with:
#!/usr/bin/perl

use strict;

print "Variable a: "; chomp(my $a = <STDIN>);
print "Variable b: "; chomp(my $b = <STDIN>);
print "Variable c: "; chomp(my $c = <STDIN>);
print "Variable d: "; chomp(my $d = <STDIN>);
print "Variable e: "; chomp(my $e = <STDIN>);

print "a = $a\n";
print "b = $b\n";
print "c = $c\n";
print "d = $d\n";
print "e = $e\n";

($a, $b, $c, $d, $e) = ($e, $d, $c, $b, $a);
print "\nSwapped\n\na = $a\nb = $b\nc = $c\nd = $d\ne = $e\n";
To which I was told:
Doesn't count , you used an intermeadit  variable  ,  you placed
them into a anonomous array  befor you swaped them .  ($a,$b) is
the same as  _@ = ($a,$b);

sorry , try again !
I told him:
By that reasoning, no programming language on earth can do it.

All programming languages makeup some sort of a spot in memory
to do the swap.

Perl just happens to give it a name.
He sais back to me:
Well , Tim asked me the same challenge  ,  and after pondering it
for a wile I had to tell him Perl couldn't do it  just because of
the use of an intermitant var. I think the languages HE says can
do it "REBOL"  also use an intermitant var , they just don't
document it ! so  In a sence your right  , now we just got to get
Tim to admit  the other languages  also process  the  swap using
an unseen  VAR  and he is just not aware of it !
What do you all think?
[ Reply ]
  Thats what I think about that... by Gorash2003-01-19 02:59:05
    a, b, c integers will work using xor by SMcV2003-01-19 06:38:23
      s,y^x$,y^x */, by SMcV2003-01-19 06:39:49
      Xyzzy! by Beorn2006-11-19 12:55:59
  You could cheat by Bellator2003-01-19 03:15:37
  There is a logical solution... by anymouse2003-01-19 03:35:19
    Wether or not use a register... by Gorash2003-01-19 03:52:02
  How about this ? by Yaeger2003-01-19 04:49:15
    well.. by Gorash2003-01-19 05:00:43
      Ehm.... by Yaeger2003-01-19 05:16:35
  How about this? by zigrin2003-01-19 05:26:05
    improved version :) by zigrin2003-01-19 05:54:52
  Use bitwise-XOR by Stavros2003-01-19 06:15:51
    If your challenger is really picky, by SMcV2003-01-19 06:42:41
  Let's try it in MPASM assembler: by Tomo2003-01-19 06:34:32
    Unless it's built into the processor, by kahuana2003-01-19 09:17:06
  OK OK by Gorash2003-01-19 09:40:22
  Interesting... by Spazmatic2003-01-19 09:40:41
    Stavros' XOR solution doesn't risk overflows by kahuana2003-01-19 09:52:32
      Overflow isn't really a problem. by Beorn2003-01-19 11:37:40

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)