diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-07-15 11:10:21 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-07-15 11:10:21 +0100 |
| commit | 5cccfc981512e14a5bcee9964ff8391f2e4da93b (patch) | |
| tree | 0605df384edd3578c897d8d2b2839339aef0dfa6 /challenge-330 | |
| parent | 3e616005e0d5db730ff024ee15e9e66049bb3ed4 (diff) | |
| download | perlweeklychallenge-club-5cccfc981512e14a5bcee9964ff8391f2e4da93b.tar.gz perlweeklychallenge-club-5cccfc981512e14a5bcee9964ff8391f2e4da93b.tar.bz2 perlweeklychallenge-club-5cccfc981512e14a5bcee9964ff8391f2e4da93b.zip | |
- Added solutions by Mark Anderson.
- Added solutions by Feng Chang.
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke.
- Added solutions by Benjamin Andre.
- Added solutions by Ali Moradi.
- Added solutions by Andrew Shitov.
- Added solutions by E. Choroba.
- Added solutions by Lukas Mai.
- Added solutions by Simon Proctor.
- Added solutions by Adam Russell.
- Added solutions by David Ferrone.
- Added solutions by Luca Ferrari.
- Added solutions by Peter Campbell Smith.
- Added solutions by W. Luis Mochan.
- Added solutions by Conor Hoekstra.
- Added solutions by Thomas Kohler.
- Added solutions by Jaldhar H. Vyas.
- Added solutions by Roger Bell_West.
Diffstat (limited to 'challenge-330')
| -rwxr-xr-x | challenge-330/eric-cheung/python/ch-1.py | 16 | ||||
| -rwxr-xr-x | challenge-330/eric-cheung/python/ch-2.py | 22 | ||||
| -rwxr-xr-x | challenge-330/ulrich-rieke/cpp/ch-1.cpp | 23 | ||||
| -rwxr-xr-x | challenge-330/ulrich-rieke/cpp/ch-2.cpp | 46 | ||||
| -rwxr-xr-x | challenge-330/ulrich-rieke/haskell/ch-1.hs | 25 | ||||
| -rwxr-xr-x | challenge-330/ulrich-rieke/haskell/ch-2.hs | 17 | ||||
| -rwxr-xr-x | challenge-330/ulrich-rieke/perl/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-330/ulrich-rieke/perl/ch-2.pl | 20 | ||||
| -rwxr-xr-x | challenge-330/ulrich-rieke/raku/ch-1.raku | 14 | ||||
| -rwxr-xr-x | challenge-330/ulrich-rieke/raku/ch-2.raku | 15 | ||||
| -rwxr-xr-x | challenge-330/ulrich-rieke/rust/ch-1.rs | 26 | ||||
| -rwxr-xr-x | challenge-330/ulrich-rieke/rust/ch-2.rs | 37 |
12 files changed, 278 insertions, 0 deletions
diff --git a/challenge-330/eric-cheung/python/ch-1.py b/challenge-330/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..7cb84e86b6 --- /dev/null +++ b/challenge-330/eric-cheung/python/ch-1.py @@ -0,0 +1,16 @@ +
+## strInput = "cab12" ## Example 1
+## strInput = "xy99" ## Example 2
+strInput = "pa1erl" ## Example 3
+
+arrInput = list(strInput)
+
+while len([charLoop for charLoop in arrInput if charLoop.isdigit()]) > 0:
+ nFirstNumDigit = [nIndx for nIndx, charLoop in enumerate(arrInput) if charLoop.isdigit()][0]
+ if nFirstNumDigit == 0:
+ break
+
+ del arrInput[nFirstNumDigit]
+ del arrInput[nFirstNumDigit - 1]
+
+print ("".join(arrInput))
diff --git a/challenge-330/eric-cheung/python/ch-2.py b/challenge-330/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..1ccef2a8d9 --- /dev/null +++ b/challenge-330/eric-cheung/python/ch-2.py @@ -0,0 +1,22 @@ +
+## strInput = "PERL IS gREAT" ## Example 1
+## strInput = "THE weekly challenge" ## Example 2
+strInput = "YoU ARE A stAR" ## Example 3
+
+## ==== METHOD 1 ====
+## arrOutput = []
+
+## for strLoop in strInput.split(" "):
+ ## if len(strLoop) < 3:
+ ## arrOutput.append(strLoop.lower())
+ ## else:
+ ## ## arrOutput.append(strLoop[0].upper() + strLoop[1:].lower())
+ ## arrOutput.append(strLoop.title())
+## ==== METHOD 1 ====
+
+## ==== METHOD 2 ====
+## arrOutput = [strLoop.lower() if len(strLoop) < 3 else strLoop[0].upper() + strLoop[1:].lower() for strLoop in strInput.split(" ")]
+arrOutput = [strLoop.lower() if len(strLoop) < 3 else strLoop.title() for strLoop in strInput.split(" ")]
+## ==== METHOD 2 ====
+
+print (" ".join(arrOutput))
diff --git a/challenge-330/ulrich-rieke/cpp/ch-1.cpp b/challenge-330/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..2a9d38ddba --- /dev/null +++ b/challenge-330/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,23 @@ +#include <string>
+#include <iostream>
+#include <regex>
+using namespace std::string_literals ;
+
+int main( ) {
+ std::cout << "Enter a string with lowercase English letters and digits only!\n" ;
+ std::string word ;
+ std::cin >> word ;
+ std::regex wanted { "\\D\\d" } ;
+ while ( std::regex_search( word , wanted )) {
+ word = std::regex_replace( word , wanted , "" ,
+ std::regex_constants::format_first_only) ;
+ }
+ if ( word.length( ) > 0 ) {
+ std::cout << word ;
+ }
+ else {
+ std::cout << "\"\"" ;
+ }
+ std::cout << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-330/ulrich-rieke/cpp/ch-2.cpp b/challenge-330/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..615f6f45cb --- /dev/null +++ b/challenge-330/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,46 @@ +#include <string>
+#include <vector>
+#include <iostream>
+#include <sstream>
+#include <cctype>
+
+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 ;
+}
+
+int main( ) {
+ std::cout << "Enter a string with some words separated by space!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::string result ;
+ for ( auto s : tokens ) {
+ if ( s.length( ) < 3 ) {
+ for ( auto c : s ) {
+ result.push_back( std::tolower( c ) ) ;
+ }
+ }
+ else {
+ for ( auto c : s ) {
+ if ( c == s[0] ) {
+ result.push_back( std::toupper( c ) ) ;
+ }
+ else {
+ result.push_back( std::tolower( c ) ) ;
+ }
+ }
+ }
+ result.push_back( ' ' ) ;
+ }
+ result.pop_back( ) ;
+ std::cout << result << '\n' ;
+ return 0 ;
+}
+
+
+
diff --git a/challenge-330/ulrich-rieke/haskell/ch-1.hs b/challenge-330/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..907d3d0f4c --- /dev/null +++ b/challenge-330/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,25 @@ +module Challenge330
+ where
+import Data.List.Split ( divvy )
+import Data.Char ( isLetter , isDigit )
+import Data.List ( findIndex )
+
+removePair :: String -> Int -> String
+removePair str at = take at str ++ drop ( at + 2 ) str
+
+condition :: String -> Bool
+condition str = (isLetter $ head str) && ( isDigit $ last str )
+
+solution :: String -> String
+solution str = until (\s -> not $ any condition $ divvy 2 1 s) step str
+ where
+ step :: String -> String
+ step someString = case findIndex condition $ divvy 2 1 someString of
+ Just pos -> removePair someString pos
+ Nothing -> someString
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a word with lowercase English letters and digits only!"
+ word <- getLine
+ print $ solution word
diff --git a/challenge-330/ulrich-rieke/haskell/ch-2.hs b/challenge-330/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..307d434570 --- /dev/null +++ b/challenge-330/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,17 @@ +module Challenge330_2
+ where
+import Data.Char ( toUpper , toLower )
+import Data.List ( tail )
+
+convert :: String -> String
+convert str = if length str < 3 then map toLower str else [toUpper $ head
+ str] ++ ( map toLower $ tail str )
+
+solution :: String -> String
+solution = unwords . map convert . words
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a sentence with words separated by space!"
+ line <- getLine
+ print $ solution line
diff --git a/challenge-330/ulrich-rieke/perl/ch-1.pl b/challenge-330/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..556dfdfcbf --- /dev/null +++ b/challenge-330/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter a word with lowercase English letters and digits only!" ;
+my $word = <STDIN> ;
+chomp $word ;
+while ( $word =~ /(\D\d)/ ) {
+ $word =~ s/$1// ;
+}
+if ( $word ) {
+ say $word ;
+}
+else {
+ say "\"\"" ;
+}
diff --git a/challenge-330/ulrich-rieke/perl/ch-2.pl b/challenge-330/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..d5819eff26 --- /dev/null +++ b/challenge-330/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter a string with several words separated by spaces!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @words = split( /\s/ , $line ) ;
+my @result ;
+for my $w ( @words ) {
+ if ( length( $w ) < 3 ) {
+ push( @result , lc( $w ) ) ;
+ }
+ else {
+ my $word = uc( substr( $w , 0 , 1 )) . lc( substr( $w , 1 ) ) ;
+ push( @result , $word ) ;
+ }
+}
+say join( ' ' , @result) ;
diff --git a/challenge-330/ulrich-rieke/raku/ch-1.raku b/challenge-330/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..6f8854f693 --- /dev/null +++ b/challenge-330/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,14 @@ +use v6 ;
+
+say "Enter a string consisting of lowercase English letters and digits!" ;
+my $word = $*IN.get ;
+while ( $word ~~ /(\D\d)/ ) {
+ my $part = ~$0 ;
+ $word ~~ s/$part// ;
+}
+if ( $word ) {
+ say $word ;
+}
+else {
+ say "\"\"" ;
+}
diff --git a/challenge-330/ulrich-rieke/raku/ch-2.raku b/challenge-330/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..17f722bcdb --- /dev/null +++ b/challenge-330/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,15 @@ +use v6 ;
+
+say "Enter a string with several words separated by space!" ;
+my $line = $*IN.get ;
+my @words = $line.words ;
+my @result ;
+for @words -> $w {
+ if ( $w.chars < 3 ) {
+ @result.push( $w.lc ) ;
+ }
+ else {
+ @result.push( $w.tclc ) ;
+ }
+}
+say @result.join( ' ' ) ;
diff --git a/challenge-330/ulrich-rieke/rust/ch-1.rs b/challenge-330/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..42077f69b4 --- /dev/null +++ b/challenge-330/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,26 @@ +use std::io ; + +fn main() { + println!("Enter a string consisting of lowercase English letters and digits!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let input : &str = inline.trim( ) ; + let mut in_chars : Vec<char> = Vec::new( ) ; + for c in input.chars( ) { + in_chars.push( c ) ; + } + while let Some( pos ) = in_chars.iter( ).position( |c| c.is_digit( 10 ) ) { + if pos > 0 { + in_chars.remove( pos - 1 ) ; + in_chars.remove( pos - 1 ) ; + } + else { + in_chars.remove( 0 ) ; + } + } + let mut result : String = String::new( ) ; + for c in in_chars { + result.push( c ) ; + } + println!("{:?}" , result ) ; +} diff --git a/challenge-330/ulrich-rieke/rust/ch-2.rs b/challenge-330/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..a51dde0ee6 --- /dev/null +++ b/challenge-330/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,37 @@ +use std::io ; + +fn convert( sentence : &str ) -> String { + let words : Vec<&str> = sentence.split_whitespace( ).collect( ) ; + let mut result : String = String::new( ) ; + for w in words { + if w.chars( ).count( ) < 3 { + for c in w.chars( ) { + if c.is_ascii_uppercase( ) { + result.push( c.to_ascii_lowercase( ) ) ; + } + } + } + else { + for c in w.chars( ) { + if c == w.chars( ).nth(0).unwrap( ) { + result.push( c.to_ascii_uppercase( ) ) ; + } + else { + result.push( c.to_ascii_lowercase( ) ) ; + } + } + } + result.push( ' ' ) ; + } + result.pop( ) ; + result +} + + +fn main() { + println!("Enter a string with several words separated by space!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let input : &str = inline.trim( ) ; + println!("{:?}" , convert( input ) ) ; +} |
