diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-06-25 08:40:44 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-06-25 08:40:44 +0100 |
| commit | 74faf8bd5941620c2902102e9f3be28e2c8884c2 (patch) | |
| tree | c0189c58ec02fae7b674ed9619a0919b5ae58f47 | |
| parent | c9a8225f62e1f6159c3180fcefd4ae0cec189740 (diff) | |
| download | perlweeklychallenge-club-74faf8bd5941620c2902102e9f3be28e2c8884c2.tar.gz perlweeklychallenge-club-74faf8bd5941620c2902102e9f3be28e2c8884c2.tar.bz2 perlweeklychallenge-club-74faf8bd5941620c2902102e9f3be28e2c8884c2.zip | |
- Added solutions by Simon Proctor.
- Added solutions by Niels van Dijke.
- Added solutions by Dave Jacoby.
- Added solutions by David Ferrone.
- Added solutions by Ulrich Rieke.
- Added solutions by Robert DiCicco.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Colin Crain.
- Added solutions by Thomas Kohler.
- Added solutions by W. Luis Mochan.
34 files changed, 858 insertions, 0 deletions
diff --git a/challenge-222/colin-crain/blog.txt b/challenge-222/colin-crain/blog.txt new file mode 100644 index 0000000000..c528c13755 --- /dev/null +++ b/challenge-222/colin-crain/blog.txt @@ -0,0 +1 @@ +https://colincrain.com/2023/06/22/last-number-standing/ diff --git a/challenge-222/colin-crain/perl/ch-2.pl b/challenge-222/colin-crain/perl/ch-2.pl new file mode 100755 index 0000000000..4946495fc2 --- /dev/null +++ b/challenge-222/colin-crain/perl/ch-2.pl @@ -0,0 +1,113 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# 222-2-last-element-standing.pl
+#
+# Last Member
+#
+# Submitted by: Mohammad S Anwar
+# You are given an array of positive integers, @ints.
+#
+# Write a script to find the last member if found otherwise return
+# 0. Each turn pick 2 biggest members (x, y) then decide based on
+# the following conditions, continue this until you are left with 1
+# member or none.
+#
+# a) if x == y then remove both members
+#
+# b) if x != y then remove both members and add new member (y-x)
+#
+#
+# Example 1:
+#
+# Input: @ints = (2, 7, 4, 1, 8, 1)
+# Output: 1
+#
+# Step 1: pick 7 and 8, we remove both and add new member 1 => (2, 4, 1, 1, 1).
+# Step 2: pick 2 and 4, we remove both and add new member 2 => (2, 1, 1, 1).
+# Step 3: pick 2 and 1, we remove both and add new member 1 => (1, 1, 1).
+# Step 4: pick 1 and 1, we remove both => (1).
+#
+# Example 2:
+#
+# Input: @ints = (1)
+# Output: 1
+#
+# Example 3:
+#
+# Input: @ints = (1, 1)
+# Output: 0
+#
+# Step 1: pick 1 and 1, we remove both and we left with none.
+
+# Method:
+#
+# There is, as has often been noted, more than one way to do it.
+# This is good general advice in life, and Perl takes the
+# expression as it's credo. The algortithm as described could be
+# reduced to the steps of sorting, selecting, processing and
+# repeating, with a check at every pass as to whether we have less
+# than two elements to compare.
+#
+# That seems like it should work quite nicely. You do, however,
+# have to sort the array over and over as you insert the reduced
+# elements back in, which sounds kind of boring and inefficient. If
+# we were in a hurry we would refrain from the cardinal sin of
+# premature optimization, but in this sandbox we suffer no such
+# constraints. What if we could reinsert the difference at the
+# right place in the array when we compute it, obviating any need
+# to resort?
+#
+# The obvious tradeoff is sorting the whole array versus looking
+# for the correct insertion point for the new differential element.
+# But here's the thing: sorting the array, no matter the algorithm
+# we use to go about it, will always require us to handle every
+# element. However if we examine the array elements from smallest
+# upward, we can stop when the next element is larger and insert
+# there. Seems like that will shed some unnecessary effort.
+#
+# Let's do that.
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+use constant { VERBOSE => 1};
+
+my @arr = @ARGV;
+@arr == 0 and @arr = (1,1,1,15,7);
+@arr = sort {$a<=>$b} @arr;
+VERBOSE && say "@arr";
+
+## let the games begin!
+while (@arr > 1) {
+
+ ## remove the last two elements and test their difference
+ ## scan the remaining array and reinsert at the point
+ ## where the next element is larger or the array ends
+ my ($sec, $last) = splice @arr, -2;
+ if (my $diff = $last - $sec) {
+ my $c = 0;
+# $c++ while (exists $arr[$c] and $arr[$c] < $diff);
+ ++$c while (@arr, $diff)[$c] < $diff;
+
+ splice @arr, $c, 0, $diff;
+ VERBOSE && say "@arr";
+ }
+}
+
+say( @arr ? $arr[0]
+ : 0
+);
+
+
+
+
+
diff --git a/challenge-222/colin-crain/raku/ch-2.raku b/challenge-222/colin-crain/raku/ch-2.raku new file mode 100755 index 0000000000..a5173018b1 --- /dev/null +++ b/challenge-222/colin-crain/raku/ch-2.raku @@ -0,0 +1,30 @@ +#!/usr/bin/env perl6 +# +# +# 222-2-last-element-standing.raku +# +# +# +# © 2022 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + +unit sub MAIN ( *@arr ) ; + +my $VERBOSE = 1; ## show progress of array reduction? +@arr .= sort; + +while @arr.elems > 1 { + $VERBOSE && say @arr; + $_ = [-] (@arr.splice(*-2).reverse) || next; + @arr.splice( ((|@arr, $_).first: * >= $_, :k), 0, $_ ); +} + +say @arr.defined ?? @arr[0] !! 0; + + + + + + + diff --git a/challenge-222/eric-cheung/python/ch-1.py b/challenge-222/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..79e016c41f --- /dev/null +++ b/challenge-222/eric-cheung/python/ch-1.py @@ -0,0 +1,13 @@ +
+## arrInput = [1, 1, 4, 2, 1, 3] ## Example 1
+## arrInput = [5, 1, 2, 3, 4] ## Example 2
+arrInput = [1, 2, 3, 4, 5] ## Example 3
+
+arrSort = sorted(arrInput)
+nCount = 0
+
+for nLoop in range(0, len(arrInput)):
+ if arrInput[nLoop] == arrSort[nLoop]:
+ nCount = nCount + 1
+
+print (nCount)
diff --git a/challenge-222/eric-cheung/python/ch-2.py b/challenge-222/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..8caac577c5 --- /dev/null +++ b/challenge-222/eric-cheung/python/ch-2.py @@ -0,0 +1,22 @@ +
+## arrInput = [2, 7, 4, 1, 8, 1] ## Example 1
+## arrInput = [1] ## Example 2
+arrInput = [1, 1] ## Example 3
+
+arrSort = sorted(arrInput, reverse = True)
+
+if len(arrSort) == 1:
+ print (arrSort[0])
+else:
+ while len(arrSort) > 1:
+ nElem_01 = arrSort[0]
+ nElem_02 = arrSort[1]
+ arrSort.pop(1)
+ arrSort.pop(0)
+ if nElem_01 != nElem_02:
+ arrSort.append(abs(nElem_01 - nElem_02))
+ arrSort = sorted(arrSort, reverse = True)
+ if len(arrSort) == 1:
+ print (arrSort[0])
+ else:
+ print (0)
diff --git a/challenge-222/jeanluc2020/blog-1.txt b/challenge-222/jeanluc2020/blog.txt index 4cf3cdf8fd..4cf3cdf8fd 100644 --- a/challenge-222/jeanluc2020/blog-1.txt +++ b/challenge-222/jeanluc2020/blog.txt diff --git a/challenge-222/jeanluc2020/blog-2.txt b/challenge-222/jeanluc2020/blog1.txt index 16f4c2793a..16f4c2793a 100644 --- a/challenge-222/jeanluc2020/blog-2.txt +++ b/challenge-222/jeanluc2020/blog1.txt diff --git a/challenge-222/laurent-rosenfeld/blog.txt b/challenge-222/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..b006ab6e19 --- /dev/null +++ b/challenge-222/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/06/perl-weekly-challenge-222-matching-members-and-last-member.html diff --git a/challenge-222/laurent-rosenfeld/perl/ch-1.pl b/challenge-222/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..89fe1b97b0 --- /dev/null +++ b/challenge-222/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,17 @@ +use strict; +use warnings; +use feature 'say'; + +sub matching_members { + my @sorted = sort { $a <=> $b } @_; + my $match = 0; + for my $i (0..$#sorted) { + $match++ if $_[$i] == $sorted[$i]; + } + return $match; +} + +for my $test ([<1 1 4 2 1 3>], [<5 1 2 3 4>], [<1 2 3 4 5>]) { + printf "%-12s => ", "@$test"; + say matching_members @$test; +} diff --git a/challenge-222/laurent-rosenfeld/perl/ch-2.pl b/challenge-222/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..5cbe919217 --- /dev/null +++ b/challenge-222/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use feature 'say'; + +sub last_member { + my @in = sort { $b <=> $a } @_; + while (@in > 1) { + @in = $in[0] == $in[1] ? + sort { $b <=> $a } @in[2..$#in] : + sort { $b <=> $a } ($in[1] - $in[0], @in[2..$#in]); + # say "@in"; # uncomment to view the steps + } + return scalar @in; +} + +for my $test ([2, 7, 4, 1, 8, 1], [1], [1, 1]) { + printf "%-12s => ", "@$test"; + say last_member @$test; +} diff --git a/challenge-222/laurent-rosenfeld/raku/ch-1.raku b/challenge-222/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..e890cab03a --- /dev/null +++ b/challenge-222/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,11 @@ +sub matching-members (@in) { + my @sorted = @in.sort; + my $match = 0; + $match++ if @in[$_] == @sorted[$_] for 0..@in.end; + return $match; +} + +for <1 1 4 2 1 3>, <5 1 2 3 4>, <1 2 3 4 5> -> @test { + printf "%-12s => ", "@test[]"; + say matching-members @test; +} diff --git a/challenge-222/laurent-rosenfeld/raku/ch-2.raku b/challenge-222/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..8450e5652a --- /dev/null +++ b/challenge-222/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,13 @@ +sub last-member (@ints) { + my @in = reverse sort @ints; + while @in.elems > 1 { + @in = @in[0] == @in[1] ?? @in[2..@in.end].sort.reverse !! + (@in[1] - @in[0], @in[2..@in.end]).flat.sort.reverse; + } + return @in.elems; +} + +for (2, 7, 4, 1, 8, 1), (1,), (1, 1) -> @test { + printf "%-12s => ", "@test[]"; + say last-member @test; +} diff --git a/challenge-222/perlboy1967/perl/ch1.pl b/challenge-222/perlboy1967/perl/ch-1.pl index a7908c0a23..a7908c0a23 100755 --- a/challenge-222/perlboy1967/perl/ch1.pl +++ b/challenge-222/perlboy1967/perl/ch-1.pl diff --git a/challenge-222/perlboy1967/perl/ch2.pl b/challenge-222/perlboy1967/perl/ch-2.pl index 462a39d589..462a39d589 100755 --- a/challenge-222/perlboy1967/perl/ch2.pl +++ b/challenge-222/perlboy1967/perl/ch-2.pl diff --git a/challenge-222/perlboy1967/perl/ch2a.pl b/challenge-222/perlboy1967/perl/ch-2a.pl index 1ec5e9308b..1ec5e9308b 100755 --- a/challenge-222/perlboy1967/perl/ch2a.pl +++ b/challenge-222/perlboy1967/perl/ch-2a.pl diff --git a/challenge-222/robert-dicicco/julia/ch-1.jl b/challenge-222/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..f11abecc22 --- /dev/null +++ b/challenge-222/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,43 @@ +#!/usr/bin/env julia +#= +----------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-06-19 +Challenge 222 Task 1 Matching Members ( Julia ) +----------------------------------------------- +=# +using Printf + +allints = [[1, 1, 4, 2, 1, 3],[5, 1, 2, 3, 4],[1, 2, 3, 4, 5]] + +for ints in allints + @printf("Input: @ints = %s\n",ints) + flag = 0 + sorted = sort(ints) + cnt = 1 + while cnt <= length(ints) + if ints[cnt] == sorted[cnt] + flag += 1 + end + cnt += 1 + end + @printf("Output: %d\n\n", flag) +end + +#= +----------------------------------------------- +SAMPLE OUTPUT +julia .\MatchingMembers.jl + +Input: @ints = [1, 1, 4, 2, 1, 3] +Output: 3 + +Input: @ints = [5, 1, 2, 3, 4] +Output: 0 + +Input: @ints = [1, 2, 3, 4, 5] +Output: 5 +----------------------------------------------- +=# + + diff --git a/challenge-222/robert-dicicco/perl/ch-1.pl b/challenge-222/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..f9e24752bd --- /dev/null +++ b/challenge-222/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl +=begin comment +----------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-06-19 +Challenge 222 Task 1 Matching Members ( Perl ) +----------------------------------------------- +=cut +use strict; +use warnings; +use feature 'say'; + +my @allints = ([1, 1, 4, 2, 1, 3],[5, 1, 2, 3, 4],[1, 2, 3, 4, 5]); + +for my $ints (@allints) { + my $flag = 0; + my @original = @$ints; + say "Input: \@ints = \(",join(",",@original),"\)"; + my @sorted = sort(@original); + my $cnt = scalar (@original); + for (my $digit=0; $digit < $cnt; $digit++){ + if ($original[$digit] == $sorted[$digit]) { + $flag++; + } + } + say "Output: $flag\n"; +} + +=begin comment +----------------------------------------------- +SAMPLE OUTPUT +perl .\MatchingMembers.pl + +Input: @ints = (1,1,4,2,1,3) +Output: 3 + +Input: @ints = (5,1,2,3,4) +Output: 0 + +Input: @ints = (1,2,3,4,5) +Output: 5 +----------------------------------------------- +=cut + + diff --git a/challenge-222/robert-dicicco/perl/ch-2.pl b/challenge-222/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..aae25688f8 --- /dev/null +++ b/challenge-222/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +=begin comment +----------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-06-22 +Challenge 222 Task 2 Last Member ( Perl ) +----------------------------------------------- +=cut +use strict; +use warnings; +use feature 'say'; +use List::MoreUtils qw(first_index); + +my @allints = @ARGV[0..$#ARGV]; + +say "Input: \@ints = @allints"; +while (1) { + my @srt = sort(@allints); + my $ln = scalar @srt; + if ($ln <= 1) { + say "Output: $ln"; + exit; + } else { + my $lrg = $srt[$ln - 1]; + my $nxt = $srt[$ln - 2]; + my $lrg_index = first_index { $_ eq $lrg } @allints; + my $nxt_index = first_index { $_ eq $nxt } @allints; + splice(@allints, $lrg_index, 1); + splice(@allints, $nxt_index, 1); + if ($lrg > $nxt){ + unshift(@allints, $lrg - $nxt); + } + } +} + +=begin comment +----------------------------------------------- +SAMPLE OUTPUT +perl .\LastMember.pl 2 7 4 1 8 1 +Input: @ints = 2 7 4 1 8 1 +Output: 1 + +PS G:\Projects\Perl\Challenges> perl .\LastMember.pl 1 +Input: @ints = 1 +Output: 1 + +PS G:\Projects\Perl\Challenges> perl .\LastMember.pl 1 1 +Input: @ints = 1 1 +Output: 0 +----------------------------------------------- +=cut diff --git a/challenge-222/robert-dicicco/python/ch-1.py b/challenge-222/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..f9863aacc5 --- /dev/null +++ b/challenge-222/robert-dicicco/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# ----------------------------------------------- +# AUTHOR: Robert DiCicco +# DATE : 2023-06-19 +# Challenge 222 Task 1 Matching Members ( Python ) +# ----------------------------------------------- +allints = [[1, 1, 4, 2, 1, 3],[5, 1, 2, 3, 4],[1, 2, 3, 4, 5]] + +for ints in allints: + print("Input: @ints = ",ints) + flag = 0 + sortd = sorted(ints) + cnt = 0 + while cnt < len(ints): + if ints[cnt] == sortd[cnt]: + flag += 1 + cnt += 1 + print("Output: ",flag,"\n") + + # ----------------------------------------------- + # SAMPLE OUTPUT + # python .\MatchingMembers.py + +# Input: @ints = [1, 1, 4, 2, 1, 3] +# Output: 3 + +# Input: @ints = [5, 1, 2, 3, 4] +# Output: 0 + +# Input: @ints = [1, 2, 3, 4, 5] +# Output: 5 + # ----------------------------------------------- + + diff --git a/challenge-222/robert-dicicco/raku/ch-2.raku b/challenge-222/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..d74f4750fa --- /dev/null +++ b/challenge-222/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,52 @@ +#!/usr/bin/env raku +=begin comment +----------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-06-22 +Challenge 222 Task 2 Last Member ( Raku ) +----------------------------------------------- +=end comment + +unit sub MAIN (*@allints where @allints.elems > 0) { + say "Input: \@ints = ",@allints; + loop { + my @srt = @allints.sort; + my $ln = @allints.elems - 1; + if $ln <= 1 { + say "Output: $ln\n"; + exit; + } else { + my $lrg = @srt[$ln]; + my $nxt = @srt[$ln-1]; + + my $index = @allints.first($lrg, :k); + @allints.splice($index,1); + + $index = @allints.first($nxt, :k); + @allints.splice($index,1); + + if ($lrg > $nxt) { + @allints.prepend($lrg - $nxt); + } + } + } +} + +=begin comment +----------------------------------------------- +SAMPLE OUTPUT +raku .\LastMember.rk 2 7 4 1 8 +Input: @ints = [2 7 4 1 8] +Output: 1 + +raku .\LastMember.rk 1 1 +Input: @ints = [1 1] +Output: 1 + +raku .\LastMember.rk 1 +Input: @ints = [1] +Output: 0 +----------------------------------------------- +=end comment + + diff --git a/challenge-222/robert-dicicco/ruby/ch-1.rb b/challenge-222/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..fbc26ac222 --- /dev/null +++ b/challenge-222/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,43 @@ +#!/usr/bin/env ruby +=begin +----------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-06-19 +Challenge 222 Task 1 Matching Members ( Ruby ) +----------------------------------------------- +=end + +allints = [[1, 1, 4, 2, 1, 3],[5, 1, 2, 3, 4],[1, 2, 3, 4, 5]]; + + +for ints in allints + puts("Input: @ints = #{ints}") + flag = 0 + sorted = ints.sort + cnt = 0 + while cnt < ints.length + if ints[cnt] == sorted[cnt] + flag += 1 + end + cnt += 1 + end + puts("Output: #{flag}\n\n") +end + +=begin +----------------------------------------------- +SAMPLE OUTPUT +ruby .\MatchingMembers.rb + +Input: @ints = [1, 1, 4, 2, 1, 3] +Output: 3 + +Input: @ints = [5, 1, 2, 3, 4] +Output: 0 + +Input: @ints = [1, 2, 3, 4, 5] +Output: 5 +----------------------------------------------- +=end + + diff --git a/challenge-222/robert-dicicco/ruby/ch-2.rb b/challenge-222/robert-dicicco/ruby/ch-2.rb new file mode 100644 index 0000000000..73fac1b800 --- /dev/null +++ b/challenge-222/robert-dicicco/ruby/ch-2.rb @@ -0,0 +1,52 @@ +#!/usr/bin/env ruby +=begin +----------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-06-22 +Challenge 222 Task 2 Last Member ( Ruby ) +----------------------------------------------- +=end + +allints = Array.new +for i in 0 ... ARGV.length + allints << ARGV[i].to_i +end +puts("Input: @ints = #{allints}") +loop do + srt = allints.sort + elems = srt.length() + if elems <= 1 + puts("Output: #{elems}\n\n") + exit + else + lrg = srt.last + nxt = srt[elems-2] + index = allints.find_index(lrg) + allints.delete_at(index) + index = allints.find_index(nxt) + allints.delete_at(index) + newmember = lrg - nxt + if lrg > nxt + allints.push(newmember) + end + end +end + +=begin +----------------------------------------------- +SAMPLE OUTPUT +ruby .\LastMember.rb 2 7 4 1 8 1 +Input: @ints = [2, 7, 4, 1, 8, 1] +Output: 1 + +ruby .\LastMember.rb 1 1 +Input: @ints = [1, 1] +Output: 0 + +ruby .\LastMember.rb 1 +Input: @ints = [1] +Output: 1 +----------------------------------------------- +=end + + diff --git a/challenge-222/ulrich-rieke/cpp/ch-1.cpp b/challenge-222/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..d958f3238d --- /dev/null +++ b/challenge-222/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,40 @@ +#include <iostream> +#include <vector> +#include <algorithm> +#include <string> + +std::vector<std::string> split( const std::string & startline , + const std::string & sep ) { + std::vector<std::string> separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr(start , pos - start )) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +int main( ) { + std::cout << "Enter some integers, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector<std::string> numberstrings( split( line , " " ) ) ; + std::vector<int> numbers ; + for ( auto s : numberstrings ) + numbers.push_back( std::stoi( s ) ) ; + if ( std::is_sorted( numbers.begin( ) , numbers.end( ) ) ) + std::cout << numbers.size( ) << std::endl ; + else { + std::vector<int> forSorting( numbers ) ; + std::sort( forSorting.begin( ) , forSorting.end( ) ) ; + int posCount = 0 ; + for ( int i = 0 ; i < numbers.size( ) ; i++ ) { + if ( numbers[ i ] == forSorting[ i ] ) + posCount++ ; + } + std::cout << posCount << std::endl ; + } + return 0 ; +} diff --git a/challenge-222/ulrich-rieke/haskell/ch-1.hs b/challenge-222/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..4eb2d821f3 --- /dev/null +++ b/challenge-222/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,6 @@ +module Challenge222 + where +import Data.List ( sort ) + +solution :: [Int] -> Int +solution list = length $ filter (\p -> fst p == snd p ) $ zip list ( sort list ) diff --git a/challenge-222/ulrich-rieke/haskell/ch-2.hs b/challenge-222/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..68d9dfef5f --- /dev/null +++ b/challenge-222/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,37 @@ +module Challenge222_2 + where +import Data.List ( sort , findIndex ) +import Data.List.Split ( divvy ) +import Data.Maybe ( fromJust ) + +solution :: [Int] -> Int +solution list + |l == 1 = head list + |l == 2 = abs ( head list - last list ) + |otherwise = head $ until ( (== 1 ) . length ) step list + where + l :: Int + l = length list + +step :: [Int] -> [Int] +step aList + |l == 1 = aList + |l == 2 = [abs( head aList - last aList)] + |otherwise = take pos aList ++ [mini] ++ drop ( pos + 2 ) sortedList + where + differences :: [Int] + differences = map (\li -> abs( head li - last li) ) $ divvy 2 1 sortedList + mini :: Int + mini = minimum $ filter ( /= 0 ) differences + pos :: Int + pos = fromJust $ findIndex ( == mini ) differences + sortedList :: [Int] + sortedList = sort aList + l :: Int + l = length aList + +main :: IO ( ) +main = do + putStrLn "Enter some integers , separated by blanks!" + numberstrings <- getLine + print $ solution $ map read $ words numberstrings diff --git a/challenge-222/ulrich-rieke/perl/ch-1.pl b/challenge-222/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..b96063048f |
