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

Exercise/nth prime #36

Merged
merged 4 commits into from
Jun 25, 2024
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
23 changes: 19 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@
"slug": "reverse-string",
"name": "Reverse String",
"uuid": "ba8c915a-85c0-4798-b21f-4adf4268b606",
"practices": [
"loops",
"strings"
],
"practices": ["loops", "strings"],
"prerequisites": [],
"difficulty": 1,
"topics": []
Expand Down Expand Up @@ -186,6 +183,24 @@
"practices": [],
"prerequisites": ["basics", "numbers", "loops"],
"difficulty": 1
},
{
"slug": "nth-prime",
"name": "Nth Prime",
"uuid": "9fd3acaf-d5d7-4f95-addc-db31b4779844",
"practices": [],
"prerequisites": [
"basics",
"bools",
"conditionals",
"comparisons",
"lists",
"list-methods",
"loops",
"numbers",
"strings"
],
"difficulty": 2
}
]
},
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/nth-prime/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Description

Given a number n, determine what the nth prime is.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.
44 changes: 44 additions & 0 deletions exercises/practice/nth-prime/.meta/Example.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@echo off
setlocal enabledelayedexpansion

set targetNTH=%~1
set /a "currentNTH=0"
set "primeNumbers="

if %targetNTH% equ 0 (
echo there is no zeroth prime
exit /b 0
)

for /L %%a in (1,1,10001) do (
set index=%%a
2>nul set /a "1/(!index! %% 2)" && (
if !index! neq 1 (
set divideableCheck=true
for %%b in (!primeNumbers!) do (
set /a "remainder=!index! %% %%b"
if !remainder! equ 0 (
set divideableCheck=false
)
)

if !divideableCheck! equ true (
set primeNumbers=!primeNumbers! !index!
set /a "currentNTH+=1"
if !currentNTH! equ %targetNTH% (
echo !index!
exit /b 0
)
)
)
)

if !index! equ 2 (
set primeNumbers=!primeNumbers! !index!
set /a "currentNTH+=1"
if !currentNTH! equ %targetNTH% (
echo !index!
exit /b 0
)
)
)
16 changes: 16 additions & 0 deletions exercises/practice/nth-prime/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"authors": ["dbenham", "GroophyLifefor"],
"files": {
"solution": [
"NthPrime.bat"
],
"test": [
"NthPrimeTest.bat"
],
"example": [
".meta/Example.bat"
]
},
"blurb": "Given a number n, determine what the nth prime is.",
"source": "A variation on Problem 7 at Project Euler"
}
13 changes: 13 additions & 0 deletions exercises/practice/nth-prime/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[af9ffe10-dc13-42d8-a742-e7bdafac449d]
description = "Say Hi!"
10 changes: 10 additions & 0 deletions exercises/practice/nth-prime/NthPrime.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@echo off
setlocal enabledelayedexpansion

set targetNTH=%~1
set result=

REM Your code goes here


echo %result%
116 changes: 116 additions & 0 deletions exercises/practice/nth-prime/NthPrimeTest.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
@echo off
REM ---------------------------------------------------
REM Difference Of Squares Unit Testing
REM
REM sUnit Testing Framework version: 0.2
REM ---------------------------------------------------

:Main
REM Initalize result variable
set "slug=NthPrime"

CALL :Initialize

REM --------------------
REM Test Case Start \/\/
REM Resource: https://github.com/exercism/problem-specifications/blob/main/exercises/nth-prime/canonical-data.json
REM --------------------
set "expected=2"
set "if_success=Test passed"
set "if_failed=Test failed: first prime"
CALL :Assert "1"

set "expected=3"
set "if_success=Test passed"
set "if_failed=Test failed: second prime"
CALL :Assert "2"

set "expected=13"
set "if_success=Test passed"
set "if_failed=Test failed: sixth prime"
CALL :Assert "6"

set "expected=229"
set "if_success=Test passed"
set "if_failed=Test failed: 50th prime"
CALL :Assert "50"

set "expected=487"
set "if_success=Test passed"
set "if_failed=Test failed: 50th prime"
CALL :Assert "93"

set "expected=there is no zeroth prime"
set "if_success=Test passed"
set "if_failed=Test failed: 50th prime"
CALL :Assert "0"

REM --------------------
REM Test Case End /\/\/\
REM --------------------

CALL :ResolveStatus
exit /b %errorlevel%
REM End of Main

REM ---------------------------------------------------
REM Assert [..Parameters(up to 9)]
REM ---------------------------------------------------
GOTO :End REM Prevents the code below from being executed
:Assert
set "stdout="

REM Run the program and capture the output then delete the file
CALL %slug%.bat %1 %2 %3 %4 %5 %6 %7 %8 %9 > stdout.bin 2>&1
set /p stdout=<stdout.bin
del stdout.bin

REM Check if the result is correct
if "%stdout%" == "%expected%" (
if defined if_success (
echo %if_success%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is correct, exit with code 0
set /a successCount+=1
exit /b 0
) else (
if defined if_failed (
echo %if_failed%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is incorrect, exit with code 1
set /a failCount+=1
exit /b 1
)
GOTO :EOF REM Go back to the line after the call to :Assert

:Initialize
REM It's for initialize, not about checking empty file
set "successCount=0"
set "failCount=0"
GOTO :EOF REM Go back to the line after the call to :CheckEmptyFile

:ResolveStatus
set "status="
if %failCount% gtr 0 (
REM status: Fail
REM message: The test failed.
exit /b 1

) else (
REM status: Pass
exit /b 0

)
GOTO :EOF REM Go back to the line after the call to :ExportResultAsJson

:End