diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-31 22:08:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-31 22:08:24 +0100 |
| commit | d970298a86e38dbb33534999ec305e0075baa67a (patch) | |
| tree | 8cae3bcf9a2b1fc85a90932e7bdc1551308e74cd | |
| parent | 3ce5f132262d3ddc09e66beab26cfcada9f9bde6 (diff) | |
| parent | cdeed4dde541c0949858e9314329c83b4283dc34 (diff) | |
| download | perlweeklychallenge-club-d970298a86e38dbb33534999ec305e0075baa67a.tar.gz perlweeklychallenge-club-d970298a86e38dbb33534999ec305e0075baa67a.tar.bz2 perlweeklychallenge-club-d970298a86e38dbb33534999ec305e0075baa67a.zip | |
Merge pull request #8167 from spadacciniweb/PWC-219
Task 1
| -rw-r--r-- | challenge-219/spadacciniweb/go/ch-1.go | 51 | ||||
| -rw-r--r-- | challenge-219/spadacciniweb/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-219/spadacciniweb/python/ch-1.py | 27 | ||||
| -rw-r--r-- | challenge-219/spadacciniweb/ruby/ch-1.rb | 31 |
4 files changed, 138 insertions, 0 deletions
diff --git a/challenge-219/spadacciniweb/go/ch-1.go b/challenge-219/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..d675a9917e --- /dev/null +++ b/challenge-219/spadacciniweb/go/ch-1.go @@ -0,0 +1,51 @@ +/* +Task 1: Sorted Squares +Submitted by: Mohammad S Anwar + +You are given a list of numbers. + +Write a script to square each number in the list and return the sorted list, increasing order. +Example 1 + +Input: @list = (-2, -1, 0, 3, 4) +Output: (0, 1, 4, 9, 16) + +Example 2 + +Input: @list = (5, -4, -1, 3, 6) +Output: (1, 9, 16, 25, 36) +*/ + +package main + +import ( + "fmt" + "log" + "os" + "strconv" + "math" + "sort" +) + +func main() { + arrStr := os.Args[1:] + if (len(arrStr) < 1) { + log.Fatal("input error") + } + + arrFloat := make([]float64, 0) + for i := 0; i <= len(arrStr)-1; i++ { + value, err := strconv.ParseFloat(arrStr[i], 8) + if (err != nil) { + log.Fatal(err) + } + arrFloat = append(arrFloat, value) + } + + arrSquare := make([]int, 0) + for i := 0; i <= len(arrFloat)-1; i++ { + arrSquare = append(arrSquare, int(math.Pow(arrFloat[i], 2))) + } + sort.Sort(sort.IntSlice(arrSquare)) + fmt.Printf("Output: %v\n",arrSquare) +} diff --git a/challenge-219/spadacciniweb/perl/ch-1.pl b/challenge-219/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..677919bc38 --- /dev/null +++ b/challenge-219/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +# Task 1: Sorted Squares +# Submitted by: Mohammad S Anwar +# +# You are given a list of numbers. +# +# Write a script to square each number in the list and return the sorted list, increasing order. +# Example 1 +# +# Input: @list = (-2, -1, 0, 3, 4) +# Output: (0, 1, 4, 9, 16) +# +# Example 2 +# +# Input: @list = (5, -4, -1, 3, 6) +# Output: (1, 9, 16, 25, 36) + +use strict; +use warnings; + +my @input = @ARGV; +die "Input error\n" + if scalar @input < 1 + or + (scalar map { $_ =~ /[\-\d]/ ? () : 1 } + @input) != 0; + +printf "(%s)\n", join ', ', sort { $a <=> $b } map { $_**2 } @input; diff --git a/challenge-219/spadacciniweb/python/ch-1.py b/challenge-219/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..726e633c6c --- /dev/null +++ b/challenge-219/spadacciniweb/python/ch-1.py @@ -0,0 +1,27 @@ +# Task 1: Sorted Squares +# Submitted by: Mohammad S Anwar +# +# You are given a list of numbers. +# +# Write a script to square each number in the list and return the sorted list, increasing order. +# Example 1 +# +# Input: @list = (-2, -1, 0, 3, 4) +# Output: (0, 1, 4, 9, 16) +# +# Example 2 +# +# Input: @list = (5, -4, -1, 3, 6) +# Output: (1, 9, 16, 25, 36) + +import re +import sys + +if __name__ == "__main__": + input = sys.argv[1:] + if (len(input) < 1 + or + len(list(filter(lambda x: re.search(r'[^\-\d]', x), input))) != 0): + sys.exit("Input error\n") + + print(sorted([int(i) ** 2 for i in input])) diff --git a/challenge-219/spadacciniweb/ruby/ch-1.rb b/challenge-219/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..1e82d6187f --- /dev/null +++ b/challenge-219/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,31 @@ +# Task 1: Sorted Squares +# Submitted by: Mohammad S Anwar +# +# You are given a list of numbers. +# +# Write a script to square each number in the list and return the sorted list, increasing order. +# Example 1 +# +# Input: @list = (-2, -1, 0, 3, 4) +# Output: (0, 1, 4, 9, 16) +# +# Example 2 +# +# Input: @list = (5, -4, -1, 3, 6) +# Output: (1, 9, 16, 25, 36) + +def main(input) + if input.length < 1 \ + or \ + input.select{ |i| i[/[^\-\d]/] }.length > 0 + then + puts "Input error" + exit(0) + end + + new_array = [] + input.each {|x| new_array << x.to_i**2} + print new_array.sort{ |a,b| a <=> b} +end + +main ARGV |
