Author Topic: Write a rot13 program in one line  (Read 7520 times)

0 Members and 1 Guest are viewing this topic.

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Write a rot13 program in one line
« on: August 15, 2006, 04:06:53 pm »
it must do IO as well
« Last Edit: August 15, 2006, 04:18:21 pm by Ender »

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Write a rot13 program in one line
« Reply #1 on: August 15, 2006, 04:42:05 pm »
Code: [Select]
<?php for($i=0;$i<strlen($_GET['chars']);$i++){$char=substr($_GET['chars'],$i,1);$ca=ord($char);echo((($ca>96&&$ca<123)||($ca>64&&$ca<91))?chr((($ca>96&&$ca<122)?(122-$ca>=13)?($ca+13):(97+(13-(123-$ca))):((90-$ca>=13)?($ca+13):(64+(13-(90-$ca)))))):$char);} ?>

http://www.sidoh.org/~sidoh/rot13.php?chars=hi2u

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Write a rot13 program in one line
« Reply #2 on: August 15, 2006, 04:58:17 pm »
You can't use semicolons, it's no longer a single line! :P

Here's a real one-liner, no semicolons or nothing, handles upper/lower case and punctuation properly:

foreach(<>){foreach(split(//)) { print (((ord($_) > 0x40 && ord($_) < 0x5b) || (ord($_) > 0x60 && ord($_) < 0x7b)) ? chr(((ord($_) - (ord($_) < 0x61 ? 0x41 : 0x61) + 13) % 26) + (ord($_) < 0x61 ? 0x41 : 0x61)) : $_)}}
« Last Edit: August 15, 2006, 05:02:59 pm by iago »

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Write a rot13 program in one line
« Reply #3 on: August 15, 2006, 05:11:21 pm »
You can't use semicolons, it's no longer a single line! :P

Then replace $ca with ord(substr($_GET['chars'],$i,1)) and I win :P

Here's a real one-liner, no semicolons or nothing, handles upper/lower case and punctuation properly:

The one I did did that too...

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Write a rot13 program in one line
« Reply #4 on: August 15, 2006, 05:13:19 pm »
Good job!

Next challenge: Write it in less than 25 characters.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Write a rot13 program in one line
« Reply #5 on: August 15, 2006, 05:16:27 pm »
Then replace $ca with ord(substr($_GET['chars'],$i,1)) and I win :P
You lose!  Using PHP is cheating anyways, you don't have to do I/O :(

The one I did did that too...
I know, I was just saying.. :P

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Write a rot13 program in one line
« Reply #6 on: August 15, 2006, 05:17:47 pm »

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Write a rot13 program in one line
« Reply #7 on: August 15, 2006, 05:19:06 pm »
Good job!

Next challenge: Write it in less than 25 characters.
Mmm, not likely, I doubt I could do less than about 150, including i/o...

Best I can do in Perl, without using CGI, is 73 chars, only works on uppercase/no punctuation:
foreach(<>){foreach(split(//)){$i=ord($_);print chr($i<110?$i+13:$i-13)}}
:(
« Last Edit: August 15, 2006, 05:26:18 pm by iago »

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Write a rot13 program in one line
« Reply #8 on: August 15, 2006, 08:41:41 pm »
Hehe, there's a trick to it. There's a very very quick way to do IO (both an input and an output) in perl (what may be two lines may count as one line, but usually one of the lines I am talking about is not considered a line).
« Last Edit: August 15, 2006, 08:43:26 pm by Ender »

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: Write a rot13 program in one line
« Reply #9 on: August 15, 2006, 09:18:43 pm »
include('a'); echo r($d);

I win.

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Write a rot13 program in one line
« Reply #10 on: August 15, 2006, 09:20:17 pm »
lol no stop looking for workarounds you guys!

Plus, I was vague about what I meant by two lines. There's only one semi-colon (or line-terminating character excepting crlf).

EDIT: K, I'm just going to stop being ambiguous, sorry for being it before. The challenge is 25 characters or less excluding the #! shebang. It can be done in one line (excluding the shebang, and by line I mean a programming statement) but this is not a requirement (although there may be no two-line 25-char-satisfying solution to this). And any external sources or binaries that you use cannot assist you in doing the rot13.

« Last Edit: August 16, 2006, 02:08:25 am by Ender »

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Write a rot13 program in one line
« Reply #11 on: August 16, 2006, 03:04:14 pm »
exec('rot13');

I win. 

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Write a rot13 program in one line
« Reply #12 on: August 16, 2006, 11:59:35 pm »
Okay, here's the answer that involves 25 chars or less. Being only one line, we may as well run this one in interactive mode.

Code: [Select]
perl -p -e "tr/A-Za-z/N-ZA-Mn-za-m/;"

Cool eh? It uses transliterations instead of looping through every character in a string.

Edit: You'll never want to go to rot13.com again! =P
« Last Edit: August 17, 2006, 12:19:49 am by Ender »

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Write a rot13 program in one line
« Reply #13 on: August 17, 2006, 08:19:05 am »
I was wondering if it could be done using tr///, but I didn't know how.  Huh!

Offline Newby

  • Moderator
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: Write a rot13 program in one line
« Reply #14 on: August 17, 2006, 08:51:46 am »
That whole line is 37 characters. Eh?
- Newby
http://www.x86labs.org

Quote
[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT. 

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Write a rot13 program in one line
« Reply #15 on: August 17, 2006, 09:01:16 am »
Newby, this is the code:
Code: [Select]
tr/A-Za-z/N-ZA-Mn-za-m/;

Put that in a file and perl -p it. Less than 25 characters.

Offline Newby

  • Moderator
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: Write a rot13 program in one line
« Reply #16 on: August 17, 2006, 09:09:37 am »
I realize what the code is. But you gave that entire line like the line itself was the solution. :P

How about this shell script for the win?

Code: [Select]
tr A-Za-z N-ZA-Mn-za-m
Quote
newby@impaler:~/Downloads/vmware-server$ perl -p -e "tr/A-Za-z/N-ZA-Mn-za-m/;"
Testing!
Grfgvat!

newby@impaler:~/Downloads/vmware-server$ tr A-Za-z N-ZA-Mn-za-m
Testing!
Grfgvat!

Mine requires less resources I wager. :)
- Newby
http://www.x86labs.org

Quote
[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT. 

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Write a rot13 program in one line
« Reply #17 on: August 17, 2006, 09:19:17 am »
haha that's neat :D I didn't know there was a tr command.

I don't really care about the resources but it is less to type ^.^