diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-06-11 13:20:59 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-06-11 13:20:59 +0100 |
| commit | f18d8d36fd5ac5fbc0f1686fa737213cfb0c4cbd (patch) | |
| tree | ad974bf13ceadafa33de46f5690a52d9da8e5f01 | |
| parent | a08549ab8d51fba7f19190abaf01ec59ba175307 (diff) | |
| download | perlweeklychallenge-club-f18d8d36fd5ac5fbc0f1686fa737213cfb0c4cbd.tar.gz perlweeklychallenge-club-f18d8d36fd5ac5fbc0f1686fa737213cfb0c4cbd.tar.bz2 perlweeklychallenge-club-f18d8d36fd5ac5fbc0f1686fa737213cfb0c4cbd.zip | |
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke.
- Added solutions by Wanderdoc.
- Added solutions by Mark Anderson.
- Added solutions by Peter Meszaros.
- Added solutions by Niels van Dijke.
- Added solutions by E. Choroba.
- Added solutions by Peter Campbell Smith.
- Added solutions by David Ferrone.
- Added solutions by Luca Ferrari.
- Added solutions by Thomas Kohler.
- Added solutions by Dave Jacoby.
- Added solutions by Robbie Hatley.
- Added solutions by PokGoPun.
- Added solutions by Roger Bell_West.
33 files changed, 3028 insertions, 2400 deletions
diff --git a/challenge-273/eric-cheung/python/ch-1.py b/challenge-273/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..e01e8f5dca --- /dev/null +++ b/challenge-273/eric-cheung/python/ch-1.py @@ -0,0 +1,31 @@ +
+def round(dInput):
+ return int(dInput) + (1 if dInput - int(dInput) >= 0.5 else 0)
+
+## Example 1
+strInput = "perl"
+strChar = "e"
+
+## Example 2
+## strInput = "java"
+## strChar = "a"
+
+## Example 3
+## strInput = "python"
+## strChar = "m"
+
+## Example 4
+## strInput = "ada"
+## strChar = "a"
+
+## Example 5
+## strInput = "ballerina"
+## strChar = "l"
+
+## Example 6
+## strInput = "analitik"
+## strChar = "k"
+
+nPercent = round(strInput.count(strChar) * 100 / len(strInput))
+
+print (nPercent)
diff --git a/challenge-273/eric-cheung/python/ch-2.py b/challenge-273/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..2b9c57d362 --- /dev/null +++ b/challenge-273/eric-cheung/python/ch-2.py @@ -0,0 +1,15 @@ +
+## strInput = "aabb" ## Example 1
+## strInput = "abab" ## Example 2
+## strInput = "aaa" ## Example 3
+strInput = "bbb" ## Example 4
+
+if strInput.count("b") <= 1:
+ print (False)
+else:
+ nFirstBPos = strInput.index("b")
+
+ nNumOfA = strInput[nFirstBPos + 1:].count("a")
+ nNumOfB = strInput[nFirstBPos + 1:].count("b")
+
+ print (nNumOfA == 0 and nNumOfB > 0)
diff --git a/challenge-273/perlboy1967/perl/ch1.pl b/challenge-273/perlboy1967/perl/ch-1.pl index 1b63f8c4ec..1b63f8c4ec 100755 --- a/challenge-273/perlboy1967/perl/ch1.pl +++ b/challenge-273/perlboy1967/perl/ch-1.pl diff --git a/challenge-273/perlboy1967/perl/ch2.pl b/challenge-273/perlboy1967/perl/ch-2.pl index db7ee646d1..db7ee646d1 100755 --- a/challenge-273/perlboy1967/perl/ch2.pl +++ b/challenge-273/perlboy1967/perl/ch-2.pl diff --git a/challenge-273/ulrich-rieke/cpp/ch-1.cpp b/challenge-273/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..5e98e0873d --- /dev/null +++ b/challenge-273/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,25 @@ +#include <iostream>
+#include <string>
+#include <algorithm>
+#include <cmath>
+
+int main( ) {
+ std::cout << "Enter a word!\n" ;
+ std::string word ;
+ std::cin >> word ;
+ std::cout << "Enter a character!\n" ;
+ char c ;
+ std::cin >> c ;
+ int frequency = std::count( word.begin( ) , word.end( ) , c ) ;
+ double percentage = (static_cast<double>( frequency ) / word.length( ) )
+ * 100.0 ;
+ double bottom = std::floor( percentage ) ;
+ if ( percentage - bottom > 0.4 ) {
+ std::cout << bottom + 1 << '\n' ;
+ }
+ else {
+ std::cout << bottom << '\n' ;
+ }
+ return 0 ;
+}
+
diff --git a/challenge-273/ulrich-rieke/cpp/ch-2.cpp b/challenge-273/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..27cf48fd06 --- /dev/null +++ b/challenge-273/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,16 @@ +#include <iostream>
+#include <string>
+
+int main( ) {
+ std::cout << "Enter a word!\n" ;
+ std::string word ;
+ std::cin >> word ;
+ std::string result { "false" } ;
+ auto pos = word.find( 'b' ) ;
+ if ( pos != std::string::npos ) {
+ if ( word.substr( pos + 1 ).find( 'a' ) == std::string::npos )
+ result = "true" ;
+ }
+ std::cout << result << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-273/ulrich-rieke/haskell/ch-1.hs b/challenge-273/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..836a63edef --- /dev/null +++ b/challenge-273/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,18 @@ +module Challenge273
+ where
+import Data.List ( findIndices )
+
+solution :: String -> Char -> Int
+solution haystack needle =
+ let percentage = ((fromIntegral $ length $ findIndices ( == needle ) haystack) /
+ (fromIntegral $ length haystack)) * 100.0
+ bottom = floor percentage
+ in if percentage - ( fromIntegral bottom ) > 0.4 then bottom + 1 else bottom
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a word!"
+ word <- getLine
+ putStrLn "Enter a character!"
+ character <- getLine
+ print $ solution word ( head character )
diff --git a/challenge-273/ulrich-rieke/haskell/ch-2.hs b/challenge-273/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..4e878c0156 --- /dev/null +++ b/challenge-273/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,16 @@ +module Challenge273_2
+ where
+import Data.List ( findIndex )
+import Data.Maybe ( fromJust )
+
+condition :: String -> Bool
+condition str = elem 'b' str && notElem 'a' ( drop ( pos + 1 ) str )
+ where
+ pos :: Int
+ pos = fromJust $ findIndex ( == 'b' ) str
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a word!"
+ word <- getLine
+ print $ condition word
diff --git a/challenge-273/ulrich-rieke/perl/ch-1.pl b/challenge-273/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..859219bee6 --- /dev/null +++ b/challenge-273/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use POSIX ;
+
+say "Enter a word!" ;
+my $word = <STDIN> ;
+chomp $word ;
+say "Enter a character!" ;
+my $character = <STDIN> ;
+chomp $character ;
+my $percentage = (scalar ( grep { $_ eq $character } split(
+ // , $word ) ) / length $word ) * 100 ;
+if ( $percentage - floor( $percentage ) > 0.4 ) {
+ say floor( $percentage ) + 1 ;
+}
+else {
+ say floor( $percentage ) ;
+}
diff --git a/challenge-273/ulrich-rieke/perl/ch-2.pl b/challenge-273/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..7e9f6e1868 --- /dev/null +++ b/challenge-273/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter a word!" ;
+my $word = <STDIN> ;
+chomp $word ;
+my $result = "false" ;
+if ( $word =~ /b/ ) {
+ my $pos = index( $word , 'b' ) ;
+ if ( substr( $word , $pos + 1 ) !~ /a/ ) {
+ $result = "true" ;
+ }
+}
+say $result ;
diff --git a/challenge-273/ulrich-rieke/raku/ch-1.raku b/challenge-273/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..e7e41a7a06 --- /dev/null +++ b/challenge-273/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,8 @@ +use v6 ;
+
+say "Enter a word!" ;
+my $word = $*IN.get ;
+say "Enter a character!" ;
+my $needle = $*IN.get ;
+my $count = $word.comb.grep( {$_ eq $needle} ).elems ;
+say (( $count / $word.chars ) * 100).round ;
diff --git a/challenge-273/ulrich-rieke/raku/ch-2.raku b/challenge-273/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..784e645bb0 --- /dev/null +++ b/challenge-273/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,12 @@ +use v6 ;
+
+say "Enter a word!" ;
+my $word = $*IN.get ;
+my $result = False ;
+if ( $word ~~ /b/ ) {
+ my $pos = $word.index( 'b' ) ;
+ if ($word.substr( $pos + 1 ) !~~ /a/) {
+ $result = True ;
+ }
+}
+say $result ;
diff --git a/challenge-273/ulrich-rieke/rust/ch-1.rs b/challenge-273/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..c9bc619bc8 --- /dev/null +++ b/challenge-273/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,18 @@ +use std::io ; + +fn main() { + println!("Enter a word!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let word_line : &str = inline.as_str( ).trim( ) ; + println!("Enter a character!" ) ; + let mut cline : String = String::new( ) ; + io::stdin( ).read_line( &mut cline ).unwrap( ) ; + let charline : &str = cline.as_str( ).trim( ) ; + let needle : char = charline.chars( ).nth( 0 ).unwrap( ) ; + let frequency = word_line.chars( ).filter( | c | *c == needle ). + count( ) ; + let result : u8 = ((frequency as f32 / word_line.chars( ).count( ) as f32) + * 100.0).round( ) as u8 ; + println!("{}" , result ) ; +} diff --git a/challenge-273/ulrich-rieke/rust/ch-2.rs b/challenge-273/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..3c8d0157f5 --- /dev/null +++ b/challenge-273/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,17 @@ +use std::io ; + +fn main() { + println!("Enter a word!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = inline.as_str( ).trim( ) ; + let mut result : bool = false ; + if entered_line.contains( "b" ) { + let bpos : usize = entered_line.find( "b" ).unwrap( ) ; + let ( _ , second ) = entered_line.split_at( bpos ) ; + if ! second.contains( "a" ) { + result = true ; + } + } + println!("{}" , result) ; +} diff --git a/challenge-273/wanderdoc/perl/ch-1.pl b/challenge-273/wanderdoc/perl/ch-1.pl new file mode 100755 index 0000000000..da0e3ba65a --- /dev/null +++ b/challenge-273/wanderdoc/perl/ch-1.pl @@ -0,0 +1,71 @@ +#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a string, $str and a character $char.
+
+Write a script to return the percentage, nearest whole, of given character in the given string.
+Example 1
+
+Input: $str = "perl", $char = "e"
+Output: 25
+
+Example 2
+
+Input: $str = "java", $char = "a"
+Output: 50
+
+Example 3
+
+Input: $str = "python", $char = "m"
+Output: 0
+
+Example 4
+
+Input: $str = "ada", $char = "a"
+Output: 67
+
+Example 5
+
+Input: $str = "ballerina", $char = "l"
+Output: 22
+
+Example 6
+
+Input: $str = "analitik", $char = "k"
+Output: 13
+=cut
+
+
+use List::Util qw(sum);
+# use Math::Round;
+use Test2::V0;
+
+# sprintf uses the Round half to even method.
+# Math::Round would do the right thing.
+# Here we imitate $Math::Round::half to fix the sprintf behavior.
+use constant epsilon => 1e-6;
+
+is(percentage_character('perl', 'e'), 25, 'Example 1');
+is(percentage_character('java', 'a'), 50, 'Example 2');
+is(percentage_character('python', 'm'), 0, 'Example 3');
+is(percentage_character('ada', 'a'), 67, 'Example 4');
+is(percentage_character('ballerina', 'l'), 22, 'Example 5');
+is(percentage_character('analitik', 'k'), 13, 'Example 6');
+done_testing();
+
+
+sub percentage_character
+{
+ my $string = $_[0];
+ my $character = $_[1];
+ my %freq;
+ for my $chr (split(//, lc $string))
+ {
+ $freq{$chr}++;
+ }
+ my $this_freq = $freq{ lc $character } // 0;
+ return # round($this_freq * 100 / sum(values %freq));
+ sprintf("%.0f", (epsilon + $this_freq / sum(values %freq)) * 100 );
+}
\ No newline at end of file diff --git a/challenge-273/wanderdoc/perl/ch-2.pl b/challenge-273/wanderdoc/perl/ch-2.pl new file mode 100755 index 0000000000..a718d82948 --- /dev/null +++ b/challenge-273/wanderdoc/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a string, $str.
+
+Write a script to return true if there is at least one b, and no a appears after the first b.
+Example 1
+
+Input: $str = "aabb"
+Output: true
+
+Example 2
+
+Input: $str = "abab"
+Output: false
+
+Example 3
+
+Input: $str = "aaa"
+Output: false
+
+Example 4
+
+Input: $str = "bbb"
+Output: true
+=cut
+
+use constant false => 0;
+use constant true => 1;
+
+use Test2::V0;
+
+is(b_after_a('aabb'), true, 'Example 1');
+is(b_after_a('abab'), false, 'Example 2');
+is(b_after_a('aaa'), false, 'Example 3');
+is(b_after_a('bbb'), true, 'Example 4');
+is(b_after_a('bbba'), true, 'Example 5'); # no a appears after the _first_ b
+done_testing();
+
+sub b_after_a
+{
+ my $string = $_[0];
+ return ($string =~ /b/ and $string !~ /[^b]b(?=a)/) * 1;
+ # 'no a after _any_ b' would be: $string !~ /b(?=a)/
+}
\ No newline at end of file diff --git a/stats/pwc-challenge-272.json b/stats/pwc-challenge-272.json new file mode 100644 index 0000000000..7d5edee691 --- /dev/null +++ b/stats/pwc-challenge-272.json @@ -0,0 +1,665 @@ +{ + "subtitle" : { + "text" : "[Champions: 35] Last updated at 2024-06-11 12:09:13 GMT" + }, + "xAxis" : { + "type" : "category" + }, + "plotOptions" : { + "series" : { + "borderWidth" : 0, + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + } + } + }, + "tooltip" : { + "followPointer" : 1, + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" + }, + "legend" : { + "enabled" : 0 + }, + "series" : [ + { + "data" : [ + { + "y" : 3, + "name" : "Adam Russell", + "drilldown" : "Adam Russell" + }, + { + "drilldown" : "Ali Moradi", + "name" : "Ali Moradi", + "y" : 5 + }, + { + "drilldown" : "Andrew Schneider", + "y" : 3, + "name" : "Andrew Schneider" + }, + { + "drilldown" : "Arne Sommer", + "y" : 3, + "name" : "Arne Sommer" + }, + { + "drilldown" : "Athanasius", + "name" : "Athanasius", + "y" : 4 + }, + { + "name" : "BarrOff", + "y" : 2, + "drilldown" : "BarrOff" + }, + { + "drilldown" : "Bob Lied", + "y" : 2, + "name" : "Bob Lied" + }, + { + "drilldown" : "Bruce Gray", + "y" : 2, + "name" : "Bruce Gray" + }, + { + "name" : "Dave Jacoby", + "y" : 2, + "drilldown" : "Dave Jacoby" + }, + { + "drilldown" : "David Ferrone", + "y" : 2, + "name" : "David Ferrone" + }, + { + "name" : "E. Choroba", + "y" : 2, + "drilldown" : "E. Choroba" + }, + { + "y" : 2, + "name" : "Feng Chang", + "drilldown" : "Feng Chang" + }, + { + "y" : 5, + "name" : "Jaldhar H. Vyas", + "drilldown" : "Jaldhar H. Vyas" + }, + { + "y" : 2, + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek" + }, + { + "drilldown" : "Jorg Sommrey", + "y" : 3, + "name" : "Jorg Sommrey" + }, + { + "drilldown" : "Laurent Rosenfeld", + "y" : 6, + "name" : "Laurent Rosenfeld" + }, + { + "drilldown" : "Luca Ferrari", + "y" : 11, + "name" : "Luca Ferrari" + }, + { + "y" : 2, + "name" : "Mark Anderson", + "drilldown" : "Mark Anderson" + }, + { + "y" : 2, + "name" : "Matthew Neleigh", + "drilldown" : "Matthew Neleigh" + }, + { + "drilldown" : "Matthias Muth", + "y" : 3, + "name" : "Matthias Muth" + }, + { + "name" : "Nelo Tovar", + "y" : 2, + "drilldown" : "Nelo Tovar" + }, + { + "drilldown" : "Niels van Dijke", + "y" : 2, + "name" : "Niels van Dijke" + }, + { + "y" : 5, + "name" : "Packy Anderson", + "drilldown" : "Packy Anderson" + }, + { + "drilldown" : "Peter Campbell Smith", + "y" : 3, + "name" : "Peter Campbell Smith" + }, + { + "drilldown" : "Peter Meszaros", + "name" : "Peter Meszaros", + "y" : 2 + }, + { + "drilldown" : "Reinier Maliepaard", + "y" : 3, + "name" : "Reinier Maliepaard" + }, + { + "y" : 3, + "name" : "Robbie Hatley", + "drilldown" : "Robbie Hatley" + }, + { + "drilldown" : "Robert Ransbottom", + "y" : 2, + "name" : "Robert Ransbottom" + }, + { + "drilldown" : "Roger Bell_West", + "y" : 5, + "name" : "Roger Bell_West" + }, + { + "drilldown" : "Santiago Leyva", + "name" : "Santiago Leyva", + "y" : 2 + }, + { + "drilldown" : "Simon Green", + "y" : 2, + "name" : "Simon Green" + }, + { + "drilldown" : "Thomas Kohler", + "name" : "Thomas Kohler", + "y" : 4 + }, + { + "drilldown" : "Ulrich Rieke", + "name" : "Ulrich Rieke", + "y" : 4 + }, + { + "name" : "W. Luis Mochan", + "y" : 3, + "drilldown" : "W. Luis Mochan" + }, + { + "drilldown" : "Wanderdoc", + "y" : 2, + "name" : "Wanderdoc" + } + ], + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 272" + } + ], + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "title" : { + "text" : "The Weekly Challenge - 272" + }, + "chart" : { + "type" : "column" + }, + "drilldown" : { + "series" : [ + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Adam Russell", + "name" : "Adam Russell" + }, + { + "name" : "Ali Moradi", + "id" : "Ali Moradi", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Andrew Schneider", + "id" : "Andrew Schneider", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Arne Sommer", + "id" : "Arne Sommer", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Athanasius", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "id" : "Athanasius" + }, + { + "name" : "BarrOff", + "id" : "BarrOff", + "data" : [ + [ + "Raku", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Bob Lied", + "name" : "Bob Lied" + }, + { + "name" : "Bruce Gray", + "id" : "Bruce Gray", + "data" : [ + [ + "Raku", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Dave Jacoby", + "name" : "Dave Jacoby" + }, + { + "name" : "David Ferrone", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "David Ferrone" + }, + { + "name" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "E. Choroba" + }, + { + "name" : "Feng Chang", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Feng Chang" + }, + { + "id" : "Jaldhar H. Vyas", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Jaldhar H. Vyas" + }, + { + "name" : "Jan Krnavek", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Jan Krnavek" + }, + { + "id" : "Jorg Sommrey", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Jorg Sommrey" + }, + { + "name" : "Laurent Rosenfeld", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 2 + ] + ], + "id" : "Laurent Rosenfeld" + }, + { + "name" : "Luca Ferrari", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 9 + ] + ], + |
