aboutsummaryrefslogtreecommitdiff
path: root/challenge-197
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-197')
-rwxr-xr-xchallenge-197/eric-cheung/python/ch-1.py10
-rwxr-xr-xchallenge-197/eric-cheung/python/ch-2.py23
-rw-r--r--challenge-197/ulrich-rieke/cpp/ch-1.cpp32
-rw-r--r--challenge-197/ulrich-rieke/cpp/ch-2.cpp73
-rw-r--r--challenge-197/ulrich-rieke/haskell/ch-1.hs5
-rw-r--r--challenge-197/ulrich-rieke/haskell/ch-2.hs39
-rw-r--r--challenge-197/ulrich-rieke/perl/ch-1.pl20
-rw-r--r--challenge-197/ulrich-rieke/perl/ch-2.pl58
-rw-r--r--challenge-197/ulrich-rieke/raku/ch-1.raku9
-rw-r--r--challenge-197/ulrich-rieke/raku/ch-2.raku48
-rw-r--r--challenge-197/ulrich-rieke/rust/ch-1.rs24
-rw-r--r--challenge-197/ulrich-rieke/rust/ch-2.rs61
12 files changed, 402 insertions, 0 deletions
diff --git a/challenge-197/eric-cheung/python/ch-1.py b/challenge-197/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..b84bff5774
--- /dev/null
+++ b/challenge-197/eric-cheung/python/ch-1.py
@@ -0,0 +1,10 @@
+
+import numpy as np
+
+## arrInputList = np.array([1, 0, 3, 0, 0, 5]) ## Example 1
+## arrInputList = np.array([1, 6, 4]) ## Example 2
+arrInputList = np.array([0, 1, 0, 2, 0]) ## Example 3
+
+arrOutputList = np.append(arrInputList[arrInputList != 0], arrInputList[arrInputList == 0])
+
+print (arrOutputList)
diff --git a/challenge-197/eric-cheung/python/ch-2.py b/challenge-197/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..f95f735238
--- /dev/null
+++ b/challenge-197/eric-cheung/python/ch-2.py
@@ -0,0 +1,23 @@
+
+## Remarks
+## https://zhenyu0519.github.io/2020/07/12/lc280/
+
+def wiggleSort(arrSubInput):
+
+ bGtr = False
+ arrSubOutput = arrSubInput
+
+ for nLoop in range(len(arrSubOutput) - 1):
+ if bGtr and arrSubOutput[nLoop] <= arrSubOutput[nLoop + 1] or not bGtr and arrSubOutput[nLoop] >= arrSubOutput[nLoop + 1]:
+ arrSubOutput[nLoop], arrSubOutput[nLoop + 1] = arrSubOutput[nLoop + 1], arrSubOutput[nLoop]
+
+ bGtr = not bGtr
+
+ return arrSubOutput
+
+
+## arrInputList = [1, 5, 1, 1, 6, 4] ## Example 1
+## arrInputList = [1, 3, 2, 2, 3, 1] ## Example 2
+arrInputList = [1, 5, 2, 3, 4, 6] ## Example
+
+print (wiggleSort(arrInputList))
diff --git a/challenge-197/ulrich-rieke/cpp/ch-1.cpp b/challenge-197/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..6ca1f48cd8
--- /dev/null
+++ b/challenge-197/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,32 @@
+#include <vector>
+#include <iostream>
+#include <algorithm>
+#include <iterator>
+
+int main( ) {
+ std::cout << "Enter a number of integers , a negative integer to end!\n"
+ ;
+ std::vector<int> numbers ;
+ int num ;
+ std::cin >> num ;
+ while ( num > - 1 ) {
+ numbers.push_back( num ) ;
+ std::cin >> num ;
+ }
+ std::vector<int> ordered ;
+ std::copy_if( numbers.begin( ) , numbers.end( ) ,
+ std::back_inserter( ordered ) , []( int i ) {
+ return i != 0 ; } ) ;
+ int zeronum = std::count( numbers.begin( ) , numbers.end( ) , 0 ) ;
+ if ( zeronum != 0 ) {
+ for ( int i = 0 ; i < zeronum ; i++ )
+ ordered.push_back( 0 ) ;
+ }
+ std::cout << ordered.size( ) << '\n' ;
+ std::cout << '(' ;
+ for ( int n : ordered ) {
+ std::cout << n << ' ' ;
+ }
+ std::cout << ")\n" ;
+ return 0 ;
+}
diff --git a/challenge-197/ulrich-rieke/cpp/ch-2.cpp b/challenge-197/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..e9a0dd71da
--- /dev/null
+++ b/challenge-197/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,73 @@
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include <utility>
+#include <map>
+
+bool myCondition( const std::vector<int> & numbers ) {
+ int len = numbers.size( ) ;
+ if ( len == 3 ) {
+ return ( (numbers[0] < numbers[1]) && ( numbers[1] > numbers[2] ) ) ;
+ }
+ if ( len > 3 ) {
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ if ( i % 2 == 0 ) {
+ if ( ! (numbers[ i ] < numbers[ i + 1 ]) ) {
+ return false ;
+ }
+ }
+ else {
+ if ( ! (numbers[ i ] > numbers[ i + 1 ] ) ) {
+ return false ;
+ }
+ }
+ }
+ }
+ return true ;
+}
+
+void printOut( const std::vector<int> & nums ) {
+ std::cout << '(' ;
+ for ( int i : nums ) {
+ std::cout << i << ' ' ;
+ }
+ std::cout << ')' << std::endl ;
+}
+
+int main( ) {
+ std::cout << "Please enter at least 3 integers, 0 to end!\n" ;
+ std::vector<int> numbers ;
+ int num ;
+ std::cin >> num ;
+ while ( num != 0 ) {
+ numbers.push_back( num ) ;
+ std::cin >> num ;
+ }
+ while ( numbers.size( ) < 3 ) {
+ std::cout << "You should enter at least 3 numbers!\n!" ;
+ std::cin >> num ;
+ numbers.push_back( num ) ;
+ }
+ std::map<int , int> frequencies ;
+ for ( int i : numbers ) {
+ frequencies[i]++;
+ }
+ std::vector<std::pair<int , int>> thePairs ( frequencies.begin( ) ,
+ frequencies.end( ) ) ;
+ std::pair<int , int> maxPair = *std::max_element( thePairs.begin( ) ,
+ thePairs.end( ) , []( const auto & p1 , const auto & p2 ) {
+ return p1.second > p2.second ; } ) ;
+ if ( maxPair.second >= numbers.size( ) - 1 ) {
+ std::cout << "No wiggling possible!\n" ;
+ }
+ else {
+ sort( numbers.begin( ) , numbers.end( ) ) ;
+ while ( std::next_permutation( numbers.begin( ) , numbers.end( ))) {
+ if ( myCondition( numbers ) ) {
+ printOut( numbers ) ;
+ }
+ }
+ }
+ return 0 ;
+}
diff --git a/challenge-197/ulrich-rieke/haskell/ch-1.hs b/challenge-197/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..365c9805ed
--- /dev/null
+++ b/challenge-197/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,5 @@
+module Challenge197
+ where
+
+solution :: [Int] -> [Int]
+solution list = filter ( /= 0 ) list ++ ( filter ( == 0 ) list )
diff --git a/challenge-197/ulrich-rieke/haskell/ch-2.hs b/challenge-197/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..a870aaa28d
--- /dev/null
+++ b/challenge-197/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,39 @@
+module Challenge197_2
+ where
+import Data.List (group , sort , sortOn , (!! ) , permutations )
+import qualified Data.Set as S
+
+myCondition :: [Int] -> Bool
+myCondition numbers
+ |l == 3 = numbers !! 0 < numbers !! 1 && numbers !! 1 > numbers !! 2
+ |l > 3 = all (\p -> fst p < snd p ) firstPairs && all (\p -> fst p
+ > snd p ) secondPairs
+ where
+ l :: Int
+ l = length numbers
+ firstPairs :: [(Int , Int)]
+ firstPairs = map (\i -> (numbers !! i , numbers !! ( i + 1 ))) $
+ filter even [0..l - 2]
+ secondPairs :: [(Int , Int)]
+ secondPairs = map (\i -> ( numbers !! i , numbers !! ( i + 1 ))) $
+ filter odd [0 .. l - 2]
+
+askForInput :: IO [Int]
+askForInput = do
+ putStrLn "Enter at least 3 integers, separated by a blank!"
+ numbers <- getLine
+ let theNumbers = map read $ words numbers
+ if length theNumbers >= 3
+ then return theNumbers
+ else do
+ askForInput
+
+main :: IO ( )
+main = do
+ numbers <- askForInput
+ let theLast = last $ sortOn length $ group $ sort numbers
+ l = length numbers
+ permus = S.toList $ S.fromList $ permutations numbers
+ if (l > 3) && ( length theLast >= (l - 1))
+ then putStrLn "A wiggle order can't be created!"
+ else print $ filter myCondition permus
diff --git a/challenge-197/ulrich-rieke/perl/ch-1.pl b/challenge-197/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..8ec764b5c9
--- /dev/null
+++ b/challenge-197/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter some integers, separated by a blank!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my @nonzeroes = grep { $_ != 0 } @numbers ;
+my @zeroes = grep { $_ == 0 } @numbers ;
+if ( @zeroes ) {
+ for (1..scalar( @zeroes ) ) {
+ push @nonzeroes , 0 ;
+ }
+ say '(' . join( ',' , @nonzeroes ) . ')' ;
+}
+else {
+ say '(' . join ( ',' , @numbers ) . ')' ;
+}
diff --git a/challenge-197/ulrich-rieke/perl/ch-2.pl b/challenge-197/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..f5c1a353ee
--- /dev/null
+++ b/challenge-197/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use Algorithm::Combinatorics qw ( permutations ) ;
+use List::Util qw ( max ) ;
+
+say "Enter at least 3 integers, separated by a blank!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+while ( scalar( @numbers ) < 3 ) {
+ say "Please enter at least 3 integers!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+ @numbers = split( /\s/ , $line ) ;
+}
+my %numberfrequencies ;
+map { $numberfrequencies{ $_ }++ } @numbers ;
+my $len = scalar( @numbers ) ;
+if ( $len > 3 && max( values %numberfrequencies ) >= ($len - 1) ) {
+ say "Wiggle condition can't be fulfilled!" ;
+}
+else {
+ my @validPermus ;
+ my $iter = permutations(\@numbers ) ;
+ while ( my $p = $iter->next ) {
+ if ( myCondition( $p ) ) {
+ push @validPermus , $p ;
+ }
+ }
+ for my $combi ( @validPermus ) {
+ say '(' . join( ',' , @{$combi} ) . ')' ;
+ }
+}
+
+sub myCondition {
+ my $array = shift ;
+ if ( scalar( @{$array} ) == 3 ) {
+ return ($array->[0] < $array->[1]) && $array->[1] > $array->[2] ;
+ }
+ if ( scalar( @{$array} ) > 3 ) {
+ my $len = scalar( @{$array} ) ;
+ for my $i (0.. $len - 2 ) {
+ if ( $i % 2 == 0 ) {
+ unless ( $array->[$i] < $array->[$i + 1] ) {
+ return 0 ;
+ }
+ }
+ else {
+ unless ( $array->[$i] > $array->[$i + 1] ) {
+ return 0 ;
+ }
+ }
+ }
+ return 1 ;
+ }
+}
diff --git a/challenge-197/ulrich-rieke/raku/ch-1.raku b/challenge-197/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..2ce3443863
--- /dev/null
+++ b/challenge-197/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,9 @@
+use v6 ;
+
+say "Enter some integers , separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.split( /\s/ ).map( {.Int} ) ;
+my @nonzeroes = @numbers.grep( { $_ != 0 } ) ;
+my @zeroes = @numbers.grep( { $_ == 0 } ) ;
+my @ordered = @nonzeroes.push( |@zeroes ) ;
+say '(' ~ @ordered.join( ',' ) ~ ')' ;
diff --git a/challenge-197/ulrich-rieke/raku/ch-2.raku b/challenge-197/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..d7882521bb
--- /dev/null
+++ b/challenge-197/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,48 @@
+use v6 ;
+
+sub myCondition( @array --> Bool ) {
+ my $len = @array.elems ;
+ if ( $len == 3 ) {
+ return @array[0] < @array[1] && @array[1] > @array[2] ;
+ }
+ if ( $len > 3 ) {
+ for (0..$len - 2 ) -> $i {
+ if ( $i %% 2 ) {
+ unless ( @array[ $i ] < @array[ $i + 1 ] ) {
+ return False ;
+ }
+ }
+ else {
+ unless ( @array[ $i ] > @array[ $i + 1 ] ) {
+ return False ;
+ }
+ }
+ }
+ }
+ return True ;
+}
+
+say "Enter some integers, separated by a blank!" ;
+my $line = $*IN.get ;
+my @numbers = $line.split( /\s/ ).map( {.Int} ) ;
+while ( @numbers.elems < 3 ) {
+ say "At least 3 numbers should be entered!" ;
+ $line = $*IN.get ;
+ @numbers = $line.split( /\s/ ).map( {.Int} ) ;
+}
+my %numberfrequencies ;
+for @numbers -> $num {
+ %numberfrequencies{ $num }++ ;
+}
+my $len = @numbers.elems ;
+if ( $len > 3 && %numberfrequencies.values.max >= $len - 1 ) {
+ say "Wiggle condition can't be fulfilled!" ;
+}
+else {
+ my @permus = @numbers.permutations.map( {.Array} ) ;
+ for @permus -> @subpermu {
+ if ( myCondition( @subpermu ) ) {
+ say '(' ~ @subpermu.join( ',' ) ~ ')' ;
+ }
+ }
+}
diff --git a/challenge-197/ulrich-rieke/rust/ch-1.rs b/challenge-197/ulrich-rieke/rust/ch-1.rs
new file mode 100644
index 0000000000..98f2704373
--- /dev/null
+++ b/challenge-197/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,24 @@
+use std::io ;
+
+fn main() {
+ println!("Enter some integers, separated by a blank!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let entered_line: &str = &*inline ;
+ let numbers : Vec<i32> = entered_line.split_whitespace( ).map(
+ | s | s.trim( ).parse::<i32>( ).unwrap( )).collect( ) ;
+ let mut ordered : Vec<i32> = Vec::new( ) ;
+ let mut zerocount = 0 ;
+ for i in &numbers {
+ if *i != 0 {
+ ordered.push( *i ) ;
+ }
+ else {
+ zerocount += 1 ;
+ }
+ }
+ for _ in 0..zerocount {
+ ordered.push( 0 ) ;
+ }
+ println!("{:?}" , ordered ) ;
+}
diff --git a/challenge-197/ulrich-rieke/rust/ch-2.rs b/challenge-197/ulrich-rieke/rust/ch-2.rs
new file mode 100644
index 0000000000..84aad1d320
--- /dev/null
+++ b/challenge-197/ulrich-rieke/rust/ch-2.rs
@@ -0,0 +1,61 @@
+use std::io ;
+use itertools::Itertools ;
+use std::collections::{HashSet , HashMap} ;
+
+fn my_condition( a_permu : &Vec<i32> ) -> bool {
+ let len = a_permu.len( ) ;
+ let is_wiggled = match len {
+ l if l < 3 => false ,
+ 3 => a_permu[0] < a_permu[1] && a_permu[1] > a_permu[2] ,
+ _l @ 4..=usize::MAX => {
+ let mut even_indices : Vec<usize> = Vec::new( ) ;
+ let mut odd_indices : Vec<usize> = Vec::new( ) ;
+ for i in 0..len - 1 {
+ if i % 2 == 0 {
+ even_indices.push( i ) ;
+ }
+ else {
+ odd_indices.push( i ) ;
+ }
+ }
+ return even_indices.iter( ).all( | ind | a_permu[*ind]
+ < a_permu[ *ind + 1 ] )
+ && odd_indices.iter( ).all( | ind | a_permu[ *ind ] >
+ a_permu[ *ind + 1 ] ) ;
+ }
+ _ => false ,
+ } ;
+ is_wiggled
+}
+
+fn main() {
+ println!("Please enter some integers, separated by blanks!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let entered_line : &str = &*inline ;
+ let numbers : Vec<i32> = entered_line.split_whitespace( ).
+ map( | s | s.trim( ).parse::<i32>().unwrap( ) ).collect( ) ;
+ let mut frequencies : HashMap<i32 , i32> = HashMap::new( ) ;
+ for n in &numbers {
+ let counter = frequencies.entry( *n ).or_insert( 0 ) ;
+ *counter += 1 ;
+ }
+ let mut values : Vec<i32> = Vec::new( ) ;
+ for ( _ , val ) in frequencies.iter( ) {
+ values.push( *val ) ;
+ }
+ let maxval = values.iter( ).max( ).unwrap( ) ;
+ if numbers.len( ) > 3 && maxval >= &((numbers.len( ) - 1) as i32 ) {
+ println!("Wiggle condition can't be fulfilled!" ) ;
+ }
+ else {
+ let l = numbers.len( ) ;
+ let mut combis : HashSet<Vec<i32>> = HashSet::new( ) ;
+ for perm in numbers.into_iter( ).permutations( l ) {
+ if my_condition( &perm ) {
+ combis.insert( perm ) ;
+ }
+ }
+ println!("{:?}" , combis) ;
+ }
+}