General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!
NOTE: All new threads will be moderated. Political threads will not be approved. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
09-12-2009, 05:32 AM
|
#31
|
LQ 5k Club
Registered: Jan 2006
Location: Oldham, Lancs, England
Distribution: Slackware64 15; SlackwareARM-current (aarch64); Debian 12
Posts: 8,311
Rep: 
|
I thought it was an error message in Klingon:
You have moved the mouse. Windows will now shut down.
|
|
|
09-12-2009, 06:45 AM
|
#32
|
Moderator
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
|
Ah, I have so few words of wisdom that it's nice to have them noticed 
|
|
|
09-12-2009, 08:30 AM
|
#33
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,127
Rep: 
|
Jiml8 good point on the don't subtract from space. Made a few more compact versions just for you
Code:
#!/usr/bin/perl
$var = "Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/";
@lets = ($var =~ m/(.)/g);
for ($i = 0; $i <= $#lets; $i += 2) {
next if ($lets[$i] eq " ");
$lets[$i] = pack('c', unpack('c', $lets[$i])-1);
}
$ans = join('', @lets);
print "$ans\n";
and
Code:
#!/usr/bin/perl
$var = "Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/";
for ($i = 0; $i < length($var); $i += 2) {
next if (substr($var,$i,1) eq " ");
substr($var, $i, 1, pack('c', unpack('c', substr($var, $i, 1))-1));
}
print "$var\n";
and finally (though this one suffers from the last character not being translated, and won't work in all perls)
Code:
#!/usr/bin/perl
$var = "Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/";
$var =~ s/(.)(?{ $n = pack('c', unpack('c', $^N)-1); $n = " " if ($n le " ") })(.)/$n$2/g;
print "$var\n";
edit - added this one (just cause that pack/unpack looks ugly)
Code:
#!/usr/bin/perl
$var = "Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/";
$var =~ s/(.)(?{ $n = chr(ord($^N)-1); $n = " " if ($n le " ") })(.)/$n$2/g;
print "$var\n";
Last edited by estabroo; 09-12-2009 at 11:08 PM.
Reason: for the horde
|
|
|
09-12-2009, 05:32 PM
|
#34
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
Well, alright. I can compress mine too. Readability doesn't suffer too much. I do have to declare variables though; compiler won't do it for me on the fly.
Code:
#include <stdio.h>
#include <string.h>
void main()
{
char data[] = "Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/",*linestart = data,*lineptr;
for(lineptr = linestart;lineptr <= linestart + strlen(data);lineptr+=2){if(*lineptr != 0x20) --*lineptr;}
printf("%s\n",data);
}
Since C doesn't have sophisticated string handling, I don't think I can squeeze much more out of this. Want to run some timing exercises and see which is faster?
Last edited by jiml8; 09-12-2009 at 05:51 PM.
|
|
|
09-12-2009, 05:41 PM
|
#35
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,127
Rep: 
|
lol, now we just need an erlanger to one-liner it for us 
|
|
|
09-12-2009, 05:59 PM
|
#36
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
Wow. You posted fast. I updated my code after you posted, without being aware you HAD posted. Shortened it a bit more.
|
|
|
09-12-2009, 06:28 PM
|
#37
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,127
Rep: 
|
hehe, I'd bet the C is faster. All of this reminded me of a great programming challenge site, its solving math problems (some of them are even crypto), the basic idea is to write a program in any language that'll solve the problem with a bonus challenge of making it run in under a minute.
http://projecteuler.net/
|
|
|
09-12-2009, 11:02 PM
|
#38
|
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360
Rep: 
|
Using bash and tr
Code:
text='Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/'
charset=$(echo -e "$(printf '\\x%x' {33..126})")
for ((n = 0; n < ${#text}; n += 2));do
echo -n "${text:n:1}" | tr "$charset" " $charset"
echo -n "${text:n+1:1}"
done
Congratulations =) You passed our idiot filter =) What are your three favorite technical books? Please include at least one software engineering book, the others can be whatever, i.e., physics, math, etc.
Last edited by Kenhelm; 09-12-2009 at 11:23 PM.
|
|
|
09-12-2009, 11:18 PM
|
#39
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,127
Rep: 
|
Got the regex one fixed now, shorter and handles that pesky last character.
Code:
#!/usr/bin/perl
$var = "Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/";
$var =~ s/([^ ])(?{ $n = chr(ord($^N)-1); })(.?)|( .)(?{ $n = $^N })/$n$2/g;
print "$var\n";
|
|
|
09-12-2009, 11:24 PM
|
#40
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,127
Rep: 
|
Wow Kenhelm, very nice, I wouldn't have given bash enough credit to be able to do it.
|
|
|
09-13-2009, 11:59 AM
|
#41
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Quote:
Originally Posted by Kenhelm
Using bash and tr
Code:
text='Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/'
charset=$(echo -e "$(printf '\\x%x' {33..126})")
for ((n = 0; n < ${#text}; n += 2));do
echo -n "${text:n:1}" | tr "$charset" " $charset"
echo -n "${text:n+1:1}"
done
Congratulations =) You passed our idiot filter =) What are your three favorite technical books? Please include at least one software engineering book, the others can be whatever, i.e., physics, math, etc.
|
Dang  you beat me to it! I was going to give it a shot in bash (and likely still will, so I'm not peeking at how you did it) but nice job! Bash is the language I'm currently most able with -- I'm not much for C, perl, python, etc..
I have a couple ideas to try out.. This kind of stuff keeps the brain fresh & juicy
I think it'd be cool to see this in Lisp too, if that can be done. Any takers?
Sasha
Last edited by GrapefruiTgirl; 09-13-2009 at 12:00 PM.
|
|
|
09-13-2009, 01:14 PM
|
#42
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
And, in PHP:
Code:
#!/usr/bin/php
<?php
$data="Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/";
for($ii=0;$ii<strlen($data);$ii+=2){if($data[$ii]<>" ")$data[$ii] = chr(ord($data[$ii]) - 1);}
echo $data."\n";
?>
You do need to have the command line php interpreter installed for this one. I could also make this one obscure by using regex's. Maybe I will.
edit: Here is the regexp version in PHP.
Code:
#!/usr/bin/php
<?php
$data="Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/";
echo preg_replace("((.)(.)?)e" ,"(\"\\1\" != \" \")?chr(ord('\\1') - 1).'\\2':'\\1\\2'", $data)."\n";
?>
And this regexp does handle that pesky last character.
Last edited by jiml8; 09-13-2009 at 04:13 PM.
|
|
|
09-13-2009, 11:31 PM
|
#43
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
bash + awk solution
Ok! While I'm semi-impressed with what I have come up with (because it's the first time I've ever used awk for anything), it has a bug.
Here's the code:
Code:
#!/bin/bash
data="Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/"
i=$((0)); leng=$(($(echo "$data" | awk '{ if (length($0) > len) len = length($0) } END { print len }')))
while ((i <= $leng)); do
output="$output$(echo $(($(printf %d "'$(echo ${data:$((i)):1} | gawk '{ printf("%c",int($0)); } {print}')")-1)) | gawk '{ printf("%c",$0); }')${data:$((i+1)):1}"
i=$((i+2))
done
echo "$output"
..and here's the output (note the two ÿ characters which I have no clue why they are appearing -- but I'm working on it.)
Code:
bash-3.1$ sh decode
Congratulations =) You passed our idiot filter =)ÿWhat are your three favorite technical books?ÿPlease include at least one software engineering book, the others can be whatever, i.e., physics, math, etc.
bash-3.1$
Last edited by GrapefruiTgirl; 09-14-2009 at 08:10 AM.
Reason: executed length computation only once -- speeds up execution. Changed some awks to gawks
|
|
|
09-14-2009, 01:51 AM
|
#44
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
Anyone want to do it in Fortran? I might try; I used to program a LOT of Fortran, but the last time I did was in about 1990 and I have never programmed for the gnu fortran compiler. Might be awhile before it is running.
We also need someone to step up and do it in python. And there was a request for lisp.
While we're at it, is Modula still in use? Pascal? Forth?
|
|
|
09-14-2009, 08:47 AM
|
#45
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,127
Rep: 
|
Here is a python one (far from optimal)
Code:
#!/usr/bin/python
import re
def decode(m):
a = m.group(0)
if (len(a) == 2):
a = m.group(1)
b = m.group(2)
else:
b = ""
if (a != " "):
a = chr(ord(a)-1)
return a + b
text = "Doogsauumauipnt >)! Zov qatsfd!ovr!ieipt!fjlues >) Wiau brf zovr!tirfe!fbvprjtf uedhoidam copkt? Pmebsf jndlvdf bt!lfatt!ooe!spfuwbrf fnhioefrjnh copk- uhf ptiess!cbn!bf xhbtfvfr- j.f.- qhzsjct,!mbti,!euc/";
var = re.sub("(.)(.)|(.)", decode, text)
print var
GrapefruiTGirl - the problem your seeing is the subtract from space issue
|
|
|
All times are GMT -5. The time now is 06:56 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|