diff options
| -rw-r--r-- | challenge-213/spadacciniweb/go/ch-1.go | 59 | ||||
| -rw-r--r-- | challenge-213/spadacciniweb/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-213/spadacciniweb/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-213/spadacciniweb/ruby/ch-1.rb | 34 |
4 files changed, 158 insertions, 0 deletions
diff --git a/challenge-213/spadacciniweb/go/ch-1.go b/challenge-213/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..f98d7545e0 --- /dev/null +++ b/challenge-213/spadacciniweb/go/ch-1.go @@ -0,0 +1,59 @@ +/* +Task 1: Fun Sort +Submitted by: Mohammad S Anwar + +You are given a list of positive integers. +Write a script to sort the all even integers first then all odds in ascending order. + +Example 1 +Input: @list = (1,2,3,4,5,6) +Output: (2,4,6,1,3,5) + +Example 2 +Input: @list = (1,2) +Output: (2,1) + +Example 3 +Input: @list = (1) +Output: (1) +*/ + +package main + +import ( + "fmt" + "log" + "os" + "strconv" + "sort" +) + +func main() { + arrStr := os.Args[1:] + if (len(arrStr) < 1) { + log.Fatal("input error") + } + + arrInt := make([]int, 0) + for i := 0; i <= len(arrStr)-1; i++ { + value, err := strconv.Atoi(arrStr[i]) + if (err != nil) { + log.Fatal(err) + } + arrInt = append(arrInt, value) + } + + listFun_even := make([]int, 0) + listFun_odd := make([]int, 0) + for i := 0; i <= len(arrInt)-1; i++ { + if (arrInt[i] % 2 == 0) { + listFun_even = append(listFun_even, arrInt[i]) + } else { + listFun_odd = append(listFun_odd, arrInt[i]) + } + } + sort.Sort(sort.Reverse(sort.IntSlice(listFun_even))) + sort.Sort(sort.Reverse(sort.IntSlice(listFun_odd))) + listFun := append(listFun_even, listFun_odd...) + fmt.Printf("Output: %v\n",listFun) +} diff --git a/challenge-213/spadacciniweb/perl/ch-1.pl b/challenge-213/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..c9ece72839 --- /dev/null +++ b/challenge-213/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +# Task 1: Fun Sort +# Submitted by: Mohammad S Anwar +# +# You are given a list of positive integers. +# Write a script to sort the all even integers first then all odds in ascending order. +# +# Example 1 +# Input: @list = (1,2,3,4,5,6) +# Output: (2,4,6,1,3,5) +# +# Example 2 +# Input: @list = (1,2) +# Output: (2,1) +# +# Example 3 +# Input: @list = (1) +# Output: (1) + +use strict; +use warnings; + +my @input = @ARGV; +die "Input error\n" + if scalar @input < 1 + or + (scalar map { $_ =~ /\D/ ? 1 : () } + @input) != 0; + +my @list = sort { $b <=> $a } map { $_ % 2 == 0 ? $_ : () } @input; +push @list, sort { $b <=> $a } map { $_ % 2 ? $_ : () } @input; +printf "(%s)\n", join ',', @list; diff --git a/challenge-213/spadacciniweb/python/ch-1.py b/challenge-213/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..1d260a625a --- /dev/null +++ b/challenge-213/spadacciniweb/python/ch-1.py @@ -0,0 +1,32 @@ +# Task 1: Fun Sort +# Submitted by: Mohammad S Anwar +# +# You are given a list of positive integers. +# Write a script to sort the all even integers first then all odds in ascending order. +# +# Example 1 +# Input: @list = (1,2,3,4,5,6) +# Output: (2,4,6,1,3,5) +# +# Example 2 +# Input: @list = (1,2) +# Output: (2,1) +# +# Example 3 +# Input: @list = (1) +# Output: (1) + +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") + + input = list(map(int, input)) + fun_list = sorted( list(filter(lambda x: x % 2 == 0, input)), reverse=True ) + \ + sorted( list(filter(lambda x: x % 2, input)), reverse=True) + print(list(fun_list)) diff --git a/challenge-213/spadacciniweb/ruby/ch-1.rb b/challenge-213/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..7db88a90f2 --- /dev/null +++ b/challenge-213/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,34 @@ +# Task 1: Fun Sort +# Submitted by: Mohammad S Anwar +# +# You are given a list of positive integers. +# Write a script to sort the all even integers first then all odds in ascending order. +# +# Example 1 +# Input: @list = (1,2,3,4,5,6) +# Output: (2,4,6,1,3,5) +# +# Example 2 +# Input: @list = (1,2) +# Output: (2,1) +# +# Example 3 +# Input: @list = (1) +# Output: (1) + +def main(input) + if input.length < 1 \ + or \ + input.select{ |i| i[/\D/] }.length > 0 + then + puts "Input error" + exit(0) + end + + list_fun = input.select{ |x| x.to_i.even? }.reverse + list_fun.concat( input.select{ |x| x.to_i.odd? }.reverse ) + print list_fun + +end + +main ARGV |
