aboutsummaryrefslogtreecommitdiff
path: root/challenge-321
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-321')
-rw-r--r--challenge-321/conor-hoekstra/bqn/ch-1.bqn (renamed from challenge-321/conor-hoekstra/ch-1.bqn)0
-rw-r--r--challenge-321/conor-hoekstra/bqn/ch-2.bqn (renamed from challenge-321/conor-hoekstra/ch-2.bqn)0
-rwxr-xr-xchallenge-321/eric-cheung/python/ch-1.py19
-rwxr-xr-xchallenge-321/eric-cheung/python/ch-2.py29
-rwxr-xr-xchallenge-321/perlboy1967/perl/ch-1.pl (renamed from challenge-321/perlboy1967/perl/ch1.pl)0
-rwxr-xr-xchallenge-321/perlboy1967/perl/ch-2.pl (renamed from challenge-321/perlboy1967/perl/ch2.pl)0
-rwxr-xr-xchallenge-321/ulrich-rieke/cpp/ch-1.cpp38
-rwxr-xr-xchallenge-321/ulrich-rieke/cpp/ch-2.cpp32
-rwxr-xr-xchallenge-321/ulrich-rieke/haskell/ch-1.hs22
-rwxr-xr-xchallenge-321/ulrich-rieke/haskell/ch-2.hs21
-rwxr-xr-xchallenge-321/ulrich-rieke/perl/ch-1.pl21
-rwxr-xr-xchallenge-321/ulrich-rieke/perl/ch-2.pl23
-rwxr-xr-xchallenge-321/ulrich-rieke/raku/ch-1.raku13
-rwxr-xr-xchallenge-321/ulrich-rieke/raku/ch-2.raku21
-rwxr-xr-xchallenge-321/ulrich-rieke/rust/ch-1.rs23
-rwxr-xr-xchallenge-321/ulrich-rieke/rust/ch-2.rs26
16 files changed, 288 insertions, 0 deletions
diff --git a/challenge-321/conor-hoekstra/ch-1.bqn b/challenge-321/conor-hoekstra/bqn/ch-1.bqn
index a67f8afb62..a67f8afb62 100644
--- a/challenge-321/conor-hoekstra/ch-1.bqn
+++ b/challenge-321/conor-hoekstra/bqn/ch-1.bqn
diff --git a/challenge-321/conor-hoekstra/ch-2.bqn b/challenge-321/conor-hoekstra/bqn/ch-2.bqn
index 8a98af3d4a..8a98af3d4a 100644
--- a/challenge-321/conor-hoekstra/ch-2.bqn
+++ b/challenge-321/conor-hoekstra/bqn/ch-2.bqn
diff --git a/challenge-321/eric-cheung/python/ch-1.py b/challenge-321/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..6bd299ac82
--- /dev/null
+++ b/challenge-321/eric-cheung/python/ch-1.py
@@ -0,0 +1,19 @@
+
+## arrNums = [1, 2, 4, 3, 5, 6] ## Example 1
+## arrNums = [0, 2, 4, 8, 3, 5] ## Example 2
+arrNums = [7, 3, 1, 0, 5, 9] ## Example 3
+
+arrNums = sorted(arrNums)
+
+arrAvg = []
+
+while len(arrNums) > 0:
+ nMax = arrNums[-1]
+ nMin = arrNums[0]
+
+ arrAvg.append((nMax + nMin) / 2)
+
+ arrNums.pop(-1)
+ arrNums.pop(0)
+
+print (len(set(arrAvg)))
diff --git a/challenge-321/eric-cheung/python/ch-2.py b/challenge-321/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..092cd5378f
--- /dev/null
+++ b/challenge-321/eric-cheung/python/ch-2.py
@@ -0,0 +1,29 @@
+
+def GetRefineStr (strInput):
+ arrTemp = list(strInput)
+ while "#" in arrTemp:
+ nPosBackSpace = arrTemp.index("#")
+ arrTemp.pop(nPosBackSpace)
+ arrTemp.pop(nPosBackSpace - 1)
+
+ return "".join(arrTemp)
+
+## Example 1
+## strInput_01 = "ab#c"
+## strInput_02 = "ad#c"
+
+## Example 2
+## strInput_01 = "ab##"
+## strInput_02 = "a#b#"
+
+## Example 3
+strInput_01 = "a#b"
+strInput_02 = "c"
+
+strInput_01_rev = GetRefineStr(strInput_01)
+strInput_02_rev = GetRefineStr(strInput_02)
+
+## print (strInput_01_rev)
+## print (strInput_02_rev)
+
+print (strInput_01_rev == strInput_02_rev)
diff --git a/challenge-321/perlboy1967/perl/ch1.pl b/challenge-321/perlboy1967/perl/ch-1.pl
index 36a3e5678b..36a3e5678b 100755
--- a/challenge-321/perlboy1967/perl/ch1.pl
+++ b/challenge-321/perlboy1967/perl/ch-1.pl
diff --git a/challenge-321/perlboy1967/perl/ch2.pl b/challenge-321/perlboy1967/perl/ch-2.pl
index e8f7b52389..e8f7b52389 100755
--- a/challenge-321/perlboy1967/perl/ch2.pl
+++ b/challenge-321/perlboy1967/perl/ch-2.pl
diff --git a/challenge-321/ulrich-rieke/cpp/ch-1.cpp b/challenge-321/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..6dc8d9402b
--- /dev/null
+++ b/challenge-321/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,38 @@
+#include <vector>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <numeric>
+#include <set>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::string word ;
+ std::istringstream istr { text } ;
+ while ( std::getline( istr , word , delimiter ) )
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+int main( ) {
+ std::cout << "Enter some numbers , their total number must be even!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<double> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stod( s ) ) ;
+ std::set<double> averages ;
+ while ( numbers.size( ) > 0 ) {
+ auto maxpos = std::max_element( numbers.begin( ) , numbers.end( ) ) ;
+ auto maxi = *maxpos ;
+ numbers.erase( maxpos ) ;
+ auto minpos = std::min_element( numbers.begin( ) , numbers.end( ) ) ;
+ auto mini = *minpos ;
+ numbers.erase( minpos ) ;
+ double average = ( maxi + mini ) / 2.0 ;
+ averages.insert( average ) ;
+ }
+ std::cout << averages.size( ) << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-321/ulrich-rieke/cpp/ch-2.cpp b/challenge-321/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..caf736cbfb
--- /dev/null
+++ b/challenge-321/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,32 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <sstream>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::istringstream istr { text } ;
+ std::string word ;
+ while ( std::getline( istr , word , delimiter ) )
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+std::string reduceStr( std::string word ) {
+ auto it = word.find( "#" ) ;
+ while ( it != std::string::npos ) {
+ word = word.erase( --it , 2 ) ;
+ it = word.find( "#" ) ;
+ }
+ return word ;
+}
+
+int main( ) {
+ std::cout << "Enter 2 words witz zero or more #!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::cout << std::boolalpha << ( reduceStr( tokens[0] ) ==
+ reduceStr( tokens[1] ) ) << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-321/ulrich-rieke/haskell/ch-1.hs b/challenge-321/ulrich-rieke/haskell/ch-1.hs
new file mode 100755
index 0000000000..775fbe86cd
--- /dev/null
+++ b/challenge-321/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,22 @@
+module Challenge321
+ where
+import qualified Data.Set as S
+import Data.List( sort )
+--the basic idea is : we sort the list and then pair the smallest with the
+--largest element, the second smallest with the second largest and so on
+--by zipping the first half of the sorted element with the reversed second half
+
+solution :: [Double] -> Int
+solution list =
+ let
+ sorted = sort list
+ half = div (length sorted) 2
+ zipped = zip ( take half sorted ) ( reverse $ drop half sorted )
+ averages = S.fromList $ map (\p -> ( fst p + snd p ) / 2.0 ) zipped
+ in S.size averages
+
+main :: IO ( )
+main = do
+ putStrLn "Enter an even number of numbers separated by whitespace!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-321/ulrich-rieke/haskell/ch-2.hs b/challenge-321/ulrich-rieke/haskell/ch-2.hs
new file mode 100755
index 0000000000..6652e6c2d8
--- /dev/null
+++ b/challenge-321/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,21 @@
+module Challenge321_2
+ where
+import Data.List ( findIndex )
+
+reduceStr :: String -> String
+reduceStr str = until ( not . elem '#' ) myReduce str
+ where
+ myReduce :: String -> String
+ myReduce intermediate = case findIndex ( == '#' ) intermediate of
+ Just pos -> take ( pos - 1 ) intermediate ++ drop ( pos + 1 )
+ intermediate
+ Nothing -> intermediate
+
+solution :: [String] -> Bool
+solution list = reduceStr (head list) == reduceStr( last list )
+
+main :: IO ( )
+main = do
+ putStrLn "Enter 2 strings with zero or more #!"
+ stringline <- getLine
+ print $ solution $ words stringline
diff --git a/challenge-321/ulrich-rieke/perl/ch-1.pl b/challenge-321/ulrich-rieke/perl/ch-1.pl
new file mode 100755
index 0000000000..117ad4a2cc
--- /dev/null
+++ b/challenge-321/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+#the basic idea is : sort the numbers and then pair the smallest with the largest,
+#the second smallest with the second largest and so on. Count the number of
+#different averages!
+
+say "Enter an even number of numbers separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my @sorted = sort { $a <=> $b } @numbers ;
+my %averages ;
+my $len = scalar( @sorted ) ;
+for my $i (0.. $len / 2 - 1 ) {
+ my $average = ($sorted[ $i ] + $sorted[ $len - $i - 1 ]) / 2 ;
+ $averages{$average}++ ;
+}
+say scalar( keys %averages ) ;
diff --git a/challenge-321/ulrich-rieke/perl/ch-2.pl b/challenge-321/ulrich-rieke/perl/ch-2.pl
new file mode 100755
index 0000000000..7c717563ef
--- /dev/null
+++ b/challenge-321/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub reduceStr {
+ my $string = shift ;
+ while ( $string =~ s/[^#]#//g ) {
+ }
+ return $string ;
+}
+
+say "Enter 2 strings with zero or more #!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @words = split( /\s/ , $line ) ;
+if ( reduceStr( $words[0] ) eq reduceStr( $words[1] )) {
+ say "true" ;
+}
+else {
+ say "false" ;
+}
+
diff --git a/challenge-321/ulrich-rieke/raku/ch-1.raku b/challenge-321/ulrich-rieke/raku/ch-1.raku
new file mode 100755
index 0000000000..cb2c60c6ee
--- /dev/null
+++ b/challenge-321/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,13 @@
+use v6 ;
+
+say "Enter an even number of numbers , separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {+$_} ) ;
+my %averages ;
+my @sorted = @numbers.sort( {$^a <=> $^b} ) ;
+my $len = @numbers.elems ;
+for (0..$len div 2 - 1 ) -> $pos {
+ my $average = (@sorted[$pos] + @sorted[$len - $pos - 1] ) / 2.0 ;
+ %averages{$average}++ ;
+}
+say %averages.keys.elems ;
diff --git a/challenge-321/ulrich-rieke/raku/ch-2.raku b/challenge-321/ulrich-rieke/raku/ch-2.raku
new file mode 100755
index 0000000000..ce0c7e20ad
--- /dev/null
+++ b/challenge-321/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,21 @@
+use v6 ;
+
+sub reduceStr( $string is rw ) {
+ while ( $string ~~ /'#'/ ) {
+ my $pos = $string.index( '#' ) ;
+ my $firstpart = $string.substr( 0 , $pos - 1 ) ;
+ if ( $pos < $string.chars - 1 ) {
+ my $secondpart = $string.substr( $pos + 1 ) ;
+ $string = $firstpart ~ $secondpart ;
+ }
+ else {
+ $string = $firstpart ;
+ }
+ }
+ return $string ;
+}
+
+say "Enter 2 strings with zero or more #!" ;
+my $line = $*IN.get ;
+my @words = $line.words ;
+say (reduceStr(@words[0]) eq reduceStr(@words[1])) ;
diff --git a/challenge-321/ulrich-rieke/rust/ch-1.rs b/challenge-321/ulrich-rieke/rust/ch-1.rs
new file mode 100755
index 0000000000..3c0e5fd50a
--- /dev/null
+++ b/challenge-321/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,23 @@
+use std::io ;
+use std::collections::HashSet ;
+
+fn main() {
+ println!("Enter some numbers, separated by blanks!");
+ println!("The length of the number array should be even!" ) ;
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let mut numbers : Vec<f32> = inline.trim( ).split_whitespace( ).map( |s|
+ s.parse::<f32>().unwrap( ) ).collect( ) ;
+ //in order to collect all the f32 averages in a HashSet one has
+ //to convert every f32 average into a string since f32 doesn't fulfil
+ //the necessary traits!
+ let mut averages : HashSet<String> = HashSet::new( ) ;
+ numbers.sort_by(|a , b| a.partial_cmp(b).unwrap( ) ) ;
+ let len : usize = numbers.len( ) ;
+ for i in 0..len / 2 {
+ let average : f32 = ( numbers[i] + numbers[ len - i - 1] ) / 2.0 ;
+ let aver_str = average.to_string( ) ;
+ averages.insert( aver_str ) ;
+ }
+ println!("{}" , averages.len( ) ) ;
+}
diff --git a/challenge-321/ulrich-rieke/rust/ch-2.rs b/challenge-321/ulrich-rieke/rust/ch-2.rs
new file mode 100755
index 0000000000..1ce79d2354
--- /dev/null
+++ b/challenge-321/ulrich-rieke/rust/ch-2.rs
@@ -0,0 +1,26 @@
+use std::io ;
+
+fn reduce_str( word : &str ) -> String {
+ let len : usize = word.chars( ).count( ) ;
+ let mut my_word : String = word.into( ) ;
+ while let Some( pos ) = my_word.find( "#" ) {
+ let first_part : &str = &my_word[..pos - 1] ;
+ if pos < len - 1 {
+ let second_part : &str = &my_word[pos + 1..] ;
+ let first_str : String = first_part.into( ) ;
+ my_word = first_str + second_part ;
+ }
+ else {
+ my_word = first_part.into( ) ;
+ }
+ }
+ my_word
+}
+
+fn main() {
+ println!("Enter 2 words with zero or more #!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let words : Vec<&str> = inline.trim( ).split_whitespace( ).collect( ) ;
+ println!("{}" , reduce_str( words[0] ) == reduce_str( words[1] ) ) ;
+}