diff options
| author | Andreas Vögele <andreas@andreasvoegele.com> | 2023-08-01 09:39:36 +0200 |
|---|---|---|
| committer | Andreas Vögele <andreas@andreasvoegele.com> | 2023-08-01 17:16:28 +0200 |
| commit | 59c335be6f85948114dc7e090d1df6fb76b3a62c (patch) | |
| tree | 63226ce42fbc765c9a1f6d64b0bde0bada2022ed | |
| parent | b7c6ba230d95ffad246f0f9874e82cbd95e12f56 (diff) | |
| download | perlweeklychallenge-club-59c335be6f85948114dc7e090d1df6fb76b3a62c.tar.gz perlweeklychallenge-club-59c335be6f85948114dc7e090d1df6fb76b3a62c.tar.bz2 perlweeklychallenge-club-59c335be6f85948114dc7e090d1df6fb76b3a62c.zip | |
Challenge 228 by Andreas Vögele
| -rwxr-xr-x | challenge-228/andreas-voegele/kotlin/ch-1.kts | 13 | ||||
| -rwxr-xr-x | challenge-228/andreas-voegele/kotlin/ch-2.kts | 29 | ||||
| -rwxr-xr-x | challenge-228/andreas-voegele/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-228/andreas-voegele/perl/ch-2.pl | 32 |
4 files changed, 95 insertions, 0 deletions
diff --git a/challenge-228/andreas-voegele/kotlin/ch-1.kts b/challenge-228/andreas-voegele/kotlin/ch-1.kts new file mode 100755 index 0000000000..988a593671 --- /dev/null +++ b/challenge-228/andreas-voegele/kotlin/ch-1.kts @@ -0,0 +1,13 @@ +#!/usr/bin/env kotlin + +/* + * You are given an array of integers. Write a script to find out the sum of + * unique elements in the given array. Non-unique elements are ignored. + */ + +fun uniqueSum(ints: List<Int>): Int = + ints.groupingBy { it }.eachCount().filterValues { it == 1 }.keys.sum() + +println(uniqueSum(listOf(2, 1, 3, 2))) +println(uniqueSum(listOf(1, 1, 1, 1))) +println(uniqueSum(listOf(2, 1, 3, 4))) diff --git a/challenge-228/andreas-voegele/kotlin/ch-2.kts b/challenge-228/andreas-voegele/kotlin/ch-2.kts new file mode 100755 index 0000000000..f4a8d3b6bf --- /dev/null +++ b/challenge-228/andreas-voegele/kotlin/ch-2.kts @@ -0,0 +1,29 @@ +#!/usr/bin/env kotlin + +/* + * You are given an array of integers in which all elements are unique. Write + * a script to perform the following operations until the array is empty and + * return the total count of operations. + * + * If the first element is the smallest then remove it otherwise move it to the + * end. + */ + +fun emptyArray(ints: List<Int>): Int { + tailrec fun removeSmallest(count: Int, ints: List<Int>, mins: List<Int>): Int { + val size = ints.size + if (size < 2) { + return count + size + } + val tail = ints.subList(1, size) + val first = ints.first() + if (first > mins.first()) { + return removeSmallest(count + 1, tail + first, mins) + } + return removeSmallest(count + 1, tail, mins.subList(1, size)) + } + return removeSmallest(0, ints, ints.sorted()) +} + +println(emptyArray(listOf(3, 4, 2))) +println(emptyArray(listOf(1, 2, 3))) diff --git a/challenge-228/andreas-voegele/perl/ch-1.pl b/challenge-228/andreas-voegele/perl/ch-1.pl new file mode 100755 index 0000000000..4232b5ed46 --- /dev/null +++ b/challenge-228/andreas-voegele/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl + +# You are given an array of integers. Write a script to find out the sum of +# unique elements in the given array. Non-unique elements are ignored. + +use 5.036; +use utf8; + +use List::Util qw(sum0); + +sub unique_sum (@ints) { + my %count_for; + for (@ints) { + ++$count_for{$_}; + } + return sum0 grep { $count_for{$_} == 1 } @ints; +} + +say unique_sum(2, 1, 3, 2); +say unique_sum(1, 1, 1, 1); +say unique_sum(2, 1, 3, 4); diff --git a/challenge-228/andreas-voegele/perl/ch-2.pl b/challenge-228/andreas-voegele/perl/ch-2.pl new file mode 100755 index 0000000000..1e650e4256 --- /dev/null +++ b/challenge-228/andreas-voegele/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +# You are given an array of integers in which all elements are unique. Write +# a script to perform the following operations until the array is empty and +# return the total count of operations. +# +# If the first element is the smallest then remove it otherwise move it to the +# end. + +use 5.036; +use utf8; + +sub empty_array (@ints) { + my $count = 0; + my @mins = sort { $a <=> $b } @ints; + while (@ints > 0) { + ++$count; + my $first = shift @ints; + if (@ints > 0) { + if ($first > $mins[0]) { + push @ints, $first; + } + else { + shift @mins; + } + } + } + return $count; +} + +say empty_array(3, 4, 2); +say empty_array(1, 2, 3); |
