From 2e92a2fcfd36aad67aa2a314de715f71eadcade2 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Sat, 6 Apr 2024 17:57:26 +0200 Subject: Add ch-1 in Perl, Python, Ruby and Go --- challenge-263/spadacciniweb/go/ch-1.go | 61 ++++++++++++++++++++++++++++++ challenge-263/spadacciniweb/perl/ch-1.pl | 61 ++++++++++++++++++++++++++++++ challenge-263/spadacciniweb/python/ch-1.py | 48 +++++++++++++++++++++++ challenge-263/spadacciniweb/ruby/ch-1.rb | 51 +++++++++++++++++++++++++ 4 files changed, 221 insertions(+) create mode 100644 challenge-263/spadacciniweb/go/ch-1.go create mode 100644 challenge-263/spadacciniweb/perl/ch-1.pl create mode 100644 challenge-263/spadacciniweb/python/ch-1.py create mode 100644 challenge-263/spadacciniweb/ruby/ch-1.rb diff --git a/challenge-263/spadacciniweb/go/ch-1.go b/challenge-263/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..d350929f77 --- /dev/null +++ b/challenge-263/spadacciniweb/go/ch-1.go @@ -0,0 +1,61 @@ +/* +# Task 1: Target Index +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and a target element $k. +# Write a script to return the list of indices in the sorted array where the element is same as the given target element. +# +# Example 1 +# Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 +# Output: (1, 2) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 +# +# Example 2 +# Input: @ints = (1, 2, 4, 3, 5), $k = 6 +# Output: () +# +# No element in the given array matching the given target. +# +# Example 3 +# Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 +# Output: (4) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target index: (4) as $ints[4] = 4 +*/ + +package main + +import ( + "fmt" + "sort" +) + +func list_of_indices(ints []int, k int) { + sort.Ints(ints) + indexes := make([]int, 0) + for i, v := range ints { + if v == k { + indexes = append(indexes, i) + } + } + fmt.Printf("%s -> %s\n", + fmt.Sprint(ints), + fmt.Sprint(indexes)) +} + +func main() { + ints := []int{1, 5, 3, 2, 4, 2} + k := 2 + list_of_indices(ints, k) + + ints = []int{1, 2, 4, 3, 5} + k = 6 + list_of_indices(ints, k) + + ints = []int{5, 3, 2, 4, 2, 1} + k = 4 + list_of_indices(ints, k) +} diff --git a/challenge-263/spadacciniweb/perl/ch-1.pl b/challenge-263/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..41acb9fe2b --- /dev/null +++ b/challenge-263/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl + +# Task 1: Target Index +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and a target element $k. +# Write a script to return the list of indices in the sorted array where the element is same as the given target element. +# +# Example 1 +# Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 +# Output: (1, 2) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 +# +# Example 2 +# Input: @ints = (1, 2, 4, 3, 5), $k = 6 +# Output: () +# +# No element in the given array matching the given target. +# +# Example 3 +# Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 +# Output: (4) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target index: (4) as $ints[4] = 4 + +use strict; +use warnings; + +my @ints = (1, 5, 3, 2, 4, 2); +my $k = 2; +list_of_indices(\@ints, $k); + +@ints = (1, 2, 4, 3, 5); +$k = 6; +list_of_indices(\@ints, $k); + +@ints = (5, 3, 2, 4, 2, 1); +$k = 4; +list_of_indices(\@ints, $k); + +exit 0; + +sub list_of_indices { + my $ints = shift; + my $k = shift; + + my @ints = sort { $a <=> $b } @$ints; + my @indexes; + while (my ($idx, $value) = each(@ints) ) { + push @indexes, $idx + if $value == $k; + } + printf "(%s) -> (%s)\n", + (join ',', @$ints), + (join ',', @indexes); + + return undef; +} diff --git a/challenge-263/spadacciniweb/python/ch-1.py b/challenge-263/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..cec470dcac --- /dev/null +++ b/challenge-263/spadacciniweb/python/ch-1.py @@ -0,0 +1,48 @@ +# Task 1: Target Index +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and a target element $k. +# Write a script to return the list of indices in the sorted array where the element is same as the given target element. +# +# Example 1 +# Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 +# Output: (1, 2) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 +# +# Example 2 +# Input: @ints = (1, 2, 4, 3, 5), $k = 6 +# Output: () +# +# No element in the given array matching the given target. +# +# Example 3 +# Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 +# Output: (4) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target index: (4) as $ints[4] = 4 + +def list_of_indices(ints,k): + ints.sort() + + indexes = list(filter(lambda i: ints[i] == k, range(len(ints)))) + print("(%s) -> (%s)" % + ( ",".join(map(str, ints)), + ",".join(map(str, indexes)) + ) + ) + +if __name__ == "__main__": + ints = [1, 5, 3, 2, 4, 2] + k = 2 + list_of_indices(ints, k) + + ints = [1, 2, 4, 3, 5] + k = 6 + list_of_indices(ints, k) + + ints = [5, 3, 2, 4, 2, 1] + k = 4 + list_of_indices(ints, k) diff --git a/challenge-263/spadacciniweb/ruby/ch-1.rb b/challenge-263/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..c85aab102d --- /dev/null +++ b/challenge-263/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,51 @@ +# Task 1: Target Index +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and a target element $k. +# Write a script to return the list of indices in the sorted array where the element is same as the given target element. +# +# Example 1 +# Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 +# Output: (1, 2) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 +# +# Example 2 +# Input: @ints = (1, 2, 4, 3, 5), $k = 6 +# Output: () +# +# No element in the given array matching the given target. +# +# Example 3 +# Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 +# Output: (4) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target index: (4) as $ints[4] = 4 + +def list_of_indices(ints, k) + ints = ints.sort + indexes = [] + ints.each_with_index do | value, idx | + if value == k + indexes.push idx + end + end + + printf "(%s) -> (%s)\n", + ints.join(","), + indexes.join(",") +end + +ints = [1, 5, 3, 2, 4, 2] +k = 2 +list_of_indices(ints, k) + +ints = [1, 2, 4, 3, 5] +k = 6 +list_of_indices(ints, k) + +ints = [5, 3, 2, 4, 2, 1] +k = 4 +list_of_indices(ints, k) -- cgit