diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-08-01 18:36:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-01 18:36:56 +0100 |
| commit | 6919309e7a165c572c3cef20c8d0bd7ed9e79f57 (patch) | |
| tree | b11e3a5a1568aaa3d8292d2ea5defe32ed13691a | |
| parent | a5c8c3696920a816b2f4779f0b30fe2558f35551 (diff) | |
| parent | 789fd5f8c7253c1a487a6413e4d9bc46a700a3d2 (diff) | |
| download | perlweeklychallenge-club-6919309e7a165c572c3cef20c8d0bd7ed9e79f57.tar.gz perlweeklychallenge-club-6919309e7a165c572c3cef20c8d0bd7ed9e79f57.tar.bz2 perlweeklychallenge-club-6919309e7a165c572c3cef20c8d0bd7ed9e79f57.zip | |
Merge pull request #8490 from voegelas/challenge-228
Challenge 228
| -rwxr-xr-x | challenge-221/andreas-voegele/kotlin/ch-2.kts | 2 | ||||
| -rwxr-xr-x | challenge-223/andreas-voegele/kotlin/ch-1.kts | 6 | ||||
| -rwxr-xr-x | challenge-224/andreas-voegele/kotlin/ch-2.kts | 9 | ||||
| -rwxr-xr-x | challenge-225/andreas-voegele/kotlin/ch-1.kts | 8 | ||||
| -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 |
8 files changed, 107 insertions, 13 deletions
diff --git a/challenge-221/andreas-voegele/kotlin/ch-2.kts b/challenge-221/andreas-voegele/kotlin/ch-2.kts index 04f42e7cd6..413caa100e 100755 --- a/challenge-221/andreas-voegele/kotlin/ch-2.kts +++ b/challenge-221/andreas-voegele/kotlin/ch-2.kts @@ -15,7 +15,7 @@ fun nextNumbers(acc: MutableList<Int>, distance: Int, ints: Ints): Ints { var a = acc.last() for (b in ints) { if (Math.abs(a - b) == distance) { - acc.add(b); + acc.add(b) a = b } } diff --git a/challenge-223/andreas-voegele/kotlin/ch-1.kts b/challenge-223/andreas-voegele/kotlin/ch-1.kts index 8775bbc91e..f1235a1ba2 100755 --- a/challenge-223/andreas-voegele/kotlin/ch-1.kts +++ b/challenge-223/andreas-voegele/kotlin/ch-1.kts @@ -11,6 +11,6 @@ fun isPrime(n: Long) = fun primeNumbers(n: Long): List<Long> = (2..n).filter { isPrime(it) } -println(primeNumbers(10L).size); -println(primeNumbers(1L).size); -println(primeNumbers(20L).size); +println(primeNumbers(10L).size) +println(primeNumbers(1L).size) +println(primeNumbers(20L).size) diff --git a/challenge-224/andreas-voegele/kotlin/ch-2.kts b/challenge-224/andreas-voegele/kotlin/ch-2.kts index da2245a498..2c2d97f194 100755 --- a/challenge-224/andreas-voegele/kotlin/ch-2.kts +++ b/challenge-224/andreas-voegele/kotlin/ch-2.kts @@ -16,8 +16,7 @@ fun combineDigits(numericString: String): List<List<Int>> = val rest = numericString.drop(n) if (rest.isEmpty()) { listOf(listOf(number)) - } - else { + } else { combineDigits(rest).map { listOf(number) + it } } } @@ -38,6 +37,6 @@ fun isAdditive(numbers: List<Int>): Boolean { fun isAdditiveNumber(numericString: String) = combineDigits(numericString).any { isAdditive(it) } -println(isAdditiveNumber("112358")); -println(isAdditiveNumber("12345")); -println(isAdditiveNumber("199100199")); +println(isAdditiveNumber("112358")) +println(isAdditiveNumber("12345")) +println(isAdditiveNumber("199100199")) diff --git a/challenge-225/andreas-voegele/kotlin/ch-1.kts b/challenge-225/andreas-voegele/kotlin/ch-1.kts index 8f99122e31..6eb1f57d97 100755 --- a/challenge-225/andreas-voegele/kotlin/ch-1.kts +++ b/challenge-225/andreas-voegele/kotlin/ch-1.kts @@ -14,14 +14,14 @@ println( maxWords( "Perl and Raku belong to the same family.", "I love Perl.", - "The Perl and Raku Conference." - ) + "The Perl and Raku Conference.", + ), ) println( maxWords( "The Weekly Challenge.", "Kotlin is the most interesting guest language.", - "Team PWC has over 300 members." - ) + "Team PWC has over 300 members.", + ), ) 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); |
