Re: Palindrome Program without Reverse function
by Ratazong (Monsignor) on Oct 30, 2011 at 19:09 UTC
|
my $back = reverse $forw;
if ($back eq $forw) { # it is a palindrome
In order to avoid the reverse function you could try to code it yourself: loop through the string $forw character by character, and construct $back by appending the characters at the correct place.
Hope that idea helps you! Rata | [reply] [d/l] [select] |
Re: Palindrome Program without Reverse function
by moritz (Cardinal) on Oct 30, 2011 at 20:00 UTC
|
First of all you should only read one string from <>.
If you were given a long word on paper, and were to check if it is a palindrome, how would you do it? Just write a program that takes the same approach as you would.
| [reply] [d/l] |
Re: Palindrome Program without Reverse function
by ikegami (Patriarch) on Oct 30, 2011 at 21:54 UTC
|
str[0] eq str[str_length-1] and
str[1] eq str[str_length-2] and
str[2] eq str[str_length-3] and
...
Or, it's not a palindrome if
str[0] ne str[str_length-1] or
str[1] ne str[str_length-2] or
str[2] ne str[str_length-3] or
...
| [reply] [d/l] [select] |
Re: Palindrome Program without Reverse function
by ww (Archbishop) on Oct 31, 2011 at 02:39 UTC
|
| [reply] [d/l] [select] |
Re: Palindrome Program without Reverse function
by Anonymous Monk on Oct 31, 2011 at 04:37 UTC
|
my $palindrome_re = qr{
\b
(?<pal> # A palindrome is a sandwich...
(?<char>\w) # starting with white bread...
(?: # filled with either...
\w? # peanut butter (maybe)...
| # or...
(?&pal) # another sandwich...
)
\k<char> # topped off with white bread!
)
\b
}x;
@ARGV = 'words';
/$palindrome_re/ and print while <>;
| [reply] [d/l] |
|
|
sub isPalindrome{
substr( $_[0], $_, 1 ) eq substr( $_[0], -($_+1), 1 )
or return for 0 .. length( $_[0] ) >> 1;
1;
}
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
#!/usr/bin/perl
use Modern::Perl;
# 934820
my $palindrome_re = qr{
\b
(?<pal> # A palindrome is a sandwich...
(?<char>\w) # starting with white bread...
(?: # filled with either...
\w? # peanut butter (maybe)...
| # or...
(?&pal) # another sandwich...
)
\k<char> # topped off with white bread!
)
\b
}ix;
my @Rev_arr = qw /Research tattarrattat Kanakanak Qaanaaq Foobarblivit
+z/;
for my $word(@Rev_arr) {
if ($word =~ /$palindrome_re/) {
say $word;
} else {
say "$word is NOT a palindrome";
}
}
Execution:
C:\>934820.pl
Research is NOT a palindrome
tattarrattat
Kanakanak
Qaanaaq
Foobarblivitz is NOT a palindrome
C:\>
| [reply] [d/l] [select] |
Re: Palindrome Program without Reverse function
by JavaFan (Canon) on Oct 31, 2011 at 11:03 UTC
|
A word is a palindrome if it's either the empty string, a string of one character, or if it's a palindrome proceeded and succeeded by the same character. So, I present you the following, untested, subroutine:
my is_palindrome {length($_[0])<2||substr($_[0],0,1) eq substr($_[0],-
+1,1)&&is_palindrome(substr($_[0],1,-1);}
| [reply] [d/l] |
|
|
Tested, fixed: sub is_palindrome {length($_[0])<2||substr($_[0],0,1) eq substr($_[0],-1,1)&&is_palindrome(substr($_[0],1,-1))}
| [reply] [d/l] |
Re: Palindrome Program without Reverse function
by kranthi_votary (Initiate) on Jul 16, 2015 at 04:41 UTC
|
#!\\C:\\Perl\\bin\\perl -w
my $name=<STDIN>;
chomp($name);
@arr=split(//,$name);
$count=@arr;
$num=0;
#print $count;
for ($i=0;$i<$count;$i++){
if ($arr[$i] eq $arr[$count-1]){
#print "$arr[$i] ===== $arr[$count]\n";
$count--;
}else{
$num++;
last;
}
}
if ($num == 0){
print "Word is polindrome!!!";
}else{
print "Word is not a Polindrome!!!";
}
| [reply] [d/l] |
|
|
You replied to a 4 years old thread. Moreover, you check half the pairs pointlessly - there's no need to check $arr[$#arr] versus $arr[0], when you've already checked $arr[0] versus $arr[$#arr].
Here's how I'd code your idea:
#!/usr/bin/perl
use strict;
use warnings;
chomp(my $name = <>);
my @arr = split //, $name;
my $is_palindrome = 1;
for my $i (0 .. $#arr / 2) {
# warn "$arr[$i] eq $arr[ -$i - 1 ]";
if ($arr[$i] ne $arr[ -$i - 1 ]) {
undef $is_palindrome;
last
}
}
print 'Word is',
$is_palindrome ? q() : ' not',
" a palindrome!!!\n";
| [reply] [d/l] [select] |