diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-28 16:51:56 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-28 16:51:56 +0000 |
| commit | da50b68b9f35e07e6be4896953e6977d21a65b50 (patch) | |
| tree | 4f31e5e8c0359c8c1ce1d3bb05ffdb01a46eb7a6 | |
| parent | 0cb6839f6cf7f4131704f2c6eb5b9b21d58a2d36 (diff) | |
| parent | 209577d51a7745751b741cf7d4ccd95f66892697 (diff) | |
| download | perlweeklychallenge-club-da50b68b9f35e07e6be4896953e6977d21a65b50.tar.gz perlweeklychallenge-club-da50b68b9f35e07e6be4896953e6977d21a65b50.tar.bz2 perlweeklychallenge-club-da50b68b9f35e07e6be4896953e6977d21a65b50.zip | |
Merge pull request #9828 from spadacciniweb/PWC-262
Add ch-1 in Perl, Python, Ruby, Go, Elixir
| -rw-r--r-- | challenge-262/spadacciniweb/elixir/ch-1.exs | 56 | ||||
| -rw-r--r-- | challenge-262/spadacciniweb/go/ch-1.go | 72 | ||||
| -rw-r--r-- | challenge-262/spadacciniweb/perl/ch-1.pl | 59 | ||||
| -rw-r--r-- | challenge-262/spadacciniweb/python/ch-1.py | 49 | ||||
| -rw-r--r-- | challenge-262/spadacciniweb/ruby/ch-1.rb | 47 |
5 files changed, 283 insertions, 0 deletions
diff --git a/challenge-262/spadacciniweb/elixir/ch-1.exs b/challenge-262/spadacciniweb/elixir/ch-1.exs new file mode 100644 index 0000000000..a4ebd1dfb9 --- /dev/null +++ b/challenge-262/spadacciniweb/elixir/ch-1.exs @@ -0,0 +1,56 @@ +# Task 1: Max Positive Negative +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# Write a script to return the maximum number of either positive or negative integers in the given array. +# +# Example 1 +# Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +# Output: 4 +# +# Count of positive integers: 4 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 4 +# +# Example 2 +# Input: @ints = (-1, -2, -3, 1) +# Output: 3 +# +# Count of positive integers: 1 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 3 +# +# Example 3 +# Input: @ints = (1,2) +# Output: 2 +# +# Count of positive integers: 2 +# Count of negative integers: 0 +# Maximum of count of positive and negative integers: 2 + +defmodule MaximumOfPositiveAndNegative do + + def positive(ints) do + ints + |> Enum.count(fn x -> x > 0 end) + end + + def negative(ints) do + ints + |> Enum.count(fn x -> x < 0 end) + end + + def out(ints) do + IO.write( "(" <> Enum.join(ints, ",") <> ") -> ") + IO.puts( max(positive(ints), negative(ints)) ) + end +end + +ints = [-3, 1, 2, -1, 3, -2, 4] +MaximumOfPositiveAndNegative.out(ints) + +ints = [-1, -2, -3, 1] +MaximumOfPositiveAndNegative.out(ints) + +ints = [1,2] +MaximumOfPositiveAndNegative.out(ints) diff --git a/challenge-262/spadacciniweb/go/ch-1.go b/challenge-262/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..de672bf423 --- /dev/null +++ b/challenge-262/spadacciniweb/go/ch-1.go @@ -0,0 +1,72 @@ +/* +Task 1: Max Positive Negative +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints. +Write a script to return the maximum number of either positive or negative integers in the given array. + +Example 1 +Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +Output: 4 + +Count of positive integers: 4 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 4 + +Example 2 +Input: @ints = (-1, -2, -3, 1) +Output: 3 + +Count of positive integers: 1 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 3 + +Example 3 +Input: @ints = (1,2) +Output: 2 + +Count of positive integers: 2 +Count of negative integers: 0 +Maximum of count of positive and negative integers: 2 +*/ + +package main + +import ( + "fmt" +) + +func maxInt(x, y int) int { + if x < y { + return y + } + return x +} + +func maximum_of_positive_and_negative(ints []int) { + positiveInt := 0 + negativeInt := 0 + for i := 0; i < len(ints); i++ { + if ints[i] > 0 { + positiveInt++ + } else if ints[i] < 0 { + negativeInt++ + } + } + + s := fmt.Sprint(ints) + fmt.Printf("%s -> %d\n", + s, + maxInt(positiveInt, negativeInt)) +} + +func main() { + ints := []int{-3, 1, 2, -1, 3, -2, 4} + maximum_of_positive_and_negative(ints) + + ints = []int{-1, -2, -3, 1} + maximum_of_positive_and_negative(ints) + + ints = []int{1, 2} + maximum_of_positive_and_negative(ints) +} diff --git a/challenge-262/spadacciniweb/perl/ch-1.pl b/challenge-262/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..00686acb40 --- /dev/null +++ b/challenge-262/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,59 @@ +#!/usr/bin/env perl + +# Task 1: Max Positive Negative +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# Write a script to return the maximum number of either positive or negative integers in the given array. +# +# Example 1 +# Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +# Output: 4 +# +# Count of positive integers: 4 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 4 +# +# Example 2 +# Input: @ints = (-1, -2, -3, 1) +# Output: 3 +# +# Count of positive integers: 1 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 3 +# +# Example 3 +# Input: @ints = (1,2) +# Output: 2 +# +# Count of positive integers: 2 +# Count of negative integers: 0 +# Maximum of count of positive and negative integers: 2 + +use strict; +use warnings; + +my @ints = (-3, 1, 2, -1, 3, -2, 4); +maximum_of_positive_and_negative(\@ints); + +@ints = (-1, -2, -3, 1); +maximum_of_positive_and_negative(\@ints); + +@ints = (1,2); +maximum_of_positive_and_negative(\@ints); + +exit 0; + +sub maximum_of_positive_and_negative { + my $ints = shift; + + my $positive = scalar map { $_ > 0 ? 1 : () } + @$ints; + my $negative = scalar map { $_ < 0 ? 1 : () } + @$ints; + printf "(%s) -> %s\n", + (join ',', @$ints), + $positive > $negative ? $positive : $negative; + + return undef; +} diff --git a/challenge-262/spadacciniweb/python/ch-1.py b/challenge-262/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..d9782dc03a --- /dev/null +++ b/challenge-262/spadacciniweb/python/ch-1.py @@ -0,0 +1,49 @@ +# Task 1: Max Positive Negative +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# Write a script to return the maximum number of either positive or negative integers in the given array. +# +# Example 1 +# Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +# Output: 4 +# +# Count of positive integers: 4 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 4 +# +# Example 2 +# Input: @ints = (-1, -2, -3, 1) +# Output: 3 +# +# Count of positive integers: 1 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 3 +# +# Example 3 +# Input: @ints = (1,2) +# Output: 2 +# +# Count of positive integers: 2 +# Count of negative integers: 0 +# Maximum of count of positive and negative integers: 2 + +def maximum_of_positive_and_negative(ints): + negative = len(list(filter(lambda x: (x < 0), ints))) + positive = len(list(filter(lambda x: (x > 0), ints))) + + print("(%s) -> %d" % + ( ",".join(map(str, ints)), + max(positive, negative) + ) + ) + +if __name__ == "__main__": + ints = [-3, 1, 2, -1, 3, -2, 4] + maximum_of_positive_and_negative(ints) + + ints = [-1, -2, -3, 1] + maximum_of_positive_and_negative(ints) + + ints = [1,2] + maximum_of_positive_and_negative(ints) diff --git a/challenge-262/spadacciniweb/ruby/ch-1.rb b/challenge-262/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..d933ea2ec1 --- /dev/null +++ b/challenge-262/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,47 @@ +# Task 1: Max Positive Negative +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# Write a script to return the maximum number of either positive or negative integers in the given array. +# +# Example 1 +# Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +# Output: 4 +# +# Count of positive integers: 4 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 4 +# +# Example 2 +# Input: @ints = (-1, -2, -3, 1) +# Output: 3 +# +# Count of positive integers: 1 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 3 +# +# Example 3 +# Input: @ints = (1,2) +# Output: 2 +# +# Count of positive integers: 2 +# Count of negative integers: 0 +# Maximum of count of positive and negative integers: 2 + +def maximum_of_positive_and_negative(ints) + positive = ints.select {|i| i > 0}.size + negative = ints.select {|i| i < 0}.size + + printf "(%s) -> %s\n", + ints.join(","), + [positive,negative].max +end + +ints = [-3, 1, 2, -1, 3, -2, 4] +maximum_of_positive_and_negative(ints) + +ints = [-1, -2, -3, 1] +maximum_of_positive_and_negative(ints) + +ints = [1,2] +maximum_of_positive_and_negative(ints) |
