Skip to content

Commit fac45ec

Browse files
Merge branch 'main' into bogo-sort
2 parents e6c80d5 + af31828 commit fac45ec

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

.github/workflows/build_directory_md.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ jobs:
1919
- uses: actions/checkout@v4
2020

2121
- uses: jiro4989/setup-nim-action@v2
22+
with:
23+
parent-nim-install-directory: ${{ runner.temp }}
2224

2325
- name: Build file
2426
run: |
25-
git clean -f -x -d
27+
git clean --force -x -d
2628
2729
# Compile the script first
2830
nim c -o:directory_script .scripts/directory.nim

.github/workflows/check_code_format.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ jobs:
1919
fetch-depth: 0
2020

2121
- uses: jiro4989/setup-nim-action@v2
22+
with:
23+
nim-version: stable
24+
repo-token: ${{ secrets.GITHUB_TOKEN }}
25+
parent-nim-install-directory: ${{ runner.temp }}
2226

2327
- name: Format code
2428
run: |

maths/haversine_distance.nim

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Haversine formula
2+
3+
import std/math
4+
5+
func haversineDistance(latitudeA, longitudeA, latitudeB,
6+
longitudeB: float): float =
7+
## returns the length of the shortest path connecting the input points on an unit sphere.
8+
## The input points are represented by their spherical/geographical coordinates.
9+
## The inputs are expected to be in radians.
10+
let
11+
dLatitude = latitudeB - latitudeA
12+
dLongitude = longitudeB - longitudeA
13+
a = sin(dLatitude / 2.0)^2 + cos(latitudeA) * cos(latitudeB) * sin(
14+
dLongitude / 2.0)^2
15+
2.0 * arcsin(sqrt(a))
16+
17+
when isMainModule:
18+
import std/[unittest, sequtils, strformat]
19+
suite "haversineDistance":
20+
const testCases = [
21+
(0.0, 0.0, 0.0, 0.0, 0.0),
22+
(0.0, 0.0, PI / 2.0, 0.0, PI / 2.0),
23+
(-PI / 2.0, 0.0, PI / 2.0, 0.0, PI),
24+
(0.0, 0.0, 0.0, PI / 2.0, PI / 2.0),
25+
(0.0, -PI / 2.0, 0.0, PI / 2.0, PI),
26+
(1.0, -PI / 2.0, -1.0, PI / 2.0, PI),
27+
(2.0, -PI / 2.0, -2.0, PI / 2.0, PI),
28+
(3.0, -PI / 2.0, -3.0, PI / 2.0, PI),
29+
(3.0, -PI / 2.0 + 0.5, -3.0, PI / 2.0 + 0.5, PI),
30+
(0.0, 0.0, 0.0, PI, PI),
31+
(PI / 2.0, 1.0, PI / 2.0, 2.0, 0.0),
32+
(-PI / 2.0, 1.0, -PI / 2.0, 2.0, 0.0),
33+
(0.0, 0.0, -PI / 4.0, 0.0, PI / 4.0),
34+
(0.0, 1.0, PI / 4.0, 1.0, PI / 4.0),
35+
(-PI / 2.0, 0.0, -PI / 4.0, 0.0, PI / 4.0),
36+
(-PI / 2.0, 0.0, -PI / 4.0, 0.6, PI / 4.0),
37+
(-PI / 2.0, 3.0, -PI / 4.0, 0.2, PI / 4.0),
38+
].mapIt:
39+
(id: fmt"posA=({it[0]}, {it[1]}), posB=({it[2]}, {it[3]})",
40+
latitudeA: it[0], longitudeA: it[1],
41+
latitudeB: it[2], longitudeB: it[3],
42+
expected: it[4])
43+
44+
func isClose(a, b: float): bool =
45+
return abs(a-b) < 0.0000001
46+
47+
for tc in testCases:
48+
test tc.id:
49+
checkpoint("returns expected result")
50+
check isClose(haversineDistance(tc.latitudeA, tc.longitudeA,
51+
tc.latitudeB, tc.longitudeB), tc.expected)
52+
check isClose(haversineDistance(tc.latitudeB, tc.longitudeB,
53+
tc.latitudeA, tc.longitudeA), tc.expected)

maths/modular_inverse.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ when isMainModule:
6767
check modularInverse(17, 17).is_none()
6868

6969
randomize()
70-
const randomTestSize = 1000
70+
const randomTestSize = 10
7171
for testNum in 0..randomTestSize:
7272
let a = rand(-10000000..10000000)
7373
let modulus = rand(1..1000000)

strings/check_anagram.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func toLowerUnchecked(c: char): char {.inline.} =
5959
# 0b100_0001, so setting the sixth bit to 1 gives letter's lowercase pair.
6060
char(uint8(c) or 0b0010_0000'u8)
6161

62-
template normalizeChar(c: char) =
62+
template normalizeChar(c: char): char =
6363
## Checks if the character is a letter and lowers its case.
6464
##
6565
## Raises a `ValueError` on other characters.

0 commit comments

Comments
 (0)