diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2024-02-27 11:56:40 +0100 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2024-02-27 11:56:40 +0100 |
| commit | 4d9a6dde06f58b9845286b596e7ad93901bd07bf (patch) | |
| tree | ae259c473f0bde1fdee77b296856ae665e7434e0 | |
| parent | 4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff) | |
| download | perlweeklychallenge-club-4d9a6dde06f58b9845286b596e7ad93901bd07bf.tar.gz perlweeklychallenge-club-4d9a6dde06f58b9845286b596e7ad93901bd07bf.tar.bz2 perlweeklychallenge-club-4d9a6dde06f58b9845286b596e7ad93901bd07bf.zip | |
Add ch-1 and ch-2 in Perl, Go and Elixir
| -rw-r--r-- | challenge-258/spadacciniweb/elixir/ch-1.exs | 39 | ||||
| -rw-r--r-- | challenge-258/spadacciniweb/elixir/ch-2.exs | 62 | ||||
| -rw-r--r-- | challenge-258/spadacciniweb/go/ch-1.go | 51 | ||||
| -rw-r--r-- | challenge-258/spadacciniweb/go/ch-2.go | 62 | ||||
| -rw-r--r-- | challenge-258/spadacciniweb/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-258/spadacciniweb/perl/ch-2.pl | 60 |
6 files changed, 321 insertions, 0 deletions
diff --git a/challenge-258/spadacciniweb/elixir/ch-1.exs b/challenge-258/spadacciniweb/elixir/ch-1.exs new file mode 100644 index 0000000000..d7c7bc9b28 --- /dev/null +++ b/challenge-258/spadacciniweb/elixir/ch-1.exs @@ -0,0 +1,39 @@ +# Task 1: Count Even Digits Number +# Submitted by: Mohammad Sajid Anwar +# +# You are given a array of positive integers, @ints. +# +# Write a script to find out how many integers have even number of digits. +# +# Example 1 +# Input: @ints = (10, 1, 111, 24, 1000) +# Output: 3 +# +# There are 3 integers having even digits i.e. 10, 24 and 1000. +# +# Example 2 +# Input: @ints = (111, 1, 11111) +# Output: 0 +# +# Example 3 +# Input: @ints = (2, 8, 1024, 256) +# Output: 1 + +defmodule DigitsNumber do + def even(ints) do + Enum.filter(ints, fn x -> rem( String.length(Integer.to_string(x)), 2) == 0 end) |> Enum.count() + end + def out(ints) do + IO.write( "(" <> Enum.join(ints, ", ") <> ") -> ") + IO.puts( even(ints) ) + end +end + +ints = [10, 1, 111, 24, 1000] +DigitsNumber.out(ints) + +ints = [111, 1, 11111] +DigitsNumber.out(ints) + +ints = [2, 8, 1024, 256] +DigitsNumber.out(ints) diff --git a/challenge-258/spadacciniweb/elixir/ch-2.exs b/challenge-258/spadacciniweb/elixir/ch-2.exs new file mode 100644 index 0000000000..d2eea6efbc --- /dev/null +++ b/challenge-258/spadacciniweb/elixir/ch-2.exs @@ -0,0 +1,62 @@ +# Task 2: Sum of Values +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @int and an integer $k. +# +# Write a script to find the sum of values whose index binary representation has exactly $k number of 1-bit set. +# +# Example 1 +# Input: @ints = (2, 5, 9, 11, 3), $k = 1 +# Output: 17 +# +# Binary representation of index 0 = 0 +# Binary representation of index 1 = 1 +# Binary representation of index 2 = 10 +# Binary representation of index 3 = 11 +# Binary representation of index 4 = 100 +# +# So the indices 1, 2 and 4 have total one 1-bit sets. +# Therefore the sum, $ints[1] + $ints[2] + $ints[3] = 17 +# +# Example 2 +# Input: @ints = (2, 5, 9, 11, 3), $k = 2 +# Output: 11 +# +# Example 3 +# Input: @ints = (2, 5, 9, 11, 3), $k = 0 +# Output: 2 + +defmodule SumOfValues do + + def sum_filter(ints, k) do + rejected_indices = indexes(ints, k) + ints + |> Stream.with_index() + |> Stream.reject(fn {_item, index} -> index in rejected_indices end) + |> Stream.map(&elem(&1, 0)) + |> Enum.sum() + end + + def indexes(ints, k) do + 0..length(ints) + |> Stream.filter(fn x -> Enum.sum( Integer.digits(x, 2) ) != k end) + end + + def out(ints, k) do + IO.write( "(" <> Enum.join(ints, ", ") <> ") #{k} -> ") + IO.puts( sum_filter(ints, k) ) + end + +end + +ints = [2, 5, 9, 11, 3] +k = 1 +SumOfValues.out(ints, k) + +ints = [2, 5, 9, 11, 3] +k = 2 +SumOfValues.out(ints, k) + +ints = [2, 5, 9, 11, 3] +k = 0 +SumOfValues.out(ints, k) diff --git a/challenge-258/spadacciniweb/go/ch-1.go b/challenge-258/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..d1220c0288 --- /dev/null +++ b/challenge-258/spadacciniweb/go/ch-1.go @@ -0,0 +1,51 @@ +/* +Task 1: Count Even Digits Number +Submitted by: Mohammad Sajid Anwar + +You are given a array of positive integers, @ints. + +Write a script to find out how many integers have even number of digits. + +Example 1 +Input: @ints = (10, 1, 111, 24, 1000) +Output: 3 + +There are 3 integers having even digits i.e. 10, 24 and 1000. + +Example 2 +Input: @ints = (111, 1, 11111) +Output: 0 + +Example 3 +Input: @ints = (2, 8, 1024, 256) +Output: 1 +*/ + +package main + +import ( + "fmt" + "strconv" +) + +func even_digits_number(arrInts []int) { + even := 0 + for i := 0; i < len(arrInts); i++ { + s := strconv.Itoa(arrInts[i]) + if len(s) % 2 == 0 { + even++ + } + } + fmt.Printf("%v -> %d\n", arrInts, even) +} + +func main() { + arrInts := []int{10, 1, 111, 24, 1000} + even_digits_number(arrInts) + + arrInts = []int{111, 1, 11111} + even_digits_number(arrInts) + + arrInts = []int{2, 8, 1024, 256} + even_digits_number(arrInts) +} diff --git a/challenge-258/spadacciniweb/go/ch-2.go b/challenge-258/spadacciniweb/go/ch-2.go new file mode 100644 index 0000000000..dfe1f3dcbe --- /dev/null +++ b/challenge-258/spadacciniweb/go/ch-2.go @@ -0,0 +1,62 @@ +/* +Task 2: Sum of Values +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @int and an integer $k. + +Write a script to find the sum of values whose index binary representation has exactly $k number of 1-bit set. + +Example 1 +Input: @ints = (2, 5, 9, 11, 3), $k = 1 +Output: 17 + +Binary representation of index 0 = 0 +Binary representation of index 1 = 1 +Binary representation of index 2 = 10 +Binary representation of index 3 = 11 +Binary representation of index 4 = 100 + +So the indices 1, 2 and 4 have total one 1-bit sets. +Therefore the sum, $ints[1] + $ints[2] + $ints[3] = 17 + +Example 2 +Input: @ints = (2, 5, 9, 11, 3), $k = 2 +Output: 11 + +Example 3 +Input: @ints = (2, 5, 9, 11, 3), $k = 0 +Output: 2 +*/ + +package main + +import ( + "fmt" + "strings" +) + +func sum_of_value(arrInts []int, k int) { + sum := 0 + for i := 0; i < len(arrInts); i++ { + bin := fmt.Sprintf("%b", i) + s := string(bin[:]) + if strings.Count(s, "1") == k { + sum += arrInts[i] + } + } + fmt.Printf("%v %d -> %d\n", arrInts, k, sum) +} + +func main() { + arrInts := []int{2, 5, 9, 11, 3} + k := 1 + sum_of_value(arrInts, k) + + arrInts = []int{2, 5, 9, 11, 3} + k = 2 + sum_of_value(arrInts, k) + + arrInts = []int{2, 5, 9, 11, 3} + k = 0 + sum_of_value(arrInts, k) +} diff --git a/challenge-258/spadacciniweb/perl/ch-1.pl b/challenge-258/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..9b355b22ca --- /dev/null +++ b/challenge-258/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# Task 1: Count Even Digits Number +# Submitted by: Mohammad Sajid Anwar +# +# You are given a array of positive integers, @ints. +# +# Write a script to find out how many integers have even number of digits. +# +# Example 1 +# Input: @ints = (10, 1, 111, 24, 1000) +# Output: 3 +# +# There are 3 integers having even digits i.e. 10, 24 and 1000. +# +# Example 2 +# Input: @ints = (111, 1, 11111) +# Output: 0 +# +# Example 3 +# Input: @ints = (2, 8, 1024, 256) +# Output: 1 + +use strict; +use warnings; + +my @ints = (10, 1, 111, 24, 1000); +even_digits_number(\@ints); + +@ints = (111, 1, 11111); +even_digits_number(\@ints); + +@ints = (2, 8, 1024, 256); +even_digits_number(\@ints); + +exit 0; + +sub even_digits_number { + my $ints = shift || []; + + printf "(%s) -> %s\n", + (join ', ', @$ints ), + scalar map { (length $_) % 2 ? () : 1 } + @$ints; + + return undef; +} diff --git a/challenge-258/spadacciniweb/perl/ch-2.pl b/challenge-258/spadacciniweb/perl/ch-2.pl new file mode 100644 index 0000000000..cc3338adbf --- /dev/null +++ b/challenge-258/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl + +# Task 2: Sum of Values +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @int and an integer $k. +# +# Write a script to find the sum of values whose index binary representation has exactly $k number of 1-bit set. +# +# Example 1 +# Input: @ints = (2, 5, 9, 11, 3), $k = 1 +# Output: 17 +# +# Binary representation of index 0 = 0 +# Binary representation of index 1 = 1 +# Binary representation of index 2 = 10 +# Binary representation of index 3 = 11 +# Binary representation of index 4 = 100 +# +# So the indices 1, 2 and 4 have total one 1-bit sets. +# Therefore the sum, $ints[1] + $ints[2] + $ints[3] = 17 +# +# Example 2 +# Input: @ints = (2, 5, 9, 11, 3), $k = 2 +# Output: 11 +# +# Example 3 +# Input: @ints = (2, 5, 9, 11, 3), $k = 0 +# Output: 2 + +use strict; +use warnings; +use List::Util qw/ sum /; + +my @ints = (2, 5, 9, 11, 3); my $k = 1; +sum_of_values(\@ints, $k); + +@ints = (2, 5, 9, 11, 3); $k = 2; +sum_of_values(\@ints, $k); + +@ints = (2, 5, 9, 11, 3); $k = 0; +sum_of_values(\@ints, $k); + +exit 0; + +sub sum_of_values { + my $ints = shift || []; + my $k = shift || 0; + + my @indexes = map { ( (scalar grep { /1/ } + split //, sprintf ("%b", $_) ) == $k ) + ? $_ + : () + } 0..(scalar @$ints)-1; + printf "(%s) %d -> %s\n", + (join ', ', @$ints ), $k, + sum @ints[@indexes]; + + return undef; +} |
