diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2022-12-15 10:13:05 +0330 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2022-12-15 10:13:05 +0330 |
| commit | aecf51b43cf11e2d592fdfa86282d642e716d9a8 (patch) | |
| tree | f889a3d22fafcf14022690432b43be08bee43b3c /challenge-195/deadmarshal/d | |
| parent | c1d3932971f399789d4ee01cb886bb1e984f2563 (diff) | |
| download | perlweeklychallenge-club-aecf51b43cf11e2d592fdfa86282d642e716d9a8.tar.gz perlweeklychallenge-club-aecf51b43cf11e2d592fdfa86282d642e716d9a8.tar.bz2 perlweeklychallenge-club-aecf51b43cf11e2d592fdfa86282d642e716d9a8.zip | |
TWC195
Diffstat (limited to 'challenge-195/deadmarshal/d')
| -rw-r--r-- | challenge-195/deadmarshal/d/ch1.d | 27 | ||||
| -rw-r--r-- | challenge-195/deadmarshal/d/ch2.d | 24 |
2 files changed, 51 insertions, 0 deletions
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)); +} + |
