diff options
| author | Archargelod <archargelod@gmail.com> | 2024-02-21 01:33:32 +0800 |
|---|---|---|
| committer | Archargelod <archargelod@gmail.com> | 2024-02-21 01:33:32 +0800 |
| commit | 77f7f91c73773aa6cfd2713dea32c705a1971b58 (patch) | |
| tree | 368b987ea819a1083930390e2cef19151827c4d6 /challenge-011 | |
| parent | ddd42e0db3017a5ec3108d09efba477f19e7f04b (diff) | |
| download | perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.tar.gz perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.tar.bz2 perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.zip | |
Weeks 5-13, 257 in Nim
Diffstat (limited to 'challenge-011')
| -rw-r--r-- | challenge-011/archargelod/README | 1 | ||||
| -rwxr-xr-x | challenge-011/archargelod/nim/ch_1.nim | 24 | ||||
| -rwxr-xr-x | challenge-011/archargelod/nim/ch_2.nim | 25 |
3 files changed, 50 insertions, 0 deletions
diff --git a/challenge-011/archargelod/README b/challenge-011/archargelod/README new file mode 100644 index 0000000000..6cd57e1074 --- /dev/null +++ b/challenge-011/archargelod/README @@ -0,0 +1 @@ +Solution by archargelod diff --git a/challenge-011/archargelod/nim/ch_1.nim b/challenge-011/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..b7514dd648 --- /dev/null +++ b/challenge-011/archargelod/nim/ch_1.nim @@ -0,0 +1,24 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/math + +const + CelsiusFreezing = 0 + CelsiusBoiling = 100 + FahrenheitFreezing = 32 + FahrenheitBoiling = 212 + +proc toFahrenheit(t: int): int = + let slope = + (FahrenheitBoiling - FahrenheitFreezing) / (CelsiusBoiling - CelsiusFreezing) + int round(FahrenheitFreezing.float + slope * float(t - CelsiusFreezing)) + +proc equalPoint(): int = + while result.toFahrenheit() > result: + dec result + +when isMainModule: + import std/unittest + + suite "Equal point in Fahrenheit and Celsius": + test "-40 is the equal point": + check equalPoint() == -40 diff --git a/challenge-011/archargelod/nim/ch_2.nim b/challenge-011/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..6865bd40f5 --- /dev/null +++ b/challenge-011/archargelod/nim/ch_2.nim @@ -0,0 +1,25 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/[sequtils] + +type Matrix = seq[seq[int]] + +proc identityMatrix*(size: Positive): Matrix = + result = newSeqWith[int](size, newSeq[int](size)) + for i in 0 ..< size: + result[i][i] = 1 + +when isMainModule: + import std/unittest + + suite "Identity matrix of given size": + test "2x2 matrix": + check identityMatrix(2) == [@[1, 0], @[0, 1]] + test "6x6 matrix": + check identityMatrix(6) == [ + @[1, 0, 0, 0, 0, 0], + @[0, 1, 0, 0, 0, 0], + @[0, 0, 1, 0, 0, 0], + @[0, 0, 0, 1, 0, 0], + @[0, 0, 0, 0, 1, 0], + @[0, 0, 0, 0, 0, 1] + ] |
