From aecf51b43cf11e2d592fdfa86282d642e716d9a8 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Thu, 15 Dec 2022 10:13:05 +0330 Subject: TWC195 --- challenge-195/deadmarshal/d/ch1.d | 27 +++++++++++++++++++++++++++ challenge-195/deadmarshal/d/ch2.d | 24 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 challenge-195/deadmarshal/d/ch1.d create mode 100644 challenge-195/deadmarshal/d/ch2.d (limited to 'challenge-195/deadmarshal/d') diff --git a/challenge-195/deadmarshal/d/ch1.d b/challenge-195/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..b1c6252960 --- /dev/null +++ b/challenge-195/deadmarshal/d/ch1.d @@ -0,0 +1,27 @@ +import std.stdio:writeln; + +bool has_dups(int n) +{ + int[int] hash; + while(n) + { + if(hash.require(n%10,0)) return true; + hash[n%10] = 1; + n /= 10; + } + return false; +} + +int special_integers(int n) +{ + int count = 0; + for(int i = 1; i <= n; ++i) if(!has_dups(i)) count++; + return count; +} + +void main() +{ + writeln(special_integers(15)); + writeln(special_integers(35)); +} + diff --git a/challenge-195/deadmarshal/d/ch2.d b/challenge-195/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..b80f690abc --- /dev/null +++ b/challenge-195/deadmarshal/d/ch2.d @@ -0,0 +1,24 @@ +import std.stdio:writeln; +import std.algorithm:all,filter,reduce,uniq,minElement,sort; +import std.array:array; + +int most_frequent_even(int[] arr) +{ + if(all!"a & 1"(arr)) return -1; + int[int] hash; + foreach(int i; filter!(a => !(a & 1))(arr)) if(!(i & 1)) hash[i]++; + if(hash.values.length == hash.values.uniq.array.length) + return hash.keys.minElement; + return reduce!((a,b) => hash[a] == hash[b] ? a : b)(hash.keys.sort); +} + +void main() +{ + int[] a1 = [1,1,2,6,2]; + int[] a2 = [1,3,5,7]; + int[] a3 = [6,4,4,6,1]; + writeln(most_frequent_even(a1)); + writeln(most_frequent_even(a2)); + writeln(most_frequent_even(a3)); +} + -- cgit