aboutsummaryrefslogtreecommitdiff
path: root/challenge-135
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-20 08:23:28 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-20 08:23:28 +0100
commita660745f4efb1a8be426529a1f90d3a5a2010a83 (patch)
tree9b7d0d7c125f351a4ef47b83def4cb982275a2dc /challenge-135
parentb680c7f41f9d1518d5ce373d7301c03e4dc6a574 (diff)
downloadperlweeklychallenge-club-a660745f4efb1a8be426529a1f90d3a5a2010a83.tar.gz
perlweeklychallenge-club-a660745f4efb1a8be426529a1f90d3a5a2010a83.tar.bz2
perlweeklychallenge-club-a660745f4efb1a8be426529a1f90d3a5a2010a83.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-135')
-rw-r--r--challenge-135/ulrich-rieke/cpp/ch-1.cpp28
-rw-r--r--challenge-135/ulrich-rieke/cpp/ch-2.cpp45
-rw-r--r--challenge-135/ulrich-rieke/haskell/ch-1.hs12
-rw-r--r--challenge-135/ulrich-rieke/haskell/ch-2.hs28
-rw-r--r--challenge-135/ulrich-rieke/perl/ch-1.pl24
-rw-r--r--challenge-135/ulrich-rieke/perl/ch-2.pl29
-rw-r--r--challenge-135/ulrich-rieke/raku/ch-1.raku21
-rw-r--r--challenge-135/ulrich-rieke/raku/ch-2.raku32
8 files changed, 219 insertions, 0 deletions
diff --git a/challenge-135/ulrich-rieke/cpp/ch-1.cpp b/challenge-135/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..098d1b7fee
--- /dev/null
+++ b/challenge-135/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,28 @@
+#include <iostream>
+#include <string>
+#include <regex>
+
+int main( int argc, char * argv[] ) {
+ std::string input { argv[ 1 ] } ;
+ std::string checkRegex( "(^[+-]*\\d+$)" ) ;
+ std::regex checkReg( checkRegex ) ;
+ while ( ! std::regex_match( input , checkReg ) ) {
+ std::cout << "Input should consist of possible signs and digits only!\n" ;
+ std::getline( std::cin , input ) ;
+ }
+ int len = input.length( ) ;
+ if ( ( len == 4 ) ) {
+ std::string start { input.substr( 0 , 1 ) } ;
+ if ( (start == "+") || ( start == "-" ) )
+ std::cout << input.substr( 1 ) << std::endl ;
+ }
+ else {
+ if ( len % 2 == 0 )
+ std::cout << "even number of digits" << std::endl ;
+ if ( ( len < 3 ) && ( len % 2 != 0 ))
+ std::cout << "too short" << std::endl ;
+ if ( ( len % 2 != 0 ) && ( len > 3 ) )
+ std::cout << input.substr( ( len - 3 ) / 2 , 3 ) << std::endl ;
+ }
+ return 0 ;
+}
diff --git a/challenge-135/ulrich-rieke/cpp/ch-2.cpp b/challenge-135/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..03be87e9a2
--- /dev/null
+++ b/challenge-135/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,45 @@
+#include <iostream>
+#include <string>
+#include <regex>
+#include <cctype>
+#include <algorithm>
+#include <array>
+
+int main( int argc , char * argv[ ] ) {
+ std::string sedol { argv[ 1 ] } ;
+ bool valid = false ;
+ if ( sedol.length( ) != 7 ) {
+ std::cout << 0 << std::endl ;
+ return 1 ;
+ }
+ std::transform( sedol.begin( ) , sedol.end( ) , sedol.begin( ) ,
+ toupper ) ;
+ std::string checkReg ("(^\\w+$)" ) ;
+ std::regex checkRegex( checkReg ) ;
+ std::string anotherCheck ("([AEIOU])") ;
+ std::regex vowelCheck( anotherCheck ) ;
+ if ( (! ( std::regex_match( sedol , checkRegex ))) ||
+ ( std::regex_match( sedol , vowelCheck ) ) ) {
+ std::cout << 0 << std::endl ;
+ return 2 ;
+ }
+ std::array<int, 7> weights {{ 1, 3 , 1 , 7 , 3 , 9 , 1 }} ;
+ int sum = 0 ;
+ for ( int i = 0 ; i < 6 ; i++ ) {
+ int value = static_cast<int>( sedol[ i ] ) ;
+ if ( isupper( value )) {
+ sum += (value - 55) * weights[ i ] ;
+ }
+ else {
+ int value = std::stoi( sedol.substr( i , 1 ) ) ;
+ sum += value * weights[ i ] ;
+ }
+ }
+ std::string letterStr {sedol.substr( 6 , 1 )} ;
+ if ( std::stoi( letterStr ) == ( (10 - ( sum % 10 )) % 10 )) {
+ std::cout << 1 << std::endl ;
+ }
+ else
+ std::cout << 0 << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-135/ulrich-rieke/haskell/ch-1.hs b/challenge-135/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..13128d1e6a
--- /dev/null
+++ b/challenge-135/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,12 @@
+module Challenge135
+ where
+
+solution :: String -> String
+solution number
+ |(elem (head number) "+-") && len == 4 = drop 1 number
+ |even len = "even number of digits"
+ |(len < 3 ) && (not $ even len) = "too short"
+ |otherwise = take 3 $ drop ( div ( len - 3 ) 2 ) number
+ where
+ len :: Int
+ len = length number
diff --git a/challenge-135/ulrich-rieke/haskell/ch-2.hs b/challenge-135/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..c22cfa2721
--- /dev/null
+++ b/challenge-135/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,28 @@
+module Challenge135_2
+ where
+import Data.Char
+import Data.List ( (!!) )
+
+firstCondition :: String -> Bool
+firstCondition s = length s == 7
+
+secondCondition :: String -> Bool
+secondCondition s = not $ any (\c -> elem c "AEIOU" ) s
+
+thirdCondition :: String -> Bool
+thirdCondition str = (intToDigit ( mod (10 - ( mod theSum 10)) 10 )) == last str
+where
+ weights :: [Int]
+ weights = [1, 3 , 1 , 7 , 3 , 9 , 1]
+ convert :: Char -> Int
+ convert letter = if isUpper letter then ord letter - 55 else digitToInt letter
+ theSum :: Int
+ theSum = sum $ map (\p -> (convert $ fst p) * ( snd p )) $ take 6 $ zip str
+ weights
+
+solution :: String -> Int
+solution sedol = if firstCondition converted && secondCondition converted &&
+thirdCondition converted then 1 else 0
+where
+ converted :: String
+ converted = map toUpper sedol
diff --git a/challenge-135/ulrich-rieke/perl/ch-1.pl b/challenge-135/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..4f7f076c63
--- /dev/null
+++ b/challenge-135/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use feature "switch" ;
+
+my $n = $ARGV[ 0 ] ;
+while ( $n !~ /\A[+-]*\d+\z/ ) {
+ say "Input should consist of possible sign and numbers only!" ;
+ $n = <STDIN> ;
+ chomp $n ;
+}
+my $len = length( $n ) ;
+if ( $len == 4 && substr( $n , 0 ,1 ) =~ /\+|\-/ ) {
+ say substr( $n , 1 ) ;
+}
+else {
+ given ($len) {
+ when ( ($_ % 2 == 0)) { say "even number of digits" }
+ when ( ($_ < 3 ) and not ( $_ % 2 == 0 ) ) { say "too short" }
+ when ( ( not ($_ % 2 == 0 ) ) && ( $_ > 3 )) { say
+ substr( $n , ( $len - 3 )/ 2 , 3 ) }
+ }
+}
diff --git a/challenge-135/ulrich-rieke/perl/ch-2.pl b/challenge-135/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..8a9830ef2b
--- /dev/null
+++ b/challenge-135/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+my $sedol = $ARGV[0] ;
+my $teststring = uc( $sedol ) ;
+my $valid = 0 ;
+if ( ( $teststring !~ /\A\w{7}\z/ ) || ( $teststring =~ /[AEIOU]/ )
+ || ( $teststring =~ /_/ )) {
+ $valid = 0 ;
+}
+else {
+ my @factors = ( 1, 3 , 1 , 7 , 3 , 9 , 1 ) ;
+ my $sum = 0 ;
+ for my $i ( 0 .. 5 ) {
+ my $letter = substr( $teststring , $i , 1 ) ;
+ if ( $letter =~ /\p{N}/ ) {
+ $sum += $letter * $factors[ $i ] ;
+ }
+ if ( $letter =~ /\p{Lu}/ ) {
+ $sum += ( ord( $letter ) - 55 ) * $factors[ $i ] ;
+ }
+ }
+ if ( (substr( $teststring, 6 , 1 ) + 0 ) == ( (10 - ( $sum % 10 )) % 10 )) {
+ $valid = 1 ;
+ }
+}
+say $valid ;
diff --git a/challenge-135/ulrich-rieke/raku/ch-1.raku b/challenge-135/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..3ce786fb28
--- /dev/null
+++ b/challenge-135/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,21 @@
+use v6 ;
+
+sub MAIN( Str $input is copy ) {
+ say $input ;
+ while ( $input !~~ /^<[+-]>*\d+$/ ) {
+ say "Please enter digits only!" ;
+ $input = $*IN.get ;
+ }
+ my $len = $input.chars ;
+ if ( $len == 4 && $input.substr( 0 , 1 ) ~~ /<[+-]>/ ) {
+ say $input.substr( 1 ) ;
+ }
+ else {
+ given $len {
+ when $_ %% 2 { say "even number of digits" }
+ when ($_ < 3 and not $_ %% 2 ) { say "too short" }
+ when (not $_ %% 2 and $_ > 3 ) { say $input.substr( ( $len - 3 )
+ div 2 , 3 ) }
+ }
+ }
+}
diff --git a/challenge-135/ulrich-rieke/raku/ch-2.raku b/challenge-135/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..ea29e55c6b
--- /dev/null
+++ b/challenge-135/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,32 @@
+use v6 ;
+
+sub MAIN( Str $sedol is copy ) {
+ my Bool $valid = False ;
+ my Str $teststring = $sedol.uc ;
+ if ( $teststring !~~ /^\w ** 7$/ || $teststring ~~ /<[AEIOU]>/
+ || $teststring ~~ /'_'/ ) {
+ $valid = False ;
+ }
+ else {
+ my @factors = ( 1, 3 , 1 , 7 , 3 , 9 , 1 ) ;
+ my Int $sum = 0 ;
+ for (0 .. 5 ) -> $i {
+ my $letter = $teststring.substr( $i , 1 ) ;
+ if ( $letter ~~ /<digit>/ ) {
+ $sum += +$letter * @factors[ $i ] ;
+ }
+ if ( $letter ~~ /<upper>/ ) {
+ $sum += (ord( $letter ) - 55) * @factors[ $i ] ;
+ }
+ }
+ if ( $teststring.substr( 6 , 1 ).Int == ( 10 - ( $sum % 10 ) ) % 10 ) {
+ $valid = True ;
+ }
+ }
+ if ( $valid ) {
+ say 1 ;
+ }
+ else {
+ say 0 ;
+ }
+}