diff --git a/.github/workflows/check_code_format.yml b/.github/workflows/check_code_format.yml new file mode 100644 index 00000000..44dea45f --- /dev/null +++ b/.github/workflows/check_code_format.yml @@ -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 +... diff --git a/config.nims b/config.nims index 4cec23c5..5e37d79a 100644 --- a/config.nims +++ b/config.nims @@ -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" diff --git a/dynamic_programming/catalan_numbers.nim b/dynamic_programming/catalan_numbers.nim index 32d53a59..8536cfd5 100644 --- a/dynamic_programming/catalan_numbers.nim +++ b/dynamic_programming/catalan_numbers.nim @@ -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] @@ -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]) @@ -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: @@ -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) diff --git a/dynamic_programming/viterbi.nim b/dynamic_programming/viterbi.nim index 958d38bd..c7a2cdfe 100644 --- a/dynamic_programming/viterbi.nim +++ b/dynamic_programming/viterbi.nim @@ -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 @@ -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: @@ -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 @@ -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]] @@ -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]