aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-20 19:16:18 +0000
committerGitHub <noreply@github.com>2024-03-20 19:16:18 +0000
commit78a1fd4752bcec4d417fa7ae8c205703c9767b2a (patch)
tree2af43bea103f0948ec7f113a41f8ab263b921234
parent5395f01802fbf21afed5c5e25a44e46361431be1 (diff)
parenta53fd1be185e212f71d48a8112f20a5a2ed86120 (diff)
downloadperlweeklychallenge-club-78a1fd4752bcec4d417fa7ae8c205703c9767b2a.tar.gz
perlweeklychallenge-club-78a1fd4752bcec4d417fa7ae8c205703c9767b2a.tar.bz2
perlweeklychallenge-club-78a1fd4752bcec4d417fa7ae8c205703c9767b2a.zip
Merge pull request #9778 from spadacciniweb/PWC-261
Add ch-1 and ch-2 in Perl, Elixir, Go, Python, Ruby
-rw-r--r--challenge-261/spadacciniweb/elixir/ch-1.exs63
-rw-r--r--challenge-261/spadacciniweb/elixir/ch-2.exs64
-rw-r--r--challenge-261/spadacciniweb/go/ch-1.go83
-rw-r--r--challenge-261/spadacciniweb/go/ch-2.go79
-rw-r--r--challenge-261/spadacciniweb/perl/ch-1.pl66
-rw-r--r--challenge-261/spadacciniweb/perl/ch-2.pl72
-rw-r--r--challenge-261/spadacciniweb/python/ch-1.py58
-rw-r--r--challenge-261/spadacciniweb/python/ch-2.py61
-rw-r--r--challenge-261/spadacciniweb/ruby/ch-1.rb56
-rw-r--r--challenge-261/spadacciniweb/ruby/ch-2.rb60
10 files changed, 662 insertions, 0 deletions
diff --git a/challenge-261/spadacciniweb/elixir/ch-1.exs b/challenge-261/spadacciniweb/elixir/ch-1.exs
new file mode 100644
index 0000000000..deb687e444
--- /dev/null
+++ b/challenge-261/spadacciniweb/elixir/ch-1.exs
@@ -0,0 +1,63 @@
+# Task 1: Element Digit Sum
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints.
+# Write a script to evaluate the absolute difference between element and digit sum of the given array.
+#
+# Example 1
+# Input: @ints = (1,2,3,45)
+# Output: 36
+#
+# Element Sum: 1 + 2 + 3 + 45 = 51
+# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15
+# Absolute Difference: | 51 - 15 | = 36
+#
+# Example 2
+# Input: @ints = (1,12,3)
+# Output: 9
+#
+# Element Sum: 1 + 12 + 3 = 16
+# Digit Sum: 1 + 1 + 2 + 3 = 7
+# Absolute Difference: | 16 - 7 | = 9
+#
+# Example 3
+# Input: @ints = (1,2,3,4)
+# Output: 0
+#
+# Element Sum: 1 + 2 + 3 + 4 = 10
+# Digit Sum: 1 + 2 + 3 + 4 = 10
+# Absolute Difference: | 10 - 10 | = 0
+#
+# Example 4
+# Input: @ints = (236, 416, 336, 350)
+# Output: 1296
+
+defmodule AbsoluteDifference do
+
+ def sum_digits(ints) do
+ ints
+ |> Enum.map(fn x -> Enum.sum( Integer.digits(x) ) end)
+ |> Enum.sum()
+ end
+
+ def abs_diff(ints) do
+ Enum.sum(ints) - sum_digits(ints)
+ end
+
+ def out(ints) do
+ IO.write( "(" <> Enum.join(ints, ",") <> ") -> ")
+ IO.puts( abs_diff(ints) )
+ end
+end
+
+ints = [1,2,3,45]
+AbsoluteDifference.out(ints)
+
+ints = [1,12,3]
+AbsoluteDifference.out(ints)
+
+ints = [1,2,3,4]
+AbsoluteDifference.out(ints)
+
+ints = [236, 416, 336, 350]
+AbsoluteDifference.out(ints)
diff --git a/challenge-261/spadacciniweb/elixir/ch-2.exs b/challenge-261/spadacciniweb/elixir/ch-2.exs
new file mode 100644
index 0000000000..e385bfad2c
--- /dev/null
+++ b/challenge-261/spadacciniweb/elixir/ch-2.exs
@@ -0,0 +1,64 @@
+# Task 2: Multiply by Two
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints and an integer $start..
+# Write a script to do the followings:
+#
+# a) Look for $start in the array @ints, if found multiply the number by 2
+# b) If not found stop the process otherwise repeat
+#
+# In the end return the final value.
+#
+# Example 1
+# Input: @ints = (5,3,6,1,12) and $start = 3
+# Output: 24
+#
+# Step 1: 3 is in the array so 3 x 2 = 6
+# Step 2: 6 is in the array so 6 x 2 = 12
+# Step 3: 12 is in the array so 12 x 2 = 24
+#
+# 24 is not found in the array so return 24.
+#
+# Example 2
+# Input: @ints = (1,2,4,3) and $start = 1
+# Output: 8
+#
+# Step 1: 1 is in the array so 1 x 2 = 2
+# Step 2: 2 is in the array so 2 x 2 = 4
+# Step 3: 4 is in the array so 4 x 2 = 8
+#
+# 8 is not found in the array so return 8.
+#
+# Example 3
+# Input: @ints = (5,6,7) and $start = 2
+# Output: 2
+#
+# 2 is not found in the array so return 2.
+
+defmodule MultiplyByTwo do
+
+ def not_found(ints, start) do
+ if Enum.member?(ints, start) do
+ not_found(ints, start*2)
+ else
+ start
+ end
+ end
+
+ def out(start, ints) do
+ IO.write( "start " <> Integer.to_string(start) <> " (" <> Enum.join(ints, ",") <> ") -> ")
+ IO.puts( not_found(ints, start) )
+ end
+end
+
+ints = [5,3,6,1,12]
+start = 3
+MultiplyByTwo.out(start, ints)
+
+ints = [1,2,4,3]
+start = 1
+MultiplyByTwo.out(start, ints)
+
+ints = [5,6,7]
+start = 2
+MultiplyByTwo.out(start, ints)
diff --git a/challenge-261/spadacciniweb/go/ch-1.go b/challenge-261/spadacciniweb/go/ch-1.go
new file mode 100644
index 0000000000..6ad86be24f
--- /dev/null
+++ b/challenge-261/spadacciniweb/go/ch-1.go
@@ -0,0 +1,83 @@
+/*
+Task 1: Element Digit Sum
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of integers, @ints.
+Write a script to evaluate the absolute difference between element and digit sum of the given array.
+
+Example 1
+Input: @ints = (1,2,3,45)
+Output: 36
+
+Element Sum: 1 + 2 + 3 + 45 = 51
+Digit Sum: 1 + 2 + 3 + 4 + 5 = 15
+Absolute Difference: | 51 - 15 | = 36
+
+Example 2
+Input: @ints = (1,12,3)
+Output: 9
+
+Element Sum: 1 + 12 + 3 = 16
+Digit Sum: 1 + 1 + 2 + 3 = 7
+Absolute Difference: | 16 - 7 | = 9
+
+Example 3
+Input: @ints = (1,2,3,4)
+Output: 0
+
+Element Sum: 1 + 2 + 3 + 4 = 10
+Digit Sum: 1 + 2 + 3 + 4 = 10
+Absolute Difference: | 10 - 10 | = 0
+
+Example 4
+Input: @ints = (236, 416, 336, 350)
+Output: 1296
+*/
+
+package main
+
+import (
+ "fmt"
+ "strconv"
+)
+
+func absDiffInt(x, y int) int {
+ if x < y {
+ return y - x
+ }
+ return x - y
+}
+
+func absolute_difference(ints []int) {
+ sum_ints := 0
+ sum_digits := 0
+ for i := 0; i < len(ints); i++ {
+ sum_ints += ints[i]
+ str := strconv.Itoa(ints[i])
+ for p, _ := range str {
+ i, e := strconv.Atoi(string(str[p]))
+ if e == nil {
+ sum_digits += i
+ }
+ }
+ }
+
+ s := fmt.Sprint(ints)
+ fmt.Printf("%s -> %d\n",
+ s,
+ absDiffInt(sum_ints, sum_digits))
+}
+
+func main() {
+ ints := []int{1,2,3,45}
+ absolute_difference(ints)
+
+ ints = []int{1,12,3}
+ absolute_difference(ints)
+
+ ints = []int{1,2,3,4}
+ absolute_difference(ints)
+
+ ints = []int{236, 416, 336, 350}
+ absolute_difference(ints)
+}
diff --git a/challenge-261/spadacciniweb/go/ch-2.go b/challenge-261/spadacciniweb/go/ch-2.go
new file mode 100644
index 0000000000..5a0e19f4d9
--- /dev/null
+++ b/challenge-261/spadacciniweb/go/ch-2.go
@@ -0,0 +1,79 @@
+/*
+Task 2: Multiply by Two
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of integers, @ints and an integer $start..
+Write a script to do the followings:
+
+a) Look for $start in the array @ints, if found multiply the number by 2
+b) If not found stop the process otherwise repeat
+
+In the end return the final value.
+
+Example 1
+Input: @ints = (5,3,6,1,12) and $start = 3
+Output: 24
+
+Step 1: 3 is in the array so 3 x 2 = 6
+Step 2: 6 is in the array so 6 x 2 = 12
+Step 3: 12 is in the array so 12 x 2 = 24
+
+24 is not found in the array so return 24.
+
+Example 2
+Input: @ints = (1,2,4,3) and $start = 1
+Output: 8
+
+Step 1: 1 is in the array so 1 x 2 = 2
+Step 2: 2 is in the array so 2 x 2 = 4
+Step 3: 4 is in the array so 4 x 2 = 8
+
+8 is not found in the array so return 8.
+
+Example 3
+Input: @ints = (5,6,7) and $start = 2
+Output: 2
+
+2 is not found in the array so return 2.
+*/
+
+package main
+
+import (
+ "fmt"
+)
+
+func elementInSlice(c int, ints []int) bool {
+ for _, i := range ints {
+ if c == i {
+ return true
+ }
+ }
+ return false
+}
+
+func multiply_by_two(start int, ints []int) {
+ curr := start
+ for elementInSlice(curr, ints) == true {
+ curr *= 2
+ }
+
+ fmt.Printf("%d %v -> %d\n",
+ start,
+ ints,
+ curr)
+}
+
+func main() {
+ ints := []int{5,3,6,1,12}
+ start := 3
+ multiply_by_two(start, ints)
+
+ ints = []int{1,2,4,3}
+ start = 1
+ multiply_by_two(start, ints)
+
+ ints = []int{5,6,7}
+ start = 2
+ multiply_by_two(start, ints)
+}
diff --git a/challenge-261/spadacciniweb/perl/ch-1.pl b/challenge-261/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..9695cfe99d
--- /dev/null
+++ b/challenge-261/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/env perl
+
+# Task 1: Element Digit Sum
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints.
+# Write a script to evaluate the absolute difference between element and digit sum of the given array.
+#
+# Example 1
+# Input: @ints = (1,2,3,45)
+# Output: 36
+#
+# Element Sum: 1 + 2 + 3 + 45 = 51
+# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15
+# Absolute Difference: | 51 - 15 | = 36
+#
+# Example 2
+# Input: @ints = (1,12,3)
+# Output: 9
+#
+# Element Sum: 1 + 12 + 3 = 16
+# Digit Sum: 1 + 1 + 2 + 3 = 7
+# Absolute Difference: | 16 - 7 | = 9
+#
+# Example 3
+# Input: @ints = (1,2,3,4)
+# Output: 0
+#
+# Element Sum: 1 + 2 + 3 + 4 = 10
+# Digit Sum: 1 + 2 + 3 + 4 = 10
+# Absolute Difference: | 10 - 10 | = 0
+#
+# Example 4
+# Input: @ints = (236, 416, 336, 350)
+# Output: 1296
+
+use strict;
+use warnings;
+use List::Util qw/ sum /;
+
+my @ints = (1,2,3,45);
+absolute_difference(\@ints);
+
+@ints = (1,12,3);
+absolute_difference(\@ints);
+
+@ints = (1,2,3,4);
+absolute_difference(\@ints);
+
+@ints = (236, 416, 336, 350);
+absolute_difference(\@ints);
+
+exit 0;
+
+sub absolute_difference {
+ my $ints = shift;
+
+ my $sum = sum(@$ints);
+ my $digit_sum = sum map { split //, $_ }
+ @$ints;
+ printf "(%s) -> %s\n",
+ (join ',', @$ints),
+ abs($digit_sum - $sum);
+
+ return undef;
+}
diff --git a/challenge-261/spadacciniweb/perl/ch-2.pl b/challenge-261/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..935834c23c
--- /dev/null
+++ b/challenge-261/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/env perl
+
+# Task 2: Multiply by Two
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints and an integer $start..
+# Write a script to do the followings:
+#
+# a) Look for $start in the array @ints, if found multiply the number by 2
+# b) If not found stop the process otherwise repeat
+#
+# In the end return the final value.
+#
+# Example 1
+# Input: @ints = (5,3,6,1,12) and $start = 3
+# Output: 24
+#
+# Step 1: 3 is in the array so 3 x 2 = 6
+# Step 2: 6 is in the array so 6 x 2 = 12
+# Step 3: 12 is in the array so 12 x 2 = 24
+#
+# 24 is not found in the array so return 24.
+#
+# Example 2
+# Input: @ints = (1,2,4,3) and $start = 1
+# Output: 8
+#
+# Step 1: 1 is in the array so 1 x 2 = 2
+# Step 2: 2 is in the array so 2 x 2 = 4
+# Step 3: 4 is in the array so 4 x 2 = 8
+#
+# 8 is not found in the array so return 8.
+#
+# Example 3
+# Input: @ints = (5,6,7) and $start = 2
+# Output: 2
+#
+# 2 is not found in the array so return 2.
+
+use strict;
+use warnings;
+
+my @ints = (5,3,6,1,12);
+my $start = 3;
+multiply_by_two($start, \@ints);
+
+@ints = (1,2,4,3);
+$start = 1;
+multiply_by_two($start, \@ints);
+
+@ints = (5,6,7);
+$start = 2;
+multiply_by_two($start, \@ints);
+
+exit 0;
+
+sub multiply_by_two {
+ my $start = shift;
+ my $ints = shift;
+
+ my $curr = $start;
+ while (grep /^$curr$/, @$ints) {
+ $curr *= 2;
+ }
+
+ printf "start %d (%s) -> %s\n",
+ $start,
+ (join ',', @$ints),
+ $curr;
+
+ return undef;
+}
diff --git a/challenge-261/spadacciniweb/python/ch-1.py b/challenge-261/spadacciniweb/python/ch-1.py
new file mode 100644
index 0000000000..316c595a5e
--- /dev/null
+++ b/challenge-261/spadacciniweb/python/ch-1.py
@@ -0,0 +1,58 @@
+# Task 1: Element Digit Sum
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints.
+# Write a script to evaluate the absolute difference between element and digit sum of the given array.
+#
+# Example 1
+# Input: @ints = (1,2,3,45)
+# Output: 36
+#
+# Element Sum: 1 + 2 + 3 + 45 = 51
+# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15
+# Absolute Difference: | 51 - 15 | = 36
+#
+# Example 2
+# Input: @ints = (1,12,3)
+# Output: 9
+#
+# Element Sum: 1 + 12 + 3 = 16
+# Digit Sum: 1 + 1 + 2 + 3 = 7
+# Absolute Difference: | 16 - 7 | = 9
+#
+# Example 3
+# Input: @ints = (1,2,3,4)
+# Output: 0
+#
+# Element Sum: 1 + 2 + 3 + 4 = 10
+# Digit Sum: 1 + 2 + 3 + 4 = 10
+# Absolute Difference: | 10 - 10 | = 0
+#
+# Example 4
+# Input: @ints = (236, 416, 336, 350)
+# Output: 1296
+
+def absolute_difference(ints):
+ sum_ints = sum(ints)
+
+ sum_digits = 0
+ for n in ints:
+ sum_digits += sum( int(i) for i in str(n) )
+ print("(%s) -> %d" %
+ ( ",".join(map(str, ints)),
+ abs(sum_ints - sum_digits)
+ )
+ )
+
+if __name__ == "__main__":
+ ints = [1,2,3,45]
+ absolute_difference(ints)
+
+ ints = [1,12,3]
+ absolute_difference(ints)
+
+ ints = [1,2,3,4]
+ absolute_difference(ints)
+
+ ints = [236, 416, 336, 350]
+ absolute_difference(ints)
diff --git a/challenge-261/spadacciniweb/python/ch-2.py b/challenge-261/spadacciniweb/python/ch-2.py
new file mode 100644
index 0000000000..ff17b0fa2b
--- /dev/null
+++ b/challenge-261/spadacciniweb/python/ch-2.py
@@ -0,0 +1,61 @@
+# Task 2: Multiply by Two
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints and an integer $start..
+# Write a script to do the followings:
+#
+# a) Look for $start in the array @ints, if found multiply the number by 2
+# b) If not found stop the process otherwise repeat
+#
+# In the end return the final value.
+#
+# Example 1
+# Input: @ints = (5,3,6,1,12) and $start = 3
+# Output: 24
+#
+# Step 1: 3 is in the array so 3 x 2 = 6
+# Step 2: 6 is in the array so 6 x 2 = 12
+# Step 3: 12 is in the array so 12 x 2 = 24
+#
+# 24 is not found in the array so return 24.
+#
+# Example 2
+# Input: @ints = (1,2,4,3) and $start = 1
+# Output: 8
+#
+# Step 1: 1 is in the array so 1 x 2 = 2
+# Step 2: 2 is in the array so 2 x 2 = 4
+# Step 3: 4 is in the array so 4 x 2 = 8
+#
+# 8 is not found in the array so return 8.
+#
+# Example 3
+# Input: @ints = (5,6,7) and $start = 2
+# Output: 2
+#
+# 2 is not found in the array so return 2.
+
+def multiply_by_two(start, ints):
+ curr = start
+ while (curr in ints):
+ curr *= 2
+
+ print("start %s (%s) -> %d" %
+ ( start,
+ ",".join(map(str, ints)),
+ curr
+ )
+ )
+
+if __name__ == "__main__":
+ ints = [5,3,6,1,12]
+ start = 3
+ multiply_by_two(start, ints);
+
+ ints = [1,2,4,3]
+ start = 1
+ multiply_by_two(start, ints);
+
+ ints = [5,6,7]
+ start= 2
+ multiply_by_two(start, ints);
diff --git a/challenge-261/spadacciniweb/ruby/ch-1.rb b/challenge-261/spadacciniweb/ruby/ch-1.rb
new file mode 100644
index 0000000000..4914abe56b
--- /dev/null
+++ b/challenge-261/spadacciniweb/ruby/ch-1.rb
@@ -0,0 +1,56 @@
+# Task 1: Element Digit Sum
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints.
+# Write a script to evaluate the absolute difference between element and digit sum of the given array.
+#
+# Example 1
+# Input: @ints = (1,2,3,45)
+# Output: 36
+#
+# Element Sum: 1 + 2 + 3 + 45 = 51
+# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15
+# Absolute Difference: | 51 - 15 | = 36
+#
+# Example 2
+# Input: @ints = (1,12,3)
+# Output: 9
+#
+# Element Sum: 1 + 12 + 3 = 16
+# Digit Sum: 1 + 1 + 2 + 3 = 7
+# Absolute Difference: | 16 - 7 | = 9
+#
+# Example 3
+# Input: @ints = (1,2,3,4)
+# Output: 0
+#
+# Element Sum: 1 + 2 + 3 + 4 = 10
+# Digit Sum: 1 + 2 + 3 + 4 = 10
+# Absolute Difference: | 10 - 10 | = 0
+#
+# Example 4
+# Input: @ints = (236, 416, 336, 350)
+# Output: 1296
+
+def absolute_difference(ints)
+ digits_sum = 0
+ ints.each do |i|
+ digits_sum += i.to_s.chars.map { |digit| digit.to_i }.sum
+ end
+
+ printf "(%s) -> %s\n",
+ ints.join(","),
+ (ints.sum - digits_sum).abs
+end
+
+ints = [1,2,3,45]
+absolute_difference(ints);
+
+ints = [1,12,3]
+absolute_difference(ints);
+
+ints = [1,2,3,4]
+absolute_difference(ints);
+
+ints = [236, 416, 336, 350]
+absolute_difference(ints);
diff --git a/challenge-261/spadacciniweb/ruby/ch-2.rb b/challenge-261/spadacciniweb/ruby/ch-2.rb
new file mode 100644
index 0000000000..868d0dae75
--- /dev/null
+++ b/challenge-261/spadacciniweb/ruby/ch-2.rb
@@ -0,0 +1,60 @@
+# Task 2: Multiply by Two
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints and an integer $start..
+# Write a script to do the followings:
+#
+# a) Look for $start in the array @ints, if found multiply the number by 2
+# b) If not found stop the process otherwise repeat
+#
+# In the end return the final value.
+#
+# Example 1
+# Input: @ints = (5,3,6,1,12) and $start = 3
+# Output: 24
+#
+# Step 1: 3 is in the array so 3 x 2 = 6
+# Step 2: 6 is in the array so 6 x 2 = 12
+# Step 3: 12 is in the array so 12 x 2 = 24
+#
+# 24 is not found in the array so return 24.
+#
+# Example 2
+# Input: @ints = (1,2,4,3) and $start = 1
+# Output: 8
+#
+# Step 1: 1 is in the array so 1 x 2 = 2
+# Step 2: 2 is in the array so 2 x 2 = 4
+# Step 3: 4 is in the array so 4 x 2 = 8
+#
+# 8 is not found in the array so return 8.
+#
+# Example 3
+# Input: @ints = (5,6,7) and $start = 2
+# Output: 2
+#
+# 2 is not found in the array so return 2.
+
+def multiply_by_two(start,ints)
+ curr = start
+ while ints.include?(curr)
+ curr *= 2
+ end
+
+ printf "start %d (%s) -> %d\n",
+ start,
+ ints.join(","),
+ curr
+end
+
+ints = [5,3,6,1,12]
+start = 3
+multiply_by_two(start, ints)
+
+ints = [1,2,4,3]
+start = 1
+multiply_by_two(start, ints)
+
+ints = [5,6,7]
+start = 2
+multiply_by_two(start, ints)