Skip to content

Commit 0f30aa9

Browse files
authored
chore: add nim_test.yml (#20)
* style: format code with nimpretty * fix: remove some errors * fix: follow spellSuggest * chore: add check_all_nim_files.sh * chore: add `nim_test.yml` * fix: use proper branch name * chore: use `config.nims` * chore: remove unused shell scripts * chore: do not use `--gc: arc` * chore: add `nim_test_cyclic.yml` * chore: move --gc: arc to nim.cfg * style: add missing blank line at the end * chore: add `macos-latest` and `windows-latest`
1 parent a2fc1bd commit 0f30aa9

File tree

6 files changed

+97
-25
lines changed

6 files changed

+97
-25
lines changed

.github/workflows/nim_test.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
name: nim_test
3+
4+
# yamllint disable-line rule:truthy
5+
on:
6+
workflow_dispatch:
7+
push:
8+
branches:
9+
- main
10+
pull_request:
11+
12+
jobs:
13+
nim_test:
14+
runs-on: ${{matrix.os}}
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
os:
19+
- ubuntu-latest
20+
- macos-latest
21+
- windows-latest
22+
nim-version:
23+
- '1.6.12'
24+
25+
steps:
26+
- uses: actions/checkout@v3
27+
28+
- uses: jiro4989/setup-nim-action@v1
29+
with:
30+
nim-version: ${{matrix.nim-version}}
31+
32+
- name: Build and test
33+
run: |
34+
nim test
35+
...
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
name: nim_test_cyclic
3+
4+
# yamllint disable-line rule:truthy
5+
on:
6+
workflow_dispatch:
7+
schedule:
8+
- cron: '3 1 * * 1'
9+
10+
jobs:
11+
nim_test:
12+
runs-on: ${{matrix.os}}
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
os:
17+
- ubuntu-latest
18+
nim-version:
19+
- 'stable'
20+
- 'devel'
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
25+
- uses: jiro4989/setup-nim-action@v1
26+
with:
27+
nim-version: ${{matrix.nim-version}}
28+
29+
- name: Display nim version
30+
run: |
31+
nim --version
32+
33+
- name: Build and test
34+
run: |
35+
nim test
36+
...

config.nims

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# Nim compilation defaults for the whole repository
2-
#--gc: arc
31
if defined(release) or defined(danger):
42
--opt: speed
53
--passC: "-flto"

dynamic_programming/catalan_numbers.nim

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Catalan Numbers
2-
#[
3-
The Catalan numbers are a sequence of natural numbers that occur in the
2+
#[
3+
The Catalan numbers are a sequence of natural numbers that occur in the
44
most large set of combinatorial problems.
55
For example, it describes:
66
- the number of ways to parenthesize a product of n factors
@@ -79,10 +79,11 @@ when isMainModule:
7979
const LowerLimit = 30 # The formulas involving a division overflows above 30
8080
const UpperLimit = 36 # Other methods overflow above 36
8181

82-
let expectedResult: seq[Positive] = @[1, 1, 2, 5, 14, 42, 132, 429,
82+
let expectedResult: seq[Positive] = @[Positive(1), 1, 2, 5, 14, 42, 132, 429,
8383
1430, 4862, 16796, 58786, 208012,
8484
742900, 2674440, 9694845]
85-
const CatalanNumbersList = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796,
85+
const CatalanNumbersList: seq[Positive] = @[Positive(1), 1, 2, 5, 14, 42, 132,
86+
429, 1430, 4862, 16796,
8687
58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700,
8788
1767263190, 6564120420, 24466267020, 91482563640, 343059613650,
8889
1289904147324, 4861946401452, 18367353072152, 69533550916004,
@@ -113,7 +114,7 @@ when isMainModule:
113114
test "We can compute up to the thirty-seventh Catalan number iteratively":
114115
let limit = UpperLimit
115116
let catalanNumbersSeq = catalanNumbers(limit)
116-
for index in 0 .. limit:
117+
for index in 0 ..< limit:
117118
check catalanNumbersSeq[index] == CatalanNumbersList[index]
118119

119120
test "The thirty-seventh first Catalan numbers with iterator":

dynamic_programming/viterbi.nim

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,36 @@ func viterbi*[S, O](hmm: HiddenMarkovModel[S], observations: seq[O]): seq[S] =
3030
for state in 0 ..< len(hmm.states):
3131
# Compute the argmax for probability of the current state
3232
var
33-
max_prob = -1.0
34-
max_prob_state = 0
35-
for prior_state in 0 ..< len(hmm.states):
33+
maxProb = -1.0
34+
maxProbState = 0
35+
for priorState in 0 ..< len(hmm.states):
3636
var
37-
prob = probabilities[nObs - 1][prior_state] *
38-
hmm.transitionProbability[prior_state][state] *
37+
prob = probabilities[nObs - 1][priorState] *
38+
hmm.transitionProbability[priorState][state] *
3939
hmm.emissionProbability[state][obs]
40-
if prob > max_prob:
41-
max_prob = prob
42-
max_prob_state = prior_state
40+
if prob > maxProb:
41+
maxProb = prob
42+
maxProbState = priorState
4343
# Update probabilities and backpointers
44-
probabilities[nObs][state] = max_prob
45-
backpointers[nObs][state] = max_prob_state
44+
probabilities[nObs][state] = maxProb
45+
backpointers[nObs][state] = maxProbState
4646

4747
# Final observation
4848
var
49-
max_prob = -1.0
50-
max_prob_state = 0
49+
maxProb = -1.0
50+
maxProbState = 0
5151
for state in 0 ..< len(hmm.states):
52-
if probabilities[len(observations) - 1][state] > max_prob:
53-
max_prob = probabilities[len(observations) - 1][state]
54-
max_prob_state = state
52+
if probabilities[len(observations) - 1][state] > maxProb:
53+
maxProb = probabilities[len(observations) - 1][state]
54+
maxProbState = state
5555

5656
result = newSeq[S](len(observations))
57-
result[^1] = hmm.states[max_prob_state]
57+
result[^1] = hmm.states[maxProbState]
5858

5959
# Backward Pass - Derive the states from the probabilities
6060
for i in 1 ..< len(observations):
61-
result[^(i+1)] = hmm.states[backpointers[^i][max_prob_state]]
62-
max_prob_state = backpointers[^i][max_prob_state]
61+
result[^(i+1)] = hmm.states[backpointers[^i][maxProbState]]
62+
maxProbState = backpointers[^i][maxProbState]
6363

6464
when isMainModule:
6565
import std/unittest

nim.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Nim compilation defaults for the whole repository
2+
--gc: arc

0 commit comments

Comments
 (0)