From 59c335be6f85948114dc7e090d1df6fb76b3a62c Mon Sep 17 00:00:00 2001 From: Andreas Vögele Date: Tue, 1 Aug 2023 09:39:36 +0200 Subject: Challenge 228 by Andreas Vögele MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challenge-228/andreas-voegele/kotlin/ch-1.kts | 13 +++++++++++ challenge-228/andreas-voegele/kotlin/ch-2.kts | 29 ++++++++++++++++++++++++ challenge-228/andreas-voegele/perl/ch-1.pl | 21 ++++++++++++++++++ challenge-228/andreas-voegele/perl/ch-2.pl | 32 +++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100755 challenge-228/andreas-voegele/kotlin/ch-1.kts create mode 100755 challenge-228/andreas-voegele/kotlin/ch-2.kts create mode 100755 challenge-228/andreas-voegele/perl/ch-1.pl create mode 100755 challenge-228/andreas-voegele/perl/ch-2.pl 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 = + 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 { + tailrec fun removeSmallest(count: Int, ints: List, mins: List): 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); -- cgit From 789fd5f8c7253c1a487a6413e4d9bc46a700a3d2 Mon Sep 17 00:00:00 2001 From: Andreas Vögele Date: Tue, 1 Aug 2023 17:40:40 +0200 Subject: Make ktlint happy --- challenge-221/andreas-voegele/kotlin/ch-2.kts | 2 +- challenge-223/andreas-voegele/kotlin/ch-1.kts | 6 +++--- challenge-224/andreas-voegele/kotlin/ch-2.kts | 9 ++++----- challenge-225/andreas-voegele/kotlin/ch-1.kts | 8 ++++---- 4 files changed, 12 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, 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 = (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> = 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): 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.", + ), ) -- cgit