diff options
| author | Archargelod <archargelod@gmail.com> | 2024-03-01 14:43:05 +0800 |
|---|---|---|
| committer | Archargelod <archargelod@gmail.com> | 2024-03-01 14:43:05 +0800 |
| commit | 534715243e65728db4ee90b5ee5dee644ce71dac (patch) | |
| tree | fe17b389593130a0961afea924afc8e180fb365a /challenge-026 | |
| parent | d64cde6fdb1521b4a240e30615a90febcaf46799 (diff) | |
| download | perlweeklychallenge-club-534715243e65728db4ee90b5ee5dee644ce71dac.tar.gz perlweeklychallenge-club-534715243e65728db4ee90b5ee5dee644ce71dac.tar.bz2 perlweeklychallenge-club-534715243e65728db4ee90b5ee5dee644ce71dac.zip | |
weeks 14-26, 258 in Nim
Diffstat (limited to 'challenge-026')
| -rw-r--r-- | challenge-026/archargelod/README | 1 | ||||
| -rwxr-xr-x | challenge-026/archargelod/nim/ch_1.nim | 26 | ||||
| -rwxr-xr-x | challenge-026/archargelod/nim/ch_2.nim | 23 |
3 files changed, 50 insertions, 0 deletions
diff --git a/challenge-026/archargelod/README b/challenge-026/archargelod/README new file mode 100644 index 0000000000..6cd57e1074 --- /dev/null +++ b/challenge-026/archargelod/README @@ -0,0 +1 @@ +Solution by archargelod diff --git a/challenge-026/archargelod/nim/ch_1.nim b/challenge-026/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..807a9b847b --- /dev/null +++ b/challenge-026/archargelod/nim/ch_1.nim @@ -0,0 +1,26 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/[strutils] + +proc countLetters(input, alphabet: string): int = + let uniq = block: + var tmp: set[char] + for c in alphabet: + if c notin Letters: continue + tmp.incl c + tmp + + for c in input: + if c in uniq: + inc result + +when isMainModule: + import std/unittest + + const + Test = "chancellor" + Alphabet = "chocolate" + Expected = 8 + + suite "Stones and jewels alphabets": + test "Example 1": + check Test.countLetters(Alphabet) == Expected diff --git a/challenge-026/archargelod/nim/ch_2.nim b/challenge-026/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..cd7c76908f --- /dev/null +++ b/challenge-026/archargelod/nim/ch_2.nim @@ -0,0 +1,23 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/[math, sequtils] + +proc circularMean(degrees: varargs[float, toFloat]): float = + let radians = degrees.mapIt(it.degToRad) + + let sinSum = radians.mapIt(it.sin).sum() + let cosSum = radians.mapIt(it.cos).sum() + + arctan2(sinSum, cosSum).radToDeg.euclMod(360) + +when isMainModule: + import std/unittest + + const + Test = [@[180.0, 200.0], @[355.0, 5.0, 15.0]] + Expected = [190.0, 5.0] + + suite "Mean angles": + test "180, 200 -> 190": + check almostEqual(Test[0].circularMean(), Expected[0]) + test "355, 5, 15 -> 5": + check almostEqual(Test[1].circularMean(), Expected[1]) |
