diff options
Diffstat (limited to 'challenge-288')
| -rwxr-xr-x | challenge-288/ulrich-rieke/cpp/ch-1.cpp | 37 | ||||
| -rwxr-xr-x | challenge-288/ulrich-rieke/haskell/ch-1.hs | 25 | ||||
| -rwxr-xr-x | challenge-288/ulrich-rieke/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-288/ulrich-rieke/raku/ch-1.raku | 34 | ||||
| -rwxr-xr-x | challenge-288/ulrich-rieke/rust/ch-1.rs | 44 |
5 files changed, 182 insertions, 0 deletions
diff --git a/challenge-288/ulrich-rieke/cpp/ch-1.cpp b/challenge-288/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..4551c7edca --- /dev/null +++ b/challenge-288/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,37 @@ +#include <iostream>
+#include <string>
+#include <algorithm>
+#include <cstdlib>
+
+bool isPalindrome( int number ) {
+ auto numberstring = std::to_string( number ) ;
+ auto reversed { numberstring } ;
+ std::reverse( reversed.begin( ) , reversed.end( ) ) ;
+ return reversed == numberstring ;
+}
+
+int main( ) {
+ std::cout << "Please enter a number!\n" ;
+ int number ;
+ std::cin >> number ;
+ int current = number - 1 ;
+ while ( ! isPalindrome( current ) )
+ current-- ;
+ auto minusnum = current ;
+ auto minusdiff = std::abs( minusnum - number ) ;
+ current = number + 1 ;
+ while ( ! isPalindrome( current ) )
+ current++ ;
+ auto plusnum = current ;
+ auto plusdiff = std::abs( plusnum - number ) ;
+ if ( plusdiff == minusdiff )
+ std::cout << minusnum << '\n' ;
+ else {
+ if ( plusdiff < minusdiff )
+ std::cout << plusnum << '\n' ;
+ else
+ std::cout << minusnum << '\n' ;
+ }
+ return 0 ;
+}
+
diff --git a/challenge-288/ulrich-rieke/haskell/ch-1.hs b/challenge-288/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..71319faf31 --- /dev/null +++ b/challenge-288/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,25 @@ +module Challenge288
+ where
+
+isPalindrome :: Int -> Bool
+isPalindrome n = s == reverse s
+ where
+ s = show n
+
+solution :: String -> Int
+solution s =
+ let number = read s
+ lowerPali = until isPalindrome pred (number - 1 )
+ upperPali = until isPalindrome succ (number + 1)
+ minDiff = abs( lowerPali - number )
+ plusDiff = abs( upperPali - number )
+ in if minDiff == plusDiff then lowerPali
+ else
+ if minDiff < plusDiff then lowerPali else upperPali
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a number!"
+ number <- getLine
+ print $ solution number
+
diff --git a/challenge-288/ulrich-rieke/perl/ch-1.pl b/challenge-288/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..e4b7cd3d27 --- /dev/null +++ b/challenge-288/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub isPalindrome {
+ my $number = shift ;
+ if ( $number eq join( '' , reverse( split( // , $number )))) {
+ return 1 ;
+ }
+ else {
+ return 0 ;
+ }
+}
+
+say "Enter a number!" ;
+my $number = <STDIN> ;
+chomp $number ;
+my $current = $number ;
+$current-- ;
+while ( not ( isPalindrome( $current ) ) ) {
+ $current-- ;
+}
+my $minusnum = $current ;
+my $minusdiff = abs( $minusnum - $number ) ;
+$current = $number + 1 ;
+while ( not ( isPalindrome( $current ) ) ) {
+ $current++ ;
+}
+my $plusnum = $current ;
+my $plusdiff = abs( $plusnum - $number ) ;
+if ( $plusdiff == $minusdiff ) {
+ say $minusnum ;
+}
+else {
+ if ( $plusdiff < $minusdiff ) {
+ say $plusnum ;
+ }
+ else {
+ say $minusnum ;
+ }
+}
diff --git a/challenge-288/ulrich-rieke/raku/ch-1.raku b/challenge-288/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..0ccd1a9070 --- /dev/null +++ b/challenge-288/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,34 @@ +use v6 ;
+
+sub isPalindrome( Int $number is copy ) {
+ my $numberstring = ~$number ;
+ return $numberstring eq $numberstring.comb.reverse.join ;
+}
+
+say "Enter a number!" ;
+my $num = $*IN.get ;
+my Int $number = $num.Int ;
+my Int $current = $number ;
+repeat {
+ $current--
+} until ( isPalindrome( $current ) ) ;
+
+my $minusnum = ~$current ;
+my $minusdiff = ( $number - $current ).abs ;
+$current = $number ;
+repeat {
+ $current++ ;
+} until ( isPalindrome( $current ) ) ;
+my $plusnum = ~$current ;
+my $plusdiff = ( $number - $current ).abs ;
+if ( $plusdiff == $minusdiff ) {
+ say $minusnum ;
+}
+else {
+ if ( $plusdiff < $minusdiff ) {
+ say $plusnum ;
+ }
+ else {
+ say $minusnum ;
+ }
+}
diff --git a/challenge-288/ulrich-rieke/rust/ch-1.rs b/challenge-288/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..d9461ff59a --- /dev/null +++ b/challenge-288/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,44 @@ +use std::io ; + +fn is_palindrome( number : i32 ) -> bool { + let target : String = number.to_string( ) ; + let mut reversed : String = String::new( ) ; + let characters : &str = target.as_str( ) ; + let mut iter = characters.chars( ).rev( ) ; + while let Some( c ) = iter.next( ) { + reversed.push( c ) ; + } + target == reversed +} + +fn main() { + println!("Enter a number!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = inline.as_str( ).trim( ) ; + let number : i32 = entered_line.parse::<i32>( ).unwrap( ) ; + let mut current : i32 = number ; + current -= 1 ; + while ! is_palindrome( current ) { + current -= 1 ; + } + let minus_diff : i32 = (current - number).abs( ) ; + let minusnum = current ; + current = number + 1 ; + while ! is_palindrome( current ) { + current += 1 ; + } + let plus_diff : i32 = (current - number).abs( ) ; + let plusnum = current ; + if minus_diff == plus_diff { + println!("{:?}" , minusnum.to_string( ) ) ; + } + else { + if minus_diff < plus_diff { + println!("{:?}" , minusnum.to_string( ) ) ; + } + else { + println!("{:?}" , plusnum.to_string( ) ) ; + } + } +} |
