aboutsummaryrefslogtreecommitdiff
path: root/challenge-276
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2024-07-02 13:19:01 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2024-07-02 13:19:01 +0100
commitd3cd6c8a7072e6f3566176284074c04501bfe8e3 (patch)
tree089f60956b2bc3b2fb34cf9423e546a19ae893e7 /challenge-276
parent4f41284bb307dda2b2dab0c26bdefe6723c1755c (diff)
downloadperlweeklychallenge-club-d3cd6c8a7072e6f3566176284074c04501bfe8e3.tar.gz
perlweeklychallenge-club-d3cd6c8a7072e6f3566176284074c04501bfe8e3.tar.bz2
perlweeklychallenge-club-d3cd6c8a7072e6f3566176284074c04501bfe8e3.zip
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke. - Added solutions by Roger Bell_West. - Added solutions by Robbie Hatley. - Added solutions by Simon Green. - Added solutions by Thomas Kohler. - Added solutions by Steven Wilson. - Added solutions by Mark Anderson. - Added solutions by PokGoPun. - Added solutions by W. Luis Mochan. - Added solutions by David Ferrone. - Added solutions by Peter Campbell Smith. - Added solutions by E. Choroba. - Added solutions by Ryan Thompson. - Added solutions by Matthew Neleigh. - Added solutions by Dave Jacoby. - Added solutions by Feng Chang. - Added solutions by Joelle Maslak. - Added solutions by Niels van Dijke. - Added solutions by Mariano Ortega.
Diffstat (limited to 'challenge-276')
-rwxr-xr-xchallenge-276/eric-cheung/python/ch-1.py12
-rwxr-xr-xchallenge-276/eric-cheung/python/ch-2.py9
-rw-r--r--challenge-276/mgo1977/perl/ch-1.pl (renamed from challenge-276/mgo1977/perl/ch1.pl)0
-rw-r--r--challenge-276/mgo1977/perl/ch-2.pl (renamed from challenge-276/mgo1977/perl/ch2.pl)0
-rwxr-xr-xchallenge-276/perlboy1967/perl/ch-1.pl (renamed from challenge-276/perlboy1967/perl/ch1.pl)0
-rwxr-xr-xchallenge-276/perlboy1967/perl/ch-2.pl (renamed from challenge-276/perlboy1967/perl/ch2.pl)0
-rwxr-xr-xchallenge-276/ulrich-rieke/cpp/ch-1.cpp21
-rwxr-xr-xchallenge-276/ulrich-rieke/cpp/ch-2.cpp29
-rwxr-xr-xchallenge-276/ulrich-rieke/haskell/ch-1.hs18
-rwxr-xr-xchallenge-276/ulrich-rieke/haskell/ch-2.hs19
-rwxr-xr-xchallenge-276/ulrich-rieke/perl/ch-1.pl18
-rwxr-xr-xchallenge-276/ulrich-rieke/perl/ch-2.pl17
-rwxr-xr-xchallenge-276/ulrich-rieke/raku/ch-1.raku6
-rwxr-xr-xchallenge-276/ulrich-rieke/raku/ch-2.raku14
-rwxr-xr-xchallenge-276/ulrich-rieke/rust/ch-1.rs15
-rwxr-xr-xchallenge-276/ulrich-rieke/rust/ch-2.rs23
16 files changed, 201 insertions, 0 deletions
diff --git a/challenge-276/eric-cheung/python/ch-1.py b/challenge-276/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..abb3112347
--- /dev/null
+++ b/challenge-276/eric-cheung/python/ch-1.py
@@ -0,0 +1,12 @@
+
+from itertools import combinations
+
+## arrHours = [12, 12, 30, 24, 24] ## Example 1
+## arrHours = [72, 48, 24, 5] ## Example 2
+arrHours = [12, 18, 24] ## Example 3
+
+arrComb = combinations(arrHours, 2)
+
+arrOutput = [arrLoop for arrLoop in list(arrComb) if sum(arrLoop) % 24 == 0]
+
+print (len(arrOutput))
diff --git a/challenge-276/eric-cheung/python/ch-2.py b/challenge-276/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..1f41b77181
--- /dev/null
+++ b/challenge-276/eric-cheung/python/ch-2.py
@@ -0,0 +1,9 @@
+
+## arrInt = [1, 2, 2, 4, 1, 5] ## Example 1
+arrInt = [1, 2, 3, 4, 5] ## Example 2
+
+arrFreq = [arrInt.count(nLoop) for nLoop in set(arrInt)]
+
+nMaxFreq = max(arrFreq)
+
+print (nMaxFreq * arrFreq.count(nMaxFreq))
diff --git a/challenge-276/mgo1977/perl/ch1.pl b/challenge-276/mgo1977/perl/ch-1.pl
index 7fbb808b0b..7fbb808b0b 100644
--- a/challenge-276/mgo1977/perl/ch1.pl
+++ b/challenge-276/mgo1977/perl/ch-1.pl
diff --git a/challenge-276/mgo1977/perl/ch2.pl b/challenge-276/mgo1977/perl/ch-2.pl
index 136f80de1a..136f80de1a 100644
--- a/challenge-276/mgo1977/perl/ch2.pl
+++ b/challenge-276/mgo1977/perl/ch-2.pl
diff --git a/challenge-276/perlboy1967/perl/ch1.pl b/challenge-276/perlboy1967/perl/ch-1.pl
index 9ff82133f6..9ff82133f6 100755
--- a/challenge-276/perlboy1967/perl/ch1.pl
+++ b/challenge-276/perlboy1967/perl/ch-1.pl
diff --git a/challenge-276/perlboy1967/perl/ch2.pl b/challenge-276/perlboy1967/perl/ch-2.pl
index d19bebae7e..d19bebae7e 100755
--- a/challenge-276/perlboy1967/perl/ch2.pl
+++ b/challenge-276/perlboy1967/perl/ch-2.pl
diff --git a/challenge-276/ulrich-rieke/cpp/ch-1.cpp b/challenge-276/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..2802cb5600
--- /dev/null
+++ b/challenge-276/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,21 @@
+#include <iostream>
+#include <vector>
+#include <iterator>
+#include <utility>
+#include <algorithm>
+
+int main( ) {
+ std::cout << "Enter some positive integers , separated by blanks, 'e' to end!\n" ;
+ std::vector<int> numbers { std::istream_iterator<int>( std::cin ) ,
+ std::istream_iterator<int>( ) } ;
+ std::vector<std::pair<int , int>> combinations ;
+ int len = numbers.size( ) ;
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ for ( int j = i + 1 ; j < len ; j++ ) {
+ combinations.push_back( std::make_pair( numbers[i] , numbers[j] ) ) ;
+ }
+ }
+ std::cout << std::count_if( combinations.begin( ) , combinations.end( ) , []
+ ( const auto & p ) { return (p.first + p.second) % 24 == 0 ; } ) << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-276/ulrich-rieke/cpp/ch-2.cpp b/challenge-276/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..a3c5d7e345
--- /dev/null
+++ b/challenge-276/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,29 @@
+#include <vector>
+#include <iostream>
+#include <iterator>
+#include <map>
+
+int main( ) {
+ std::cout << "Enter some integers, separated by blanks, 'e' to end!\n" ;
+ std::vector<int> numbers { std::istream_iterator<int>( std::cin ) ,
+ std::istream_iterator<int>( ) } ;
+ std::map<int , int> frequencies ;
+ for ( int i : numbers ) {
+ frequencies[i]++ ;
+ }
+ int elements = 0 ;//elements with maximum frequency
+ int maximum = 0 ;//maximum frequency
+ for ( auto p : frequencies ) {
+ if ( p.second > maximum )
+ maximum = p.second ;
+ }
+ for ( auto p : frequencies ) {
+ if ( p.second == maximum )
+ elements++ ;
+ }
+ //we want the product of maximum frequency and the number of elements that have it
+ std::cout << elements * maximum << '\n' ;
+ return 0 ;
+}
+
+
diff --git a/challenge-276/ulrich-rieke/haskell/ch-1.hs b/challenge-276/ulrich-rieke/haskell/ch-1.hs
new file mode 100755
index 0000000000..c133891922
--- /dev/null
+++ b/challenge-276/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,18 @@
+module Challenge276
+ where
+import Data.List ( nub , (!!) )
+
+combinations :: Int -> [a] -> [[a]]
+combinations 0 _ = [[]]
+combinations n xs = [ xs !! i : x | i <- [0..(length xs ) - 1 ] ,
+ x <- combinations (n - 1 ) ( drop ( i + 1 ) xs ) ]
+
+solution :: [Int] -> Int
+solution list = length $ filter (\subli -> mod ( sum subli ) 24 == 0 ) $ nub $
+ combinations 2 list
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some positive integers, separated by blanks!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-276/ulrich-rieke/haskell/ch-2.hs b/challenge-276/ulrich-rieke/haskell/ch-2.hs
new file mode 100755
index 0000000000..4c06361f5a
--- /dev/null
+++ b/challenge-276/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,19 @@
+module Challenge276_2
+ where
+import Data.List ( sort , sortOn , group )
+
+solution :: [Int] -> Int
+solution list =
+ let sorted = sort list
+ grouped = group sorted
+ groupedLengths = sortOn length grouped
+ maxLen = length $ last groupedLengths -- maximum frequency
+ howmany = length $ filter ( (== maxLen ) . length ) grouped --elements with
+ --maximum frequency
+ in howmany * maxLen --their product
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some positive integers, separated by blanks!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-276/ulrich-rieke/perl/ch-1.pl b/challenge-276/ulrich-rieke/perl/ch-1.pl
new file mode 100755
index 0000000000..9fb2cc7793
--- /dev/null
+++ b/challenge-276/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use Algorithm::Combinatorics qw ( combinations ) ;
+
+say "Enter some positive integers , separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s+/ , $line ) ;
+my $total = 0 ;
+my $iter = combinations( \@numbers , 2 ) ;
+while ( my $c = $iter->next ) {
+ if ( ($c->[0] + $c->[1]) % 24 == 0 ) {
+ $total++ ;
+ }
+}
+say $total ;
diff --git a/challenge-276/ulrich-rieke/perl/ch-2.pl b/challenge-276/ulrich-rieke/perl/ch-2.pl
new file mode 100755
index 0000000000..6278f6af18
--- /dev/null
+++ b/challenge-276/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( max ) ;
+
+say "Enter some integers, separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s+/ , $line ) ;
+my %frequencies ;
+map { $frequencies{$_}++ } @numbers ;
+my $maximum = max( values( %frequencies ) ) ;
+my $maxElements = scalar( grep { $frequencies{ $_ } == $maximum } keys %frequencies ) ;
+#what we are interested in is the product of maximum frequency and the number of element
+#that are that frequent
+say ( $maxElements * $maximum ) ;
diff --git a/challenge-276/ulrich-rieke/raku/ch-1.raku b/challenge-276/ulrich-rieke/raku/ch-1.raku
new file mode 100755
index 0000000000..5e250b40cc
--- /dev/null
+++ b/challenge-276/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,6 @@
+use v6 ;
+
+say "Enter some positive integers, separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+say @numbers.combinations( 2 ).grep( { $_.sum %% 24 } ).elems ;
diff --git a/challenge-276/ulrich-rieke/raku/ch-2.raku b/challenge-276/ulrich-rieke/raku/ch-2.raku
new file mode 100755
index 0000000000..246fe012e1
--- /dev/null
+++ b/challenge-276/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,14 @@
+use v6 ;
+
+say "Enter some integers, separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my %frequencies ;
+@numbers.map( {%frequencies{$_}++} ) ;
+my $maximum = %frequencies.values.max ;
+my @keys = %frequencies.keys ;
+my $number = @keys.grep( {%frequencies{$_} == $maximum} ).elems ;#number of element
+#with maximum frequency
+#what we look for is the product of maximum frequency and the number of elements that
+#have that maximum frequency!
+say ( $number * $maximum ) ;
diff --git a/challenge-276/ulrich-rieke/rust/ch-1.rs b/challenge-276/ulrich-rieke/rust/ch-1.rs
new file mode 100755
index 0000000000..b743e9bc77
--- /dev/null
+++ b/challenge-276/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,15 @@
+use std::io ;
+use itertools::Itertools ;
+
+fn main() {
+ println!("Enter some positive integers, separated by blanks!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let entered_line : &str = inline.as_str( ).trim( ) ;
+ let numbers : Vec<u32> = entered_line.split_whitespace( ).map( | s |
+ s.parse::<u32>( ).unwrap( ) ).collect( ) ;
+ let solution = numbers.into_iter( ).combinations( 2 ).filter( | vec | {
+ (vec[0] + vec[1]) % 24 == 0
+ } ).count( ) ;
+ println!("{}" , solution ) ;
+}
diff --git a/challenge-276/ulrich-rieke/rust/ch-2.rs b/challenge-276/ulrich-rieke/rust/ch-2.rs
new file mode 100755
index 0000000000..f395f112a0
--- /dev/null
+++ b/challenge-276/ulrich-rieke/rust/ch-2.rs
@@ -0,0 +1,23 @@
+use std::io ;
+use std::collections::HashMap ;
+
+fn main() {
+ println!("Enter some positive integers!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let entered_line : &str = inline.as_str( ).trim( ) ;
+ let numbers : Vec<u32> = entered_line.split_whitespace( ).map( | s |
+ s.parse::<u32>( ).unwrap( ) ).collect( ) ;
+ let mut frequencies : HashMap<u32 , usize> = HashMap::new( ) ;
+ for i in numbers {
+ frequencies.entry( i ).and_modify( |n| {*n += 1} ).or_insert( 1 ) ;
+ }
+ let max_freq : usize = *frequencies.values( ).max( ).unwrap( ) ;
+ let mut number : usize = 0 ; //how many numbers have maximum frequency ?
+ for (_ , v) in &frequencies {
+ if *v == max_freq {
+ number += 1 ;
+ }
+ }
+ println!("{}" , number * max_freq) ;
+}