aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-24 16:09:37 +0000
committerGitHub <noreply@github.com>2024-03-24 16:09:37 +0000
commit4371650e99dc1c844372745f7f5bc756ac8e1dba (patch)
treecade2db998ca918429d1b3e4ae9f9cd1c48eefa9
parent2af2b576294572185b408bd7a4a4a24d3e44fcff (diff)
parent91f2331d26cbc320ce8712336d691010fa1d345e (diff)
downloadperlweeklychallenge-club-4371650e99dc1c844372745f7f5bc756ac8e1dba.tar.gz
perlweeklychallenge-club-4371650e99dc1c844372745f7f5bc756ac8e1dba.tar.bz2
perlweeklychallenge-club-4371650e99dc1c844372745f7f5bc756ac8e1dba.zip
Merge pull request #9799 from simongreen-net/master
Simon's solution to challenge 261
-rw-r--r--challenge-261/sgreen/README.md4
-rw-r--r--challenge-261/sgreen/blog.txt1
-rwxr-xr-xchallenge-261/sgreen/perl/ch-1.pl16
-rwxr-xr-xchallenge-261/sgreen/perl/ch-2.pl21
-rwxr-xr-xchallenge-261/sgreen/python/ch-1.py30
-rwxr-xr-xchallenge-261/sgreen/python/ch-2.py33
-rwxr-xr-xchallenge-261/sgreen/python/test.py22
7 files changed, 125 insertions, 2 deletions
diff --git a/challenge-261/sgreen/README.md b/challenge-261/sgreen/README.md
index 068010e562..f28ff5feac 100644
--- a/challenge-261/sgreen/README.md
+++ b/challenge-261/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 260
+# The Weekly Challenge 261
-Blog: [Weekly Challenge 260](https://dev.to/simongreennet/counting-and-ranking-3jjb)
+Blog: [Weekly Challenge 261](https://dev.to/simongreennet/weekly-challenge-261-3pg1)
diff --git a/challenge-261/sgreen/blog.txt b/challenge-261/sgreen/blog.txt
new file mode 100644
index 0000000000..a29807a541
--- /dev/null
+++ b/challenge-261/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-261-3pg1 \ No newline at end of file
diff --git a/challenge-261/sgreen/perl/ch-1.pl b/challenge-261/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..b57c0f59f3
--- /dev/null
+++ b/challenge-261/sgreen/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'sum';
+
+sub main (@ints) {
+ my $element_sum = sum(@ints);
+ my $digit_sum = sum( map { split // } @ints );
+ say abs( $element_sum - $digit_sum );
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-261/sgreen/perl/ch-2.pl b/challenge-261/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..5e32b08356
--- /dev/null
+++ b/challenge-261/sgreen/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'any';
+
+sub main (@ints) {
+ my $start = pop(@ints);
+ my $solution = $start;
+
+ while ( any { $solution == $_ } @ints ) {
+ $solution *= 2;
+ }
+
+ say $solution;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-261/sgreen/python/ch-1.py b/challenge-261/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..b95c0c3c16
--- /dev/null
+++ b/challenge-261/sgreen/python/ch-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def element_digit_sum(ints: list) -> int:
+ """Calculate the absolute difference between the element sum and the sum
+ of each digit
+
+ Args:
+ ints (list): List of integers
+
+ Returns:
+ int: The absolute difference
+ """
+
+ element_sum = sum(ints)
+ digit_sum = sum(int(i) for s in map(str, ints) for i in s)
+ return abs(element_sum - digit_sum)
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = element_digit_sum(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-261/sgreen/python/ch-2.py b/challenge-261/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..90a73a42a9
--- /dev/null
+++ b/challenge-261/sgreen/python/ch-2.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def multiple_by_two(ints: list, start: int) -> int:
+ """Multiple the start number by 2 if it is in the list
+
+ Args:
+ ints (list): List of integers
+ start (int): The starting number
+
+ Returns:
+ int: The first value that isn't in the string
+ """
+
+ solution = start
+ while solution in ints:
+ solution *= 2
+
+ return solution
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ start = array.pop()
+ result = multiple_by_two(array, start)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-261/sgreen/python/test.py b/challenge-261/sgreen/python/test.py
new file mode 100755
index 0000000000..984f8848e1
--- /dev/null
+++ b/challenge-261/sgreen/python/test.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.element_digit_sum([1, 2, 3, 45]), 36)
+ self.assertEqual(ch_1.element_digit_sum([1, 12, 3]), 9)
+ self.assertEqual(ch_1.element_digit_sum([1, 2, 3, 4]), 0)
+ self.assertEqual(ch_1.element_digit_sum([236, 416, 336, 350]), 1296)
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.multiple_by_two([5, 3, 6, 1, 12], 3), 24)
+ self.assertEqual(ch_2.multiple_by_two([1, 2, 4, 3], 1), 8)
+ self.assertEqual(ch_2.multiple_by_two([5, 6, 7], 2), 2)
+
+
+if __name__ == '__main__':
+ unittest.main()