aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-07-31 23:21:26 +0100
committerGitHub <noreply@github.com>2023-07-31 23:21:26 +0100
commit43017c7a64e42e35d855c4dc22bfaf819816e70a (patch)
tree83be837245ae12791f9ca49acd07af113a9c5168
parentb65ef24b440e3aae7db4375177c89e1b6abd3d6f (diff)
parent41dbb452693f46c89af624291d7bdb4171f09801 (diff)
downloadperlweeklychallenge-club-43017c7a64e42e35d855c4dc22bfaf819816e70a.tar.gz
perlweeklychallenge-club-43017c7a64e42e35d855c4dc22bfaf819816e70a.tar.bz2
perlweeklychallenge-club-43017c7a64e42e35d855c4dc22bfaf819816e70a.zip
Merge pull request #8476 from LubosKolouch/master
feat(challenge-228/lubos-kolouch/perl,python/): Challenge 228 LK Perl Python
-rw-r--r--challenge-228/lubos-kolouch/perl/ch-1.pl17
-rw-r--r--challenge-228/lubos-kolouch/perl/ch-2.pl22
-rw-r--r--challenge-228/lubos-kolouch/python/ch-1.py17
-rw-r--r--challenge-228/lubos-kolouch/python/ch-2.py16
4 files changed, 72 insertions, 0 deletions
diff --git a/challenge-228/lubos-kolouch/perl/ch-1.pl b/challenge-228/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..a7a8e679cb
--- /dev/null
+++ b/challenge-228/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+use List::Util qw(sum);
+
+sub unique_sum {
+ my @array = @_;
+ my %counts;
+
+ $counts{$_}++ for @array;
+ my @unique = grep { $counts{$_} == 1 } keys %counts;
+
+ return sum(@unique) // 0;
+}
+
+print unique_sum(2, 1, 3, 2), "\n"; # Output: 4
+print unique_sum(1, 1, 1, 1), "\n"; # Output: 0
+print unique_sum(2, 1, 3, 4), "\n"; # Output: 10
diff --git a/challenge-228/lubos-kolouch/perl/ch-2.pl b/challenge-228/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..6849d970e5
--- /dev/null
+++ b/challenge-228/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,22 @@
+use strict;
+use warnings;
+use List::Util 'min';
+
+sub count_operations {
+ my @array = @_;
+ my $count = 0;
+ while (@array) {
+ if ($array[0] == min @array) {
+ shift @array;
+ } else {
+ push @array, shift @array;
+ }
+ $count++;
+ }
+ return $count;
+}
+
+print count_operations(3, 4, 2); # Output: 5
+print "\n";
+print count_operations(1, 2, 3); # Output: 3
+print "\n";
diff --git a/challenge-228/lubos-kolouch/python/ch-1.py b/challenge-228/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..4470977f61
--- /dev/null
+++ b/challenge-228/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+def unique_sum(lst):
+ counts = {}
+ for num in lst:
+ if num in counts:
+ counts[num] += 1
+ else:
+ counts[num] = 1
+
+ return sum(num for num, count in counts.items() if count == 1)
+
+
+print(unique_sum([2, 1, 3, 2])) # Output: 4
+print(unique_sum([1, 1, 1, 1])) # Output: 0
+print(unique_sum([2, 1, 3, 4])) # Output: 10
diff --git a/challenge-228/lubos-kolouch/python/ch-2.py b/challenge-228/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..aa8178b649
--- /dev/null
+++ b/challenge-228/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+def count_operations(lst):
+ count = 0
+ while lst:
+ if lst[0] == min(lst):
+ lst.pop(0)
+ else:
+ lst.append(lst.pop(0))
+ count += 1
+ return count
+
+
+print(count_operations([3, 4, 2])) # Output: 5
+print(count_operations([1, 2, 3])) # Output: 3