diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-02-03 17:29:40 +0000 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-02-03 17:29:40 +0000 |
| commit | aaab417272f7ae13ade34c68a033d2b1214886d3 (patch) | |
| tree | faa6dbd23c6f83d75adb7d17ae31402afcb1dc8e /challenge-307 | |
| parent | 22771160a2fd5e5893342b5f28ee039872842852 (diff) | |
| download | perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.tar.gz perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.tar.bz2 perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-307')
| -rwxr-xr-x | challenge-307/ulrich-rieke/cpp/ch-1.cpp | 45 | ||||
| -rwxr-xr-x | challenge-307/ulrich-rieke/cpp/ch-2.cpp | 40 | ||||
| -rwxr-xr-x | challenge-307/ulrich-rieke/haskell/ch-1.hs | 16 | ||||
| -rwxr-xr-x | challenge-307/ulrich-rieke/haskell/ch-2.hs | 20 | ||||
| -rwxr-xr-x | challenge-307/ulrich-rieke/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-307/ulrich-rieke/perl/ch-2.pl | 36 | ||||
| -rwxr-xr-x | challenge-307/ulrich-rieke/raku/ch-1.raku | 16 | ||||
| -rwxr-xr-x | challenge-307/ulrich-rieke/raku/ch-2.raku | 25 | ||||
| -rwxr-xr-x | challenge-307/ulrich-rieke/rust/ch-1.rs | 19 | ||||
| -rwxr-xr-x | challenge-307/ulrich-rieke/rust/ch-2.rs | 40 |
10 files changed, 278 insertions, 0 deletions
diff --git a/challenge-307/ulrich-rieke/cpp/ch-1.cpp b/challenge-307/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..df6d096722 --- /dev/null +++ b/challenge-307/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,45 @@ +#include <string>
+#include <vector>
+#include <iostream>
+#include <sstream>
+#include <utility>
+#include <algorithm>
+
+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 some integers separated by whitespace!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stoi( s ) ) ;
+ std::vector<std::pair<int , int>> before_sort , after_sort ;
+ int len = numbers.size( ) ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ before_sort.push_back( std::make_pair( i , numbers[i] ) ) ;
+ }
+ std::sort( numbers.begin( ) , numbers.end( ) ) ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ after_sort.push_back( std::make_pair( i , numbers[i] ) ) ;
+ }
+ std::vector<int> changed_indices ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ if ( (before_sort.begin( ) + i)->second != (after_sort.begin( ) + i)->second )
+ changed_indices.push_back( (before_sort.begin( ) + i)->first ) ;
+ }
+ std::cout << "( " ;
+ for ( int i : changed_indices )
+ std::cout << i << ' ' ;
+ std::cout << ")\n" ;
+ return 0 ;
+}
diff --git a/challenge-307/ulrich-rieke/cpp/ch-2.cpp b/challenge-307/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..8d8a8dac18 --- /dev/null +++ b/challenge-307/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,40 @@ +#include <string>
+#include <iostream>
+#include <vector>
+#include <sstream>
+#include <algorithm>
+
+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 ;
+}
+
+bool areAnagrams( std::string firstWord , std::string secondWord ) {
+ std::sort( firstWord.begin( ) , firstWord.end( ) ) ;
+ std::sort( secondWord.begin( ) , secondWord.end( ) ) ;
+ return (firstWord == secondWord ) ;
+}
+
+int count_anagrams( const std::vector<std::string> & words ) {
+ int len = words.size( ) ;
+ int anagramcount = 0 ;
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ if ( areAnagrams( words[i] , words[i + 1] ) )
+ anagramcount++ ;
+ }
+ return anagramcount ;
+}
+
+int main( ) {
+ std::cout << "Enter some words separated by whitespace!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::cout << ( tokens.size( ) - count_anagrams( tokens ) ) << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-307/ulrich-rieke/haskell/ch-1.hs b/challenge-307/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..e3aee66ff9 --- /dev/null +++ b/challenge-307/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,16 @@ +module Challenge307
+ where
+import Data.List ( sort )
+
+solution :: [Int] -> [Int]
+solution list =
+ let sorted = sort list
+ compared = zip list sorted
+ indexed = zip [0, 1 ..] compared
+ in map fst $ filter (\p -> (fst $ snd p) /= (snd $ snd p)) indexed
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some integers separated by whitespace!"
+ numberline <- getLine
+ print $ solution $ map read $ words numberline
diff --git a/challenge-307/ulrich-rieke/haskell/ch-2.hs b/challenge-307/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..faa82e4637 --- /dev/null +++ b/challenge-307/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,20 @@ +module Challenge307_2
+ where
+import Data.List( sort )
+import Data.List.Split( divvy )
+
+areAnagrams :: String -> String -> Bool
+areAnagrams str1 str2 = sort str1 == sort str2
+
+solution :: [String] -> Int
+solution input =
+ let neighbours = divvy 2 1 input
+ anacount = length $ filter (\subli -> areAnagrams (head subli) (last subli) )
+ neighbours
+ in length input - anacount
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some words separated by whitespace!"
+ input <- getLine
+ print $ solution $ words input
diff --git a/challenge-307/ulrich-rieke/perl/ch-1.pl b/challenge-307/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..f059f8fc9c --- /dev/null +++ b/challenge-307/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( zip ) ;
+
+say "Enter some integers separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+my $len = scalar( @numbers ) ;
+my @before_pairs = zip [0..$len - 1] , [@numbers] ;
+my @sorted = sort { $a <=> $b } @numbers ;
+my @after_pairs = zip [0..$len - 1] , [@sorted] ;
+my @changed_indices ;
+for my $pos (0..$len - 1) {
+ if ( $before_pairs[$pos]->[1] != $after_pairs[$pos]->[1] ) {
+ push( @changed_indices , $pos ) ;
+ }
+}
+say '(' . join( ',' , @changed_indices ) . ')' ;
diff --git a/challenge-307/ulrich-rieke/perl/ch-2.pl b/challenge-307/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..f02c3415e7 --- /dev/null +++ b/challenge-307/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub findAnagramNumber {
+ my $words = shift ;
+ my $anagramCount = 0 ;
+ my $len = scalar( @$words ) ;
+ for my $pos( 0..$len - 2 ) {
+ if ( areAnagrams( $words->[$pos] , $words->[$pos + 1] ) ) {
+ $anagramCount++ ;
+ }
+ }
+ return $anagramCount ;
+}
+
+sub areAnagrams {
+ my $first = shift ;
+ my $second = shift ;
+ my $firstOrdered = join( '' , sort ( split (// , $first )) ) ;
+ my $secondOrdered = join( '' , sort ( split (// , $second ))) ;
+ if ( $firstOrdered eq $secondOrdered ) {
+ return 1 ;
+ }
+ else {
+ return 0 ;
+ }
+}
+
+say "Enter some words separated by whitespace!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @words = split( /\s/ , $line ) ;
+my $ananumber = findAnagramNumber( \@words ) ;
+say ( scalar( @words ) - $ananumber ) ;
diff --git a/challenge-307/ulrich-rieke/raku/ch-1.raku b/challenge-307/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..a2bed3ee17 --- /dev/null +++ b/challenge-307/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,16 @@ +use v6 ;
+
+say "Enter some integers separated by whitespace!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+my $len = @numbers.elems ;
+my @before_pairs = ((0..$len - 1) Z, @numbers) ;
+my @sorted = @numbers.sort( {$^a <=> $^b} ) ;
+my @after_pairs = ((0..$len - 1) Z, @sorted ) ;
+my @changed_indices ;
+for (0..$len - 1) -> $pos {
+ if ( @before_pairs[$pos][1] != @after_pairs[$pos][1] ) {
+ @changed_indices.push( $pos ) ;
+ }
+}
+say '(' ~ @changed_indices.join( ',' ) ~ ')' ;
diff --git a/challenge-307/ulrich-rieke/raku/ch-2.raku b/challenge-307/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..58a72e7afc --- /dev/null +++ b/challenge-307/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,25 @@ +use v6 ;
+
+sub find_anagrams( @array ) {
+ my $len = @array.elems ;
+ my $anagramcount = 0 ;
+ for (0..$len - 2) -> $pos {
+ if ( areAnagrams( @array[$pos] , @array[$pos + 1] )) {
+ $anagramcount++ ;
+ }
+ }
+ return $anagramcount ;
+}
+
+sub areAnagrams( $word1 , $word2 ) {
+ my @firstLetters = $word1.comb.sort ;
+ my @secondLetters = $word2.comb.sort ;
+ return ( @firstLetters == @secondLetters ) ;
+}
+
+say "Enter some words separated by whitespace!" ;
+my $line = $*IN.get ;
+my @words = $line.words ;
+my $anacount = find_anagrams( @words ) ;
+say ( @words.elems - $anacount ) ;
+
diff --git a/challenge-307/ulrich-rieke/rust/ch-1.rs b/challenge-307/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..832e1f3476 --- /dev/null +++ b/challenge-307/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,19 @@ +use std::io ; + +fn main() { + println!("Enter some integers separated by whitespace!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let numbers : Vec<i32> = inline.trim( ).split_whitespace( ).map( |s| + s.parse::<i32>( ).unwrap( ) ).collect( ) ; + let original_positions : Vec<(usize, &i32)> = numbers.iter( ).enumerate( ). + collect( ) ; + let mut sorted : Vec<i32> = numbers.clone( ) ; + sorted.sort( ) ; + let new_positions : Vec<(usize, i32)> = sorted.into_iter( ).enumerate( ). + collect( ) ; + let mut result : Vec<usize> = Vec::new( ) ; + original_positions.into_iter().zip( new_positions.into_iter( )).filter( + |&(a , b)| *a.1 != b.1 ).for_each( |p| result.push( p.0.0 )) ; + println!("{:?}" , result) ; +} diff --git a/challenge-307/ulrich-rieke/rust/ch-2.rs b/challenge-307/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..4cb571dd3e --- /dev/null +++ b/challenge-307/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,40 @@ +use std::io ; + +//find the indices of those elements that can be deleted , if any +fn find_indices( words : &Vec<&str> ) -> Vec<usize> { + let enumerated : Vec<(usize, &&str)> = words.iter( ).enumerate( ). + collect( ) ; + let enu_slice = &enumerated[..] ; + let mut iter = enu_slice.windows( 2 ) ; + let mut positions : Vec<usize> = Vec::new( ) ; + while let Some( wi ) = iter.next( ) { + if are_anagrams( wi[0].1 , wi[1].1 ) { + positions.push( wi[0].0 ) ; + } + } + positions +} + +fn are_anagrams( word1 : &str , word2 : &str ) -> bool { + let mut first_letters : Vec<char> = Vec::new( ) ; + let mut second_letters : Vec<char> = Vec::new( ) ; + for c in word1.chars( ) { + first_letters.push( c ) ; + } + for c in word2.chars( ) { + second_letters.push( c ) ; + } + first_letters.sort( ) ; + second_letters.sort( ) ; + first_letters == second_letters +} + +fn main() { + println!("Enter some words separated by whitespace!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let words : Vec<&str> = inline.trim( ).split_whitespace( ). + collect( ) ; + let positions : Vec<usize> = find_indices( &words ) ; + println!("{}" , words.len( ) - positions.len( )) ; +} |
