aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-11-27 12:02:03 +0000
committerGitHub <noreply@github.com>2022-11-27 12:02:03 +0000
commit51f5a1be301289fabb64d0ca25c936c1ecc005e2 (patch)
treed9e059f4249f310c54c60adbe04129f1d535f5c7
parent4a714da4fecfe9b3954e0aef90060f84050223a9 (diff)
parent0e91f637fdb6d6113982a78a64ae9dcd36134ce3 (diff)
downloadperlweeklychallenge-club-51f5a1be301289fabb64d0ca25c936c1ecc005e2.tar.gz
perlweeklychallenge-club-51f5a1be301289fabb64d0ca25c936c1ecc005e2.tar.bz2
perlweeklychallenge-club-51f5a1be301289fabb64d0ca25c936c1ecc005e2.zip
Merge pull request #7161 from simongreen-net/master
Simon's solution to challenge 192
-rw-r--r--challenge-192/sgreen/README.md4
-rw-r--r--challenge-192/sgreen/blog.txt1
-rwxr-xr-xchallenge-192/sgreen/perl/ch-1.pl17
-rwxr-xr-xchallenge-192/sgreen/perl/ch-2.pl51
-rwxr-xr-xchallenge-192/sgreen/python/ch-1.py19
-rwxr-xr-xchallenge-192/sgreen/python/ch-2.py48
6 files changed, 138 insertions, 2 deletions
diff --git a/challenge-192/sgreen/README.md b/challenge-192/sgreen/README.md
index ef0288441e..4a7b1a13f0 100644
--- a/challenge-192/sgreen/README.md
+++ b/challenge-192/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 191
+# The Weekly Challenge 192
-Blog [The cute recursive function](https://dev.to/simongreennet/the-cute-recursive-function-2gdo)
+Blog [Weekly Challenge 192](https://dev.to/simongreennet/weekly-challenge-192-30a6)
diff --git a/challenge-192/sgreen/blog.txt b/challenge-192/sgreen/blog.txt
new file mode 100644
index 0000000000..79bc040803
--- /dev/null
+++ b/challenge-192/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-192-30a6 \ No newline at end of file
diff --git a/challenge-192/sgreen/perl/ch-1.pl b/challenge-192/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..bc4133e6ff
--- /dev/null
+++ b/challenge-192/sgreen/perl/ch-1.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main ($n) {
+ # Find the first power of two greater than n
+ my $p = 1;
+ $p *= 2 while ( $p <= $n );
+
+ # Print the inverse of n
+ say $p- $n - 1;
+}
+
+main( $ARGV[0] );
diff --git a/challenge-192/sgreen/perl/ch-2.pl b/challenge-192/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..858e33a7e7
--- /dev/null
+++ b/challenge-192/sgreen/perl/ch-2.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util qw'min sum';
+
+sub main (@nums) {
+ # See if there is a possible target
+ my $target = sum(@nums) / scalar(@nums);
+ my $items = scalar(@nums);
+ my $moves = 0;
+
+ if ( $target != int($target) ) {
+ # There is no possible solution
+ say '-1';
+ return;
+ }
+
+ foreach my $i ( 0 .. $#nums ) {
+ # How many do we need to make the value the target
+ my $needed = $target - $nums[$i];
+
+ while ( $needed > 0 ) {
+ # Loop from the left to find which items we can remove numbers from
+ foreach my $j ( 0 .. $#nums ) {
+ my $has = $nums[$j];
+
+ # We have found a value we can take from
+ if ( $has > $target ) {
+ # Calculate how many we can take
+ my $take = min( $needed, $has - $target );
+
+ $nums[$i] += $take;
+ $nums[$j] -= $take;
+ $needed -= $take;
+
+ # Record the number of moves it would take to steal
+ # from the other item
+ $moves += $take * abs( $i - $j );
+ }
+ }
+ }
+ }
+
+ say $moves;
+}
+
+main(@ARGV)
diff --git a/challenge-192/sgreen/python/ch-1.py b/challenge-192/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..dc43fc664c
--- /dev/null
+++ b/challenge-192/sgreen/python/ch-1.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def main(n):
+ '''Main function'''
+
+ # Find the first power of two greater than n
+ p = 1
+ while p <= n:
+ p *= 2
+
+ # Print the inverse of n
+ print(p-n-1)
+
+
+if __name__ == '__main__':
+ main(int(sys.argv[1]))
diff --git a/challenge-192/sgreen/python/ch-2.py b/challenge-192/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..7a06fb1764
--- /dev/null
+++ b/challenge-192/sgreen/python/ch-2.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def main(s):
+ '''Main function'''
+ # Turn input into integers
+ nums = [int(x) for x in s]
+
+ # See if there is a possible target
+ target = sum(nums) / len(nums)
+ items = len(nums)
+ moves = 0
+
+ if target % 1 != 0:
+ # There is no possible solution
+ print(-1)
+ return
+
+ target = int(target)
+ for i in range(items):
+ # How many do we need to make the value the target
+ needed = target - nums[i]
+
+ while needed > 0:
+ # Loop from the left to find which items we can remove numbers from
+ for j in range(items):
+ has = nums[j]
+
+ # We have found a value we can take from
+ if has > target:
+ # Calculate how many we can take
+ take = min(needed, has - target)
+
+ nums[i] += take
+ nums[j] -= take
+ needed -= take
+
+ # Record the number of moves it would take to steal
+ # from the other item
+ moves += take * abs(i-j)
+
+ print(moves)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])