diff options
Diffstat (limited to 'challenge-273')
| -rwxr-xr-x | challenge-273/eric-cheung/python/ch-1.py | 31 | ||||
| -rwxr-xr-x | challenge-273/eric-cheung/python/ch-2.py | 15 | ||||
| -rwxr-xr-x | challenge-273/perlboy1967/perl/ch-1.pl (renamed from challenge-273/perlboy1967/perl/ch1.pl) | 0 | ||||
| -rwxr-xr-x | challenge-273/perlboy1967/perl/ch-2.pl (renamed from challenge-273/perlboy1967/perl/ch2.pl) | 0 | ||||
| -rwxr-xr-x | challenge-273/ulrich-rieke/cpp/ch-1.cpp | 25 | ||||
| -rwxr-xr-x | challenge-273/ulrich-rieke/cpp/ch-2.cpp | 16 | ||||
| -rwxr-xr-x | challenge-273/ulrich-rieke/haskell/ch-1.hs | 18 | ||||
| -rwxr-xr-x | challenge-273/ulrich-rieke/haskell/ch-2.hs | 16 | ||||
| -rwxr-xr-x | challenge-273/ulrich-rieke/perl/ch-1.pl | 20 | ||||
| -rwxr-xr-x | challenge-273/ulrich-rieke/perl/ch-2.pl | 16 | ||||
| -rwxr-xr-x | challenge-273/ulrich-rieke/raku/ch-1.raku | 8 | ||||
| -rwxr-xr-x | challenge-273/ulrich-rieke/raku/ch-2.raku | 12 | ||||
| -rwxr-xr-x | challenge-273/ulrich-rieke/rust/ch-1.rs | 18 | ||||
| -rwxr-xr-x | challenge-273/ulrich-rieke/rust/ch-2.rs | 17 | ||||
| -rwxr-xr-x | challenge-273/wanderdoc/perl/ch-1.pl | 71 | ||||
| -rwxr-xr-x | challenge-273/wanderdoc/perl/ch-2.pl | 47 |
16 files changed, 330 insertions, 0 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 |
