aboutsummaryrefslogtreecommitdiff
path: root/challenge-114
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-05-25 21:18:39 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-05-25 21:18:39 +0100
commit5ea37ecb6cdebf9514cb102455e31beeebcafc69 (patch)
tree2dcd4b20f2e862fbff99363e11652ad294313efb /challenge-114
parent78531dc06c2e9463612af886c7920905868c42cd (diff)
downloadperlweeklychallenge-club-5ea37ecb6cdebf9514cb102455e31beeebcafc69.tar.gz
perlweeklychallenge-club-5ea37ecb6cdebf9514cb102455e31beeebcafc69.tar.bz2
perlweeklychallenge-club-5ea37ecb6cdebf9514cb102455e31beeebcafc69.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-114')
-rw-r--r--challenge-114/ulrich-rieke/cpp/ch-1.cpp20
-rw-r--r--challenge-114/ulrich-rieke/cpp/ch-2.cpp28
-rw-r--r--challenge-114/ulrich-rieke/haskell/ch-1.hs8
-rw-r--r--challenge-114/ulrich-rieke/haskell/ch-2.hs29
-rw-r--r--challenge-114/ulrich-rieke/lisp/ch-1.lisp14
-rw-r--r--challenge-114/ulrich-rieke/lisp/ch-2.lisp11
-rw-r--r--challenge-114/ulrich-rieke/perl/ch-1.pl15
-rw-r--r--challenge-114/ulrich-rieke/perl/ch-2.pl33
-rw-r--r--challenge-114/ulrich-rieke/raku/ch-1.raku12
-rw-r--r--challenge-114/ulrich-rieke/raku/ch-2.raku20
10 files changed, 190 insertions, 0 deletions
diff --git a/challenge-114/ulrich-rieke/cpp/ch-1.cpp b/challenge-114/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..d89203bd3d
--- /dev/null
+++ b/challenge-114/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,20 @@
+#include <string>
+#include <iostream>
+#include <algorithm>
+#include <cstdlib>
+
+bool isPalindromeNumber( int n ) {
+ std::string numberstring( std::to_string( n ) ) ;
+ std::string comparison( numberstring ) ;
+ std::reverse( comparison.begin( ) , comparison.end( ) ) ;
+ return numberstring == comparison ;
+}
+
+int main( int argc , char * argv[] ) {
+ int n = std::atoi( argv[ 1 ] ) ;
+ n++ ;
+ while ( ! isPalindromeNumber( n ) )
+ n++ ;
+ std::cout << n << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-114/ulrich-rieke/cpp/ch-2.cpp b/challenge-114/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..6f0b4fcb4d
--- /dev/null
+++ b/challenge-114/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,28 @@
+#include <string>
+#include <iostream>
+#include <cstdlib>
+#include <algorithm>
+
+std::string toBinaryString( int n ) {
+ std::string binaryString ;
+ while ( n != 0 ) {
+ int i = n % 2 ;
+ binaryString.append( std::to_string( i )) ;
+ n /= 2 ;
+ }
+ reverse( binaryString.begin( ) , binaryString.end( ) ) ;
+ return binaryString ;
+}
+
+int main( int argc , char * argv[] ) {
+ int n = std::atoi( argv[ 1 ] ) ;
+ std::string numberstring( toBinaryString( n ) ) ;
+ int ones = std::count( numberstring.begin( ) , numberstring.end( ) , '1' ) ;
+ numberstring = toBinaryString( ++n ) ;
+ while ( std::count( numberstring.begin( ) , numberstring.end( ) , '1' ) != ones ) {
+ n++ ;
+ numberstring = toBinaryString( n ) ;
+ }
+ std::cout << n << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-114/ulrich-rieke/haskell/ch-1.hs b/challenge-114/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..3f8319ffc9
--- /dev/null
+++ b/challenge-114/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,8 @@
+module Challenge114
+ where
+
+solution :: Int -> Int
+solution n = head $ dropWhile myCondition [n + 1 , n + 2 ..]
+where
+ myCondition :: Int -> Bool
+ myCondition num = show num /= ( reverse $ show num )
diff --git a/challenge-114/ulrich-rieke/haskell/ch-2.hs b/challenge-114/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..3da1d6f902
--- /dev/null
+++ b/challenge-114/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,29 @@
+module Challenge114_2
+ where
+import Control.Monad.State.Lazy
+
+--using a state monad that computes a list of integers to convert to a
+--given base
+
+toBase :: State ([Integer] , Integer , Integer ) [Integer]
+toBase = do
+ ( basenumbers , num , base ) <- get
+ if num == 0
+ then
+ return basenumbers
+ else do
+ put ( mod num base : basenumbers , div num base , base )
+ toBase
+
+baseNumbers :: Integer -> Integer -> [Integer]
+baseNumbers n b = evalState toBase ( [] , n , b )
+
+count :: Eq a => a -> [a] -> Int
+count c list = length $ filter ( == c ) list
+
+solution :: Integer -> Integer
+solution n = head $ dropWhile (\i -> (count 1 $ baseNumbers i 2) /= ones )
+[n + 1 , n + 2 ..]
+where
+ ones :: Int
+ ones = count 1 $ baseNumbers n 2
diff --git a/challenge-114/ulrich-rieke/lisp/ch-1.lisp b/challenge-114/ulrich-rieke/lisp/ch-1.lisp
new file mode 100644
index 0000000000..6df25cbc4b
--- /dev/null
+++ b/challenge-114/ulrich-rieke/lisp/ch-1.lisp
@@ -0,0 +1,14 @@
+#!/usr/bin/sbcl --script
+
+(defun isPalindrome( n )
+(let (( num ( format nil "~d" n ))
+ ( reversed ))
+ (setq reversed (reverse num))
+ (string= num reversed )))
+
+(defun solution ( n )
+(do
+ (( p (+ 1 n ) (+ 1 p )))
+ ((isPalindrome p) p )))
+
+(format t "~d~%" (solution 1234))
diff --git a/challenge-114/ulrich-rieke/lisp/ch-2.lisp b/challenge-114/ulrich-rieke/lisp/ch-2.lisp
new file mode 100644
index 0000000000..9432b69912
--- /dev/null
+++ b/challenge-114/ulrich-rieke/lisp/ch-2.lisp
@@ -0,0 +1,11 @@
+#!/usr/bin/sbcl --script
+
+(defun solution( n )
+(do
+ (( p (+ 1 n) (+ 1 p ))
+ (ones ( count #\1 (format nil "~b" n ))))
+ ((= ones (count #\1 (format nil "~b" p ))) p )))
+
+;examples from the task
+(format t "~d~%" ( solution 3 ))
+(format t "~d~%" ( solution 12))
diff --git a/challenge-114/ulrich-rieke/perl/ch-1.pl b/challenge-114/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..bbae52cc17
--- /dev/null
+++ b/challenge-114/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub isPalindrome {
+ my $number = shift ;
+ return $number eq join( '' , reverse split( // , $number ) ) ;
+}
+
+my $N = $ARGV[ 0 ] ;
+do {
+ ++$N ;
+} until ( isPalindrome( $N ) ) ;
+say $N ;
diff --git a/challenge-114/ulrich-rieke/perl/ch-2.pl b/challenge-114/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..ae03c1b831
--- /dev/null
+++ b/challenge-114/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub toBinaryString {
+ my $number = shift ;
+ my @digits ;
+ while ( $number != 0 ) {
+ unshift ( @digits , $number % 2 ) ;
+ $number = int( $number / 2 ) ;
+ }
+ return join( '' , @digits ) ;
+}
+
+sub countOnes {
+ my $number = shift ;
+ my $len = length( $number ) ;
+ my $sum = 0 ;
+ for my $i ( 0 .. $len - 1 ) {
+ if ( substr( $number , $i , 1 ) eq "1" ) {
+ $sum++ ;
+ }
+ }
+ return $sum ;
+}
+
+my $N = $ARGV[ 0 ] ;
+my $ones = countOnes( toBinaryString( $N ) ) ;
+do {
+ ++$N ;
+} until ( countOnes( toBinaryString( $N ) ) == $ones ) ;
+say $N ;
diff --git a/challenge-114/ulrich-rieke/raku/ch-1.raku b/challenge-114/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..be8e181ae6
--- /dev/null
+++ b/challenge-114/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,12 @@
+use v6 ;
+
+sub isPalindrome( Int $number ) {
+ return ~$number eq ~$number.flip ;
+}
+
+sub MAIN( Int $N is copy ) {
+ repeat {
+ ++$N ;
+ } until ( isPalindrome( $N ) ) ;
+ say $N ;
+}
diff --git a/challenge-114/ulrich-rieke/raku/ch-2.raku b/challenge-114/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..9b0c7f8237
--- /dev/null
+++ b/challenge-114/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,20 @@
+use v6 ;
+
+sub countOnes( Str $numberstr ) {
+ my $sum = 0 ;
+ my $len = $numberstr.chars ;
+ for (0 .. $len - 1 ) -> $i {
+ if ( $numberstr.substr( $i , 1 ) eq "1" ) {
+ $sum++ ;
+ }
+ }
+ return $sum ;
+}
+
+sub MAIN( Int $N is copy ) {
+ my $ones = countOnes( $N.base( 2 ) ) ;
+ repeat {
+ ++$N ;
+ } until ( countOnes( $N.base( 2 )) == $ones) ;
+ say $N ;
+}