Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add check_code_format workflow #19

Merged
merged 22 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/check_code_format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: check_code_format

# yamllint disable-line rule:truthy
on:
workflow_dispatch:
push:
branches:
- main
pull_request:

jobs:
check_format_code:
name: check format code
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: jiro4989/setup-nim-action@v1

- name: Format code
run: |
git clean -f -x -d
nim prettyfy

- name: Fail if needs reformatting
run: |
if [[ $(git status --porcelain) ]]; then
echo "please reformat/prettyfy these files:"
git status --porcelain=v1
exit 1
fi
...
2 changes: 1 addition & 1 deletion config.nims
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nim compilation defaults for the whole repository
--gc: arc
#--gc: arc
if defined(release) or defined(danger):
--opt: speed
--passC: "-flto"
Expand Down
18 changes: 9 additions & 9 deletions dynamic_programming/catalan_numbers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func catalanNumbersCompileTime(index: Natural): Positive =
const catalanTable = createCatalanTable(12)
when sizeof(int) == 4:
const catalanTable = createCatalanTable(20)
when sizeof(int) == 8:
when sizeof(int) == 8:
const catalanTable = createCatalanTable(36)
catalanTable[index-1]

Expand All @@ -89,7 +89,7 @@ when isMainModule:
263747951750360, 1002242216651368, 3814986502092304, 14544636039226909,
55534064877048198, 212336130412243110, 812944042149730764,
3116285494907301262]

static:
for i in 0 ..< 36:
doAssert (CatalanNumbersList[i] == createCatalanTable(36)[i])
Expand All @@ -100,7 +100,7 @@ when isMainModule:
for index in 0 .. limit:
let catalanNumber = catalanNumbersRecursive(index)
check catalanNumber == CatalanNumbersList[index]

test "The thirty-one first Catalan numbers recursively, second formula":
let limit = LowerLimit
for index in 0 .. limit:
Expand All @@ -109,32 +109,32 @@ when isMainModule:

test "The sixteen first Catalan numbers iteratively":
check catalanNumbers(16) == expectedResult

test "We can compute up to the thirty-seventh Catalan number iteratively":
let limit = UpperLimit
let catalanNumbersSeq = catalanNumbers(limit)
for index in 0 .. limit:
check catalanNumbersSeq[index] == CatalanNumbersList[index]

test "The thirty-seventh first Catalan numbers with iterator":
let limit = UpperLimit
var index = 0
for catalanNumber in catalanNumbersIt(limit):
check catalanNumber == CatalanNumbersList[index]
inc index

test "Test the catalan number function with binomials":
let limit = LowerLimit
for index in 0 .. limit:
check catalanNumbers2(index) == CatalanNumbersList[index]

test "The Catalan Numbers binomial formula overflows at 31":
doAssertRaises(OverflowDefect):
discard catalanNumbers2(LowerLimit + 1)

test "Uses compile-time table":
check catalanNumbersCompileTime(UpperLimit) == Positive(3116285494907301262)

test "The compile-time table overflows at 37":
doAssertRaises(OverflowDefect):
discard catalanNumbersCompileTime(UpperLimit + 1)
18 changes: 9 additions & 9 deletions dynamic_programming/viterbi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ func viterbi*[S, O](hmm: HiddenMarkovModel[S], observations: seq[O]): seq[S] =
var
probabilities = newSeq[seq[float]](len(observations))
backpointers = newSeq[seq[int]](len(observations))

# Initialization
for i in 0 ..< len(observations):
probabilities[i] = newSeq[float](len(hmm.states))
backpointers[i] = newSeq[int](len(hmm.states))

for state in 0 ..< len(hmm.states):
probabilities[0][state] = hmm.startProbability[state] *
hmm.emissionProbability[state][observations[0].ord]
backpointers[0][state] = 0

# Forward Pass - Derive the probabilities
for nObs in 1 ..< len(observations):
var
var
obs = observations[nObs].ord
for state in 0 ..< len(hmm.states):
# Compute the argmax for probability of the current state
Expand All @@ -34,7 +34,7 @@ func viterbi*[S, O](hmm: HiddenMarkovModel[S], observations: seq[O]): seq[S] =
max_prob_state = 0
for prior_state in 0 ..< len(hmm.states):
var
prob = probabilities[nObs - 1][prior_state] *
prob = probabilities[nObs - 1][prior_state] *
hmm.transitionProbability[prior_state][state] *
hmm.emissionProbability[state][obs]
if prob > max_prob:
Expand All @@ -43,7 +43,7 @@ func viterbi*[S, O](hmm: HiddenMarkovModel[S], observations: seq[O]): seq[S] =
# Update probabilities and backpointers
probabilities[nObs][state] = max_prob
backpointers[nObs][state] = max_prob_state

# Final observation
var
max_prob = -1.0
Expand All @@ -52,10 +52,10 @@ func viterbi*[S, O](hmm: HiddenMarkovModel[S], observations: seq[O]): seq[S] =
if probabilities[len(observations) - 1][state] > max_prob:
max_prob = probabilities[len(observations) - 1][state]
max_prob_state = state

result = newSeq[S](len(observations))
result[^1] = hmm.states[max_prob_state]

# Backward Pass - Derive the states from the probabilities
for i in 1 ..< len(observations):
result[^(i+1)] = hmm.states[backpointers[^i][max_prob_state]]
Expand All @@ -71,7 +71,7 @@ when isMainModule:
Healthy, Fever
Observations = enum
Normal, Cold, Dizzy
var
var
hmm = HiddenMarkovModel[States]()
observations = @[Normal, Cold, Dizzy]
hmm.states = @[Healthy, Fever]
Expand Down