diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-01 17:52:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-01 17:52:03 +0100 |
| commit | 544a134ff3b3251cfd3284fbf99cbfacaec5f80f (patch) | |
| tree | aeeb013f1da03244b36542a23f745a2f53de9e66 | |
| parent | c866688956ed8d39e4bf4a136c85d1244105a567 (diff) | |
| parent | a4a5c909234335cbf64764b067d3d0f23829f8fc (diff) | |
| download | perlweeklychallenge-club-544a134ff3b3251cfd3284fbf99cbfacaec5f80f.tar.gz perlweeklychallenge-club-544a134ff3b3251cfd3284fbf99cbfacaec5f80f.tar.bz2 perlweeklychallenge-club-544a134ff3b3251cfd3284fbf99cbfacaec5f80f.zip | |
Merge pull request #9849 from ash/ash-263
Week 263 Task 1 by ash
| -rw-r--r-- | challenge-263/ash/cpp/ch-1.cpp | 48 | ||||
| -rw-r--r-- | challenge-263/ash/go/ch-1.go | 36 | ||||
| -rw-r--r-- | challenge-263/ash/java/ch-1.java | 42 | ||||
| -rw-r--r-- | challenge-263/ash/javascript/ch-1.js | 32 | ||||
| -rw-r--r-- | challenge-263/ash/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-263/ash/python/ch-1.py | 19 | ||||
| -rw-r--r-- | challenge-263/ash/raku/ch-1.raku | 22 | ||||
| -rw-r--r-- | challenge-263/ash/typescript/ch-1.ts | 33 |
8 files changed, 266 insertions, 0 deletions
diff --git a/challenge-263/ash/cpp/ch-1.cpp b/challenge-263/ash/cpp/ch-1.cpp new file mode 100644 index 0000000000..463f80aa43 --- /dev/null +++ b/challenge-263/ash/cpp/ch-1.cpp @@ -0,0 +1,48 @@ +// Solution of Task 1 of The Weekly Challenge 263 +// https://theweeklychallenge.org/blog/perl-weekly-challenge-263/ + +/* + $ c++ -std=c++20 ch-1.cpp + + $ ./a.out + (1, 2) + () + (4) +*/ + +#include<iostream> +#include<vector> +#include<algorithm> +#include<string> + +using namespace std; + +int main() { + vector<vector<int>> tests = { + {1, 5, 3, 2, 4, 2}, + {1, 2, 4, 3, 5}, + {5, 3, 2, 4, 2, 1} + }; + + int values[] = {2, 6, 4}; + + for (int n = 0; n != tests.size(); n++) { + auto vec = tests[n]; + auto value = values[n]; + + sort(vec.begin(), vec.end()); + + vector<int> result; + for (int c = 0; c != vec.size(); c++) { + if (vec[c] == value) result.push_back(c); + if (vec[c] > value) break; + } + + cout << "("; + for (int c = 0; c != result.size(); c++) { + cout << result[c]; + if (c < result.size() - 1) cout << ", "; + } + cout << ")" << endl; + } +} diff --git a/challenge-263/ash/go/ch-1.go b/challenge-263/ash/go/ch-1.go new file mode 100644 index 0000000000..eefc2fa774 --- /dev/null +++ b/challenge-263/ash/go/ch-1.go @@ -0,0 +1,36 @@ +// Solution of Task 1 of The Weekly Challenge 263 +// https://theweeklychallenge.org/blog/perl-weekly-challenge-263/ + +/* + $ go run ch-1.go + [1 2] + [] + [4] +*/ + +package main + +import ( + "fmt" + "sort" +) + +func main() { + tests := [][]int{ + {1, 5, 3, 2, 4, 2}, + {1, 2, 4, 3, 5}, + {5, 3, 2, 4, 2, 1}, + } + values := []int{2, 6, 4} + + for t, test := range tests { + sort.Ints(test) + var indices []int + for i, n := range test { + if n == values[t] { + indices = append(indices, i) + } + } + fmt.Println(indices) + } +} diff --git a/challenge-263/ash/java/ch-1.java b/challenge-263/ash/java/ch-1.java new file mode 100644 index 0000000000..fe856b3e07 --- /dev/null +++ b/challenge-263/ash/java/ch-1.java @@ -0,0 +1,42 @@ +// Solution of Task 1 of The Weekly Challenge 263 +// https://theweeklychallenge.org/blog/perl-weekly-challenge-263/ + +/* +$ java ch-1.java +[1, 2] +[] +[4] +*/ + +import java.util.Arrays; +import java.util.ArrayList; + +public class Main { + public static void main(String[] args) { + int[][] tests = {{1, 5, 3, 2, 4, 2}, + {1, 2, 4, 3, 5}, + {5, 3, 2, 4, 2, 1}}; + int[] values = {2, 6, 4}; + + for (int c = 0; c != tests.length; c++) { + int[] data = tests[c]; + int value = values[c]; + + ArrayList<Integer> indices = solve(data, value); + System.out.println(indices); + } + } + + static ArrayList<Integer> solve(int[] data, int value) { + Arrays.sort(data); + + ArrayList<Integer> indices = new ArrayList<Integer>(); + for (int c = 0; c != data.length; c++) { + int current = data[c]; + if (current == value) indices.add(c); + if (current > value) break; + } + + return indices; + } +} diff --git a/challenge-263/ash/javascript/ch-1.js b/challenge-263/ash/javascript/ch-1.js new file mode 100644 index 0000000000..40115ccc9a --- /dev/null +++ b/challenge-263/ash/javascript/ch-1.js @@ -0,0 +1,32 @@ +// Solution of Task 1 of The Weekly Challenge 263 +// https://theweeklychallenge.org/blog/perl-weekly-challenge-263/ + +/* +$ node ch-1.js +[ 1, 2 ] +[] +[ 4 ] +*/ + +let tests = [ + [1, 5, 3, 2, 4, 2], + [1, 2, 4, 3, 5], + [5, 3, 2, 4, 2, 1] +]; +let values = [2, 6, 4]; + +for (let c = 0; c != tests.length; c++) { + console.log(solve(tests[c], values[c])); +} + +function solve(data, value) { + data.sort(); + + let indices = []; + for (let c = 0; c != data.length; c++) { + if (data[c] == value) indices.push(c); + if (data[c] > value) break; + } + + return indices; +} diff --git a/challenge-263/ash/perl/ch-1.pl b/challenge-263/ash/perl/ch-1.pl new file mode 100644 index 0000000000..9b71bb2fa0 --- /dev/null +++ b/challenge-263/ash/perl/ch-1.pl @@ -0,0 +1,34 @@ +# Solution to Task 1 of The Weekly Challenge 263 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/#TASK1 + +# $ perl ch-1.pl +# (1, 2) +# () +# (4) + +use v5.20; +use experimental 'signatures'; + +my $tests = [ + [[1, 5, 3, 2, 4, 2], 2], + [[1, 2, 4, 3, 5], 6], + [[5, 3, 2, 4, 2, 1], 4] +]; + +for my $test (@$tests) { + my $result = solve($test->[0], $test->[1]); + say '(' . join(', ', @$result) . ')'; +} + +sub solve($data, $value) { + my @sorted = sort(@$data); + + my @indices; + for (my $c = 0; $c < scalar @sorted; $c++) { + my $current = $sorted[$c]; + push @indices, $c if $current == $value; + last if $current > $value; + } + + return \@indices; +} diff --git a/challenge-263/ash/python/ch-1.py b/challenge-263/ash/python/ch-1.py new file mode 100644 index 0000000000..945e73a9c1 --- /dev/null +++ b/challenge-263/ash/python/ch-1.py @@ -0,0 +1,19 @@ +# Solution of Task 1 of The Weekly Challenge 263 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-263 + +# $ python3 ch-1.py +# [1, 2] +# [] +# [4] + +tests = ((1, 5, 3, 2, 4, 2), (1, 2, 4, 3, 5), (5, 3, 2, 4, 2, 1)) +values = (2, 6, 4) + +def solve(test, value): + print( + [i for i, val in enumerate(sorted(test)) if val == value] + ) + +for c in range(0, len(tests)): + solve(tests[c], values[c]) + diff --git a/challenge-263/ash/raku/ch-1.raku b/challenge-263/ash/raku/ch-1.raku new file mode 100644 index 0000000000..aa9f22bb37 --- /dev/null +++ b/challenge-263/ash/raku/ch-1.raku @@ -0,0 +1,22 @@ +# Solution to Task 1 of The Weekly Challenge 263 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/#TASK1 + +# Test run: +# $ raku ch-1.raku +# (1 2) +# () +# (4) + +my @tests = + ((1, 5, 3, 2, 4, 2), 2), + ((1, 2, 4, 3, 5), 6), + ((5, 3, 2, 4, 2, 1), 4); + +for @tests -> @test { + say solve(@test[0], @test[1]); +} + +sub solve(@data, $value) { + my @sorted = @data.sort; + return @sorted.grep($value, :k); +} diff --git a/challenge-263/ash/typescript/ch-1.ts b/challenge-263/ash/typescript/ch-1.ts new file mode 100644 index 0000000000..e38a2a3d68 --- /dev/null +++ b/challenge-263/ash/typescript/ch-1.ts @@ -0,0 +1,33 @@ +// Solution of Task 1 of The Weekly Challenge 263 +// https://theweeklychallenge.org/blog/perl-weekly-challenge-263/ + +/* +$ tsc ch-1.ts +$ node ch-1.js +[ 1, 2 ] +[] +[ 4 ] +*/ + +const tests = [ + [1, 5, 3, 2, 4, 2], + [1, 2, 4, 3, 5], + [5, 3, 2, 4, 2, 1] +]; +const values = [2, 6, 4]; + +for (let c = 0; c != tests.length; c++) { + console.log(solve(tests[c], values[c])); +} + +function solve(data: number[], value: number) { + data.sort(); + + let indices: number[] = []; + for (let c = 0; c != data.length; c++) { + if (data[c] == value) indices.push(c); + if (data[c] > value) break; + } + + return indices; +} |
