aboutsummaryrefslogtreecommitdiff
path: root/challenge-190
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-11-09 20:22:05 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-11-09 20:22:05 +0000
commit2b107c0a1007dc904822ac1a2c19d76d697fc559 (patch)
treed2b5d1fb9f5053732d55451a2285bcf08e7fe2fb /challenge-190
parent142bc3da93a0bc254e064d2c994147c760524b8e (diff)
downloadperlweeklychallenge-club-2b107c0a1007dc904822ac1a2c19d76d697fc559.tar.gz
perlweeklychallenge-club-2b107c0a1007dc904822ac1a2c19d76d697fc559.tar.bz2
perlweeklychallenge-club-2b107c0a1007dc904822ac1a2c19d76d697fc559.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-190')
-rw-r--r--challenge-190/ulrich-rieke/cpp/ch-1.cpp22
-rw-r--r--challenge-190/ulrich-rieke/haskell/ch-1.hs7
-rw-r--r--challenge-190/ulrich-rieke/haskell/ch-2.hs37
-rw-r--r--challenge-190/ulrich-rieke/java/Challenge190.java19
-rw-r--r--challenge-190/ulrich-rieke/perl/ch-1.pl19
-rw-r--r--challenge-190/ulrich-rieke/perl/ch-2.pl68
-rw-r--r--challenge-190/ulrich-rieke/raku/ch-1.raku14
-rw-r--r--challenge-190/ulrich-rieke/rust/ch-1.rs19
8 files changed, 205 insertions, 0 deletions
diff --git a/challenge-190/ulrich-rieke/cpp/ch-1.cpp b/challenge-190/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..c013223bfe
--- /dev/null
+++ b/challenge-190/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,22 @@
+#include <iostream>
+#include <string>
+#include <cctype>
+#include <algorithm>
+
+int main( ) {
+ std::cout << "Please enter a word!\n" ;
+ std::string line ;
+ std::cin >> line ;
+ if ( (std::isupper( static_cast<int>( line[0] )) &&
+ std::all_of( line.begin( ) + 1 , line.end( ) , []( char c )
+ {return std::islower( static_cast<int>( c )) ; } )) ||
+ std::all_of( line.begin( ) , line.end( ) , []( char c )
+ { return std::isupper( static_cast<int>( c )) ; }) ||
+ std::all_of( line.begin( ) , line.end( ) , []( char c )
+ { return std::islower( static_cast<int>( c )) ; }) )
+ std::cout << 1 ;
+ else
+ std::cout << 0 ;
+ std::cout << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-190/ulrich-rieke/haskell/ch-1.hs b/challenge-190/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..c058635a08
--- /dev/null
+++ b/challenge-190/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,7 @@
+module Challenge190
+ where
+import Data.Char ( isUpper , isLower )
+
+solution :: String -> Int
+solution input = if (all isUpper input || all isLower input || ( isUpper
+( head input ) && all isLower (tail input) )) then 1 else 0
diff --git a/challenge-190/ulrich-rieke/haskell/ch-2.hs b/challenge-190/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..419a3f3f11
--- /dev/null
+++ b/challenge-190/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,37 @@
+module Challenge190_2
+ where
+import qualified Data.Set as S
+import Data.List ( permutations , sort )
+import Data.List.Split ( divvy )
+import Data.Char ( chr )
+
+--find all sublists of 1's and 2's that add up to a given integer
+findSublists :: Int -> [[Int]]
+findSublists l = [replicate t 2 ++ replicate ( l - 2 * t ) 1 |
+t <- [ 0 .. div l 2 ]]
+
+findAllPermutations :: [[Int]] -> [[Int]]
+findAllPermutations list = (S.toList $ S.fromList $ concat $
+map permutations $ filter (\li -> elem 2 li ) list) ++
+[head list]
+
+findSubstring :: String -> Int -> Int -> String
+findSubstring str from to = take ( to - from ) $ drop from str
+
+findNumberlist :: String -> [Int] -> [Int]
+findNumberlist s thePartitions = map read $ map (\li -> findSubstring s
+( head li ) ( last li )) $ divvy 2 1 $ scanl ( + ) 0 thePartitions
+
+findWord :: [Int] -> String
+findWord list = if any ( > 26 ) list then "" else map ( chr . (+ 64 )) list
+
+solution :: String -> [String]
+solution number = sort $ filter ( not . null ) $ map findWord $
+map (\li -> findNumberlist number li )
+$ findAllPermutations $ findSublists ( length number )
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a string consisting only of digits!"
+ numberstring <- getLine
+ print $ solution numberstring
diff --git a/challenge-190/ulrich-rieke/java/Challenge190.java b/challenge-190/ulrich-rieke/java/Challenge190.java
new file mode 100644
index 0000000000..7a3eaaadcd
--- /dev/null
+++ b/challenge-190/ulrich-rieke/java/Challenge190.java
@@ -0,0 +1,19 @@
+import java.util.Scanner ;
+import java.util.regex.* ;
+
+public class Challenge190 {
+ public static void main( String[] args ) {
+ Scanner sc = new Scanner( System.in ) ;
+ String reg1 = "^[A-Z][a-z]+$" ;
+ String reg2 = "^[A-Z]+$" ;
+ String reg3 = "^[a-z]+$" ;
+ System.out.println( "Please enter a line consisting of letters only!" ) ;
+ String line = sc.nextLine( ) ;
+ if ( line.matches( reg1 ) || line.matches( reg2 ) || line.matches( reg3) ){
+ System.out.println( 1 ) ;
+ }
+ else {
+ System.out.println( 0 ) ;
+ }
+ }
+}
diff --git a/challenge-190/ulrich-rieke/perl/ch-1.pl b/challenge-190/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..d9250c5a44
--- /dev/null
+++ b/challenge-190/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter a string consisting of letters only!" ;
+my $s = <STDIN> ;
+chomp $s ;
+while ( $s !~ /^[a-zA-Z]+$/ ) {
+ say "string must consist of letters only! Re-enter!" ;
+ $s = <STDIN> ;
+ chomp $s ;
+}
+if ( $s =~ /^[A-Z]+$/ || $s =~ /^[a-z]+$/ || $s =~ /^[A-Z][a-z]+$/) {
+ say 1 ;
+}
+else {
+ say 0 ;
+}
diff --git a/challenge-190/ulrich-rieke/perl/ch-2.pl b/challenge-190/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..00c6e88377
--- /dev/null
+++ b/challenge-190/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use Algorithm::Combinatorics qw ( variations_with_repetition ) ;
+use List::Util qw ( sum all ) ;
+
+#this task is about finding all combinations of 1 and 2 that add up
+#to the total length of $s , the number that is given at the outset.
+#There is a maximum of <length of $s> 1's, admixing as many 2's as
+#necessary to find a sum of <length of $s>
+
+#we compute the maximal number of 2's in all combinations of 1 and 2
+#and the minimum amount of 1's. Their sum is the shortest possible
+#array of 1's and 2's that adds up to the given length of $s. The
+#maximum length is the length of $s , if the array consists of 1's only
+sub findCombinations {
+ my $length = shift ;
+ my @nums = ( 1 , 2 ) ;
+ my @allCombinations ;
+ my $twos = int( $length / 2 ) ;#maximal number of 2's in the array
+ my $ones = $length - $twos * 2 ;#minimal number of 1's in the array
+ for my $l ( ($ones + $twos)..$length ) {
+ my $iter = variations_with_repetition(\@nums , $l ) ;
+ while ( my $p = $iter->next ) {
+ if ( sum( @$p ) == $length ) {
+ push @allCombinations , $p ;
+ }
+ }
+ }
+ return @allCombinations ;
+}
+
+#function to convert an array of 1's and 2's to a word
+sub arrayToWord {
+ my $array = shift ;
+ if ( all { $_ < 27 } @$array ) {
+ my @letters = map { chr( $_ + 64 ) } @$array ;
+ return join ('' , @letters ) ;
+ }
+ else {
+ return "" ;
+ }
+}
+
+say "Enter a number string!" ;
+my $s = <STDIN> ;
+chomp $s ;
+while ( $s !~ /^\d+$/ ) {
+ say "the string must consist of numbers only! Please re-enter!" ;
+ $s = <STDIN> ;
+ chomp $s ;
+}
+my @allCombinations = findCombinations( length $s ) ;
+my @words ;
+for my $combi ( @allCombinations ) {
+ my @digits ;
+ my $pos = 0 ;
+ for my $num( @$combi ) {
+ push @digits , substr( $s , $pos , $num ) ;
+ $pos += $num ;
+ }
+ my $word = arrayToWord( \@digits ) ;
+ if ( $word ) {
+ push @words , $word ;
+ }
+}
+say join( ',' , sort @words ) ;
diff --git a/challenge-190/ulrich-rieke/raku/ch-1.raku b/challenge-190/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..cc35fa7531
--- /dev/null
+++ b/challenge-190/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,14 @@
+use v6 ;
+
+say "Please enter a word consisting of letters only!" ;
+my $word = $*IN.get ;
+while ( $word !~~ /^<[a..zA..Z]>+$/ ) {
+ say "the word should consist of letters only! Please re-enter!" ;
+ $word = $*IN.get ;
+}
+if ( $word ~~ ( /^<[A..Z]>+$/ | /^<[A..Z]><[a..z]>+$/ | /^<[a..z]>+$/ ) ) {
+ say 1 ;
+}
+else {
+ say 0 ;
+}
diff --git a/challenge-190/ulrich-rieke/rust/ch-1.rs b/challenge-190/ulrich-rieke/rust/ch-1.rs
new file mode 100644
index 0000000000..449e0350d1
--- /dev/null
+++ b/challenge-190/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,19 @@
+use std::io ;
+
+fn main() {
+ println!("Enter a single word!") ;
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( & mut inline ).unwrap( ) ;
+ let mut entered_line : &str = &*inline ;
+ entered_line = entered_line.trim( ) ;
+ let tailstring : &str = &entered_line[1..] ;
+ if (entered_line.chars( ).nth( 0 ).unwrap( ).is_uppercase( ) &&
+ tailstring.chars( ).all( | c | c.is_lowercase( ))) ||
+ entered_line.chars( ).all( | c | c.is_lowercase( ) ) ||
+ entered_line.chars( ).all( | c | c.is_uppercase( )) {
+ println!("1") ;
+ }
+ else {
+ println!("0") ;
+ }
+}