diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-24 12:21:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-24 12:21:34 +0000 |
| commit | 9f72369040d8a2ae950fa919effc4cc5bff95c61 (patch) | |
| tree | 76b06c7ea8ac9eab656fb7ed2eaedb10394e93f9 /challenge-257 | |
| parent | da664c025eeb01397e74c61b2af119f1fe1e68e7 (diff) | |
| parent | 316a806eeee906c5733e7ced1f9f81f0957affa3 (diff) | |
| download | perlweeklychallenge-club-9f72369040d8a2ae950fa919effc4cc5bff95c61.tar.gz perlweeklychallenge-club-9f72369040d8a2ae950fa919effc4cc5bff95c61.tar.bz2 perlweeklychallenge-club-9f72369040d8a2ae950fa919effc4cc5bff95c61.zip | |
Merge pull request #9630 from spadacciniweb/PWC-257
Pwc 257
Diffstat (limited to 'challenge-257')
| -rw-r--r-- | challenge-257/spadacciniweb/elixir/ch-1.exs | 54 | ||||
| -rw-r--r-- | challenge-257/spadacciniweb/go/ch-1.go | 2 |
2 files changed, 55 insertions, 1 deletions
diff --git a/challenge-257/spadacciniweb/elixir/ch-1.exs b/challenge-257/spadacciniweb/elixir/ch-1.exs new file mode 100644 index 0000000000..3c795a53bc --- /dev/null +++ b/challenge-257/spadacciniweb/elixir/ch-1.exs @@ -0,0 +1,54 @@ +# Task 1: Smaller than Current +# Submitted by: Mohammad Sajid Anwar +# +# You are given a array of integers, @ints. +# Write a script to find out how many integers are smaller than current i.e. foreach ints[i], count ints[j] < ints[i] where i != j. +# +# Example 1 +# Input: @ints = (5, 2, 1, 6) +# Output: (2, 1, 0, 3) +# +# For $ints[0] = 5, there are two integers (2,1) smaller than 5. +# For $ints[1] = 2, there is one integer (1) smaller than 2. +# For $ints[2] = 1, there is none integer smaller than 1. +# For $ints[3] = 6, there are three integers (5,2,1) smaller than 6. +# +# Example 2 +# Input: @ints = (1, 2, 0, 3) +# Output: (1, 2, 0, 3) +# +# Example 3 +# Input: @ints = (0, 1) +# Output: (0, 1) +# +# Example 4 +# Input: @ints = (9, 4, 9, 2) +# Output: (2, 1, 2, 0) +# + +defmodule Smaller do + def current(ints) do + Enum.map(ints, fn x -> smaller_than(x, ints) end) + end + def smaller_than(i, ints) do + ints + |> Stream.filter(fn x -> x < i end) + |> Enum.count() + end + def out(ints) do + IO.write( "(" <> Enum.join(ints, ", ") <> ") -> ") + IO.puts( "(" <> Enum.join( current(ints), ", ") <> ")" ) + end +end + +ints = [5, 2, 1, 6] +Smaller.out(ints) + +ints = [1, 2, 0, 3] +Smaller.out(ints) + +ints = [0, 1] +Smaller.out(ints) + +ints = [9, 4, 9, 2] +Smaller.out(ints) diff --git a/challenge-257/spadacciniweb/go/ch-1.go b/challenge-257/spadacciniweb/go/ch-1.go index 8d6dee9dd7..479bbda5f5 100644 --- a/challenge-257/spadacciniweb/go/ch-1.go +++ b/challenge-257/spadacciniweb/go/ch-1.go @@ -48,7 +48,7 @@ func smaller_than_current(arrInts []int) { } func main() { - arrInts := []int{2, 1, 0, 3} + arrInts := []int{5, 2, 1, 6} smaller_than_current(arrInts) arrInts = []int{1, 2, 0, 3} |
