Perl Golf!
For this part, you are to write two programs and the goal is to write it as tersely as possible. White space counts, so having it all on one line is good (I will use wc to count).
The goal of these questions is to solve the problem in as few characters as possible. When you finish, put them in the pc subdirectory of the csci11 cgi directory, naming them with your userid, followed by 1 or 2 (depending on question, i.e. stacia1.pl or stacia2.pl).
-> cp stacia1.pl /usr/local/www/cgi-bin/csci11/pc/
- Write a program to take a file from STDIN and encrypt and print out the file. The encryption rotates all the characters of the alphabet 13 charactars to the right (i.e. a is n, z is m).
Results!
The one with the shortest characters used things we didn't cover in class, so
we'll refrain from declaring winners for the golf part.
We'll just look at the results:
- Chris, 37 chars:
map{y/a-zA-Z/n-za-mN-ZA-M/;print}<>;
- Stacia, 41 chars:
while(<>){tr/a-zA-Z/n-za-mN-ZA-M/;print}
- Paul, 47 chars:
while(<>){tr/a-mA-Mn-zN-Z/n-zN-Za-mA-M/;print}
- Arjun, 59 chars:
while(<>){chop;foreach$v(@s=unpack("C*",$_)){print $v+13}}
- Zach, 94 chars (and wins the obfuscated prize!):
while(<>){while(/./g){$i=$&;for($a=0;$a<13;$a++){if($i=~/\w/){$i=$&;$i++;}}$s.=$i;}}print $s;
- Write a program to detect if an input string of alphanumeric characters and spaces of even length is a palindrome. The input can be of any length. It should output "yes" if it is a palindrome and "no" if it is not.
Bonus: Make it work for any input string.
Results!
- Chris, 54 chars:
$_=<>;chop;s/\s//g;print(($_ eq reverse)?"Yes":"No");
- Paul, 70 chars (but includes a return and loops on input):
while(<>){s/\W//g;("\L$_"eq reverse"\L$_")?print"yes\n":print"no\n";}
- Neal, 87 chars (but doesn't strip white space):
@A=split(//,);pop@A;while(@A){if(pop@A ne shift@A){print"no";exit;}}print
"yes";
- Zach, 88 chars (strips white space and works on all palindromes):
$_=<>;while(/^\W*(\w)(.*)\1\W*$/i){$_=$2;}if(/\w.*\w/){print"no";}else{print"yes";}
- Stacia, I can't find my original, but it was long, over 100 chars if I remeber correctly.