diff options
| author | Andrew Shitov <andy@shitov.ru> | 2020-08-19 00:15:41 +0200 |
|---|---|---|
| committer | Andrew Shitov <andy@shitov.ru> | 2020-08-19 00:15:41 +0200 |
| commit | 45d1e1e55de48f5316915fe3de2a0faeaaf6ca11 (patch) | |
| tree | d29c74c163f44816e0493e5a2ca381975a55a624 | |
| parent | 942464d5fc6d400791ec6e45a192032518fdb99f (diff) | |
| download | perlweeklychallenge-club-45d1e1e55de48f5316915fe3de2a0faeaaf6ca11.tar.gz perlweeklychallenge-club-45d1e1e55de48f5316915fe3de2a0faeaaf6ca11.tar.bz2 perlweeklychallenge-club-45d1e1e55de48f5316915fe3de2a0faeaaf6ca11.zip | |
ash 074
| -rw-r--r-- | challenge-074/ash/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-074/ash/raku/ch-1.cpp | 33 | ||||
| -rw-r--r-- | challenge-074/ash/raku/ch-1.raku | 21 | ||||
| -rw-r--r-- | challenge-074/ash/raku/ch-2.raku | 16 |
4 files changed, 71 insertions, 0 deletions
diff --git a/challenge-074/ash/blog.txt b/challenge-074/ash/blog.txt new file mode 100644 index 0000000000..71f01a8a9c --- /dev/null +++ b/challenge-074/ash/blog.txt @@ -0,0 +1 @@ +https://andrewshitov.com/2020/08/18/the-weekly-challenge-for-74/ diff --git a/challenge-074/ash/raku/ch-1.cpp b/challenge-074/ash/raku/ch-1.cpp new file mode 100644 index 0000000000..74d78a32c3 --- /dev/null +++ b/challenge-074/ash/raku/ch-1.cpp @@ -0,0 +1,33 @@ +/* + Task 1 from + https://perlweeklychallenge.org/blog/perl-weekly-challenge-074/ + + Comments: https://andrewshitov.com/2020/08/18/the-weekly-challenge-for-74/ + + Compile as: + $ g++ --std=c++17 ch-1.cpp +*/ + +#include <iostream> +#include <vector> +#include <map> +#include <algorithm> + +using namespace std; + +int main() { + vector<int> data = {1, 2, 2, 3, 2, 4, 2}; + // vector<int> data = {1, 3, 1, 2, 4, 5}; + + map<int, int> frequency; + int max_frequency = 0; + int major = 0; + for (auto x : data) { + if (++frequency[x] > max_frequency) { + max_frequency = frequency[x]; + major = x; + } + } + + cout << (max_frequency > data.size() / 2 ? major : -1) << endl; +} diff --git a/challenge-074/ash/raku/ch-1.raku b/challenge-074/ash/raku/ch-1.raku new file mode 100644 index 0000000000..b06b692aea --- /dev/null +++ b/challenge-074/ash/raku/ch-1.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku + +# Task 1 from +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-074/ + +# Comments: https://andrewshitov.com/2020/08/18/the-weekly-challenge-for-74/ + +my @a = 1, 2, 2, 3, 2, 4, 2; +# my @a = 1, 3, 1, 2, 4, 5; + +my $most-frequent = (@a.Bag.sort: -*.value)[0]; + +say $most-frequent.value > @a.elems / 2 ?? $most-frequent.key !! -1; + +# Output: +# $ raku ch-1.raku +# 2 +# +# For the second @a: +# $ raku ch-1.raku +# -1 diff --git a/challenge-074/ash/raku/ch-2.raku b/challenge-074/ash/raku/ch-2.raku new file mode 100644 index 0000000000..f1159dfebb --- /dev/null +++ b/challenge-074/ash/raku/ch-2.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku + +# Task 2 from +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-074/ + +# Comments: https://andrewshitov.com/2020/08/18/the-weekly-challenge-for-74/ + +my $s = 'ababc'; +# my $s = 'xxyyzzyx'; + +for 1..$s.chars -> $pos { + my $substr = $s.substr(0, $pos).join; + print "In '$substr': "; + my $b = bag $substr.comb; + say $substr.comb.reverse.first({$b{$_} == 1}) // '#'; +} |
