Kotlin exercies
This commit is contained in:
parent
9f4f2c2b45
commit
3e186b69eb
73 changed files with 1799 additions and 0 deletions
1
kotlin/dominoes/.exercism/metadata.json
Normal file
1
kotlin/dominoes/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"track":"kotlin","exercise":"dominoes","id":"54f263f9c337493685adef4809bba4f6","url":"https://exercism.io/my/solutions/54f263f9c337493685adef4809bba4f6","handle":"anthonycicc","is_requester":true,"auto_approve":false}
|
Binary file not shown.
Binary file not shown.
BIN
kotlin/dominoes/.gradle/6.0.1/fileChanges/last-build.bin
Normal file
BIN
kotlin/dominoes/.gradle/6.0.1/fileChanges/last-build.bin
Normal file
Binary file not shown.
BIN
kotlin/dominoes/.gradle/6.0.1/fileContent/fileContent.lock
Normal file
BIN
kotlin/dominoes/.gradle/6.0.1/fileContent/fileContent.lock
Normal file
Binary file not shown.
BIN
kotlin/dominoes/.gradle/6.0.1/fileHashes/fileHashes.bin
Normal file
BIN
kotlin/dominoes/.gradle/6.0.1/fileHashes/fileHashes.bin
Normal file
Binary file not shown.
BIN
kotlin/dominoes/.gradle/6.0.1/fileHashes/fileHashes.lock
Normal file
BIN
kotlin/dominoes/.gradle/6.0.1/fileHashes/fileHashes.lock
Normal file
Binary file not shown.
0
kotlin/dominoes/.gradle/6.0.1/gc.properties
Normal file
0
kotlin/dominoes/.gradle/6.0.1/gc.properties
Normal file
Binary file not shown.
|
@ -0,0 +1,2 @@
|
|||
#Sat Mar 07 15:42:49 EST 2020
|
||||
gradle.version=6.0.1
|
BIN
kotlin/dominoes/.gradle/buildOutputCleanup/outputFiles.bin
Normal file
BIN
kotlin/dominoes/.gradle/buildOutputCleanup/outputFiles.bin
Normal file
Binary file not shown.
0
kotlin/dominoes/.gradle/vcs-1/gc.properties
Normal file
0
kotlin/dominoes/.gradle/vcs-1/gc.properties
Normal file
8
kotlin/dominoes/.idea/.gitignore
generated
vendored
Normal file
8
kotlin/dominoes/.idea/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
6
kotlin/dominoes/.idea/compiler.xml
generated
Normal file
6
kotlin/dominoes/.idea/compiler.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<bytecodeTargetLevel target="13" />
|
||||
</component>
|
||||
</project>
|
20
kotlin/dominoes/.idea/jarRepositories.xml
generated
Normal file
20
kotlin/dominoes/.idea/jarRepositories.xml
generated
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="BintrayJCenter" />
|
||||
<option name="name" value="BintrayJCenter" />
|
||||
<option name="url" value="https://jcenter.bintray.com/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
8
kotlin/dominoes/.idea/misc.xml
generated
Normal file
8
kotlin/dominoes/.idea/misc.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="false" />
|
||||
</project>
|
6
kotlin/dominoes/.idea/vcs.xml
generated
Normal file
6
kotlin/dominoes/.idea/vcs.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
42
kotlin/dominoes/README.md
Normal file
42
kotlin/dominoes/README.md
Normal file
|
@ -0,0 +1,42 @@
|
|||
# Dominoes
|
||||
|
||||
Make a chain of dominoes.
|
||||
|
||||
Compute a way to order a given set of dominoes in such a way that they form a
|
||||
correct domino chain (the dots on one half of a stone match the dots on the
|
||||
neighbouring half of an adjacent stone) and that dots on the halves of the
|
||||
stones which don't have a neighbour (the first and last stone) match each other.
|
||||
|
||||
For example given the stones `[2|1]`, `[2|3]` and `[1|3]` you should compute something
|
||||
like `[1|2] [2|3] [3|1]` or `[3|2] [2|1] [1|3]` or `[1|3] [3|2] [2|1]` etc, where the first and last numbers are the same.
|
||||
|
||||
For stones `[1|2]`, `[4|1]` and `[2|3]` the resulting chain is not valid: `[4|1] [1|2] [2|3]`'s first and last numbers are not the same. 4 != 3
|
||||
|
||||
Some test cases may use duplicate stones in a chain solution, assume that multiple Domino sets are being used.
|
||||
|
||||
## Setup
|
||||
|
||||
Go through the setup instructions for Kotlin to install the necessary
|
||||
dependencies:
|
||||
|
||||
[https://exercism.io/tracks/kotlin/installation](https://exercism.io/tracks/kotlin/installation)
|
||||
|
||||
## Making the test suite pass
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ ./gradlew test
|
||||
```
|
||||
|
||||
> Use `gradlew.bat` if you're on Windows
|
||||
|
||||
In the test suites all tests but the first have been skipped.
|
||||
|
||||
Once you get a test passing, you can enable the next one by removing the
|
||||
`@Ignore` annotation.
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
|
||||
It's possible to submit an incomplete solution so you can see how others have
|
||||
completed the exercise.
|
23
kotlin/dominoes/build.gradle.kts
Normal file
23
kotlin/dominoes/build.gradle.kts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
|
||||
|
||||
plugins {
|
||||
kotlin("jvm") version "1.3.60"
|
||||
}
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile(kotlin("stdlib"))
|
||||
|
||||
testImplementation("junit:junit:4.12")
|
||||
testImplementation(kotlin("test-junit"))
|
||||
}
|
||||
|
||||
tasks.withType<Test> {
|
||||
testLogging {
|
||||
exceptionFormat = TestExceptionFormat.FULL
|
||||
events("passed", "failed", "skipped")
|
||||
}
|
||||
}
|
5
kotlin/dominoes/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
kotlin/dominoes/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
188
kotlin/dominoes/gradlew
vendored
Normal file
188
kotlin/dominoes/gradlew
vendored
Normal file
|
@ -0,0 +1,188 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
#
|
||||
# Copyright 2015 the original author or authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
# Need this for relative symlinks.
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG=`dirname "$PRG"`"/$link"
|
||||
fi
|
||||
done
|
||||
SAVED="`pwd`"
|
||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD="java"
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
MAX_FD="$MAX_FD_LIMIT"
|
||||
fi
|
||||
ulimit -n $MAX_FD
|
||||
if [ $? -ne 0 ] ; then
|
||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||
fi
|
||||
else
|
||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||
fi
|
||||
fi
|
||||
|
||||
# For Darwin, add options to specify how the application appears in the dock
|
||||
if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||
SEP=""
|
||||
for dir in $ROOTDIRSRAW ; do
|
||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||
SEP="|"
|
||||
done
|
||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||
# Add a user-defined pattern to the cygpath arguments
|
||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||
fi
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
i=0
|
||||
for arg in "$@" ; do
|
||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||
|
||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=$((i+1))
|
||||
done
|
||||
case $i in
|
||||
(0) set -- ;;
|
||||
(1) set -- "$args0" ;;
|
||||
(2) set -- "$args0" "$args1" ;;
|
||||
(3) set -- "$args0" "$args1" "$args2" ;;
|
||||
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Escape application args
|
||||
save () {
|
||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||
echo " "
|
||||
}
|
||||
APP_ARGS=$(save "$@")
|
||||
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
|
||||
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
|
||||
cd "$(dirname "$0")"
|
||||
fi
|
||||
|
||||
exec "$JAVACMD" "$@"
|
100
kotlin/dominoes/gradlew.bat
vendored
Normal file
100
kotlin/dominoes/gradlew.bat
vendored
Normal file
|
@ -0,0 +1,100 @@
|
|||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
44
kotlin/dominoes/src/main/kotlin/Dominoes.kt
Normal file
44
kotlin/dominoes/src/main/kotlin/Dominoes.kt
Normal file
|
@ -0,0 +1,44 @@
|
|||
class ChainNotFoundException(msg: String = "Doesn't matter") : RuntimeException(msg)
|
||||
|
||||
data class Domino(val left: Int, val right: Int) {
|
||||
fun reversed() = Domino(right, left)
|
||||
fun contains(dots: Int): Boolean = left == dots || right == dots
|
||||
}
|
||||
|
||||
object Dominoes {
|
||||
fun formChain(vararg domino: Domino): List<Domino> = formChain(domino.toList())
|
||||
|
||||
fun formChain(inputDominoes: List<Domino>): List<Domino> {
|
||||
if (inputDominoes.isEmpty()) return emptyList()
|
||||
inputDominoes.forEachIndexed { index, domino ->
|
||||
val remaining = inputDominoes.filterIndexed { i, _ -> i != index }
|
||||
val chain = chainFrom(listOf(domino), remaining) ?: chainFrom(listOf(domino.reversed()), remaining)
|
||||
return chain ?: throw ChainNotFoundException()
|
||||
}
|
||||
throw ChainNotFoundException()
|
||||
}
|
||||
|
||||
private fun chainFrom(chain: List<Domino>, remaining: List<Domino>): List<Domino>? {
|
||||
if (remaining.isEmpty() && chain.first().left == chain.last().right) {
|
||||
return chain
|
||||
} else {
|
||||
val tail = chain.last().right
|
||||
remaining.forEachIndexed { index, domino ->
|
||||
if (domino.contains(tail)) {
|
||||
val d = if (domino.left == tail) {
|
||||
domino
|
||||
} else {
|
||||
domino.reversed()
|
||||
}
|
||||
val rest = remaining.filterIndexed { i, _ -> i != index }
|
||||
val result = chainFrom(chain + d, rest)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
}
|
138
kotlin/dominoes/src/test/kotlin/DominoesTest.kt
Normal file
138
kotlin/dominoes/src/test/kotlin/DominoesTest.kt
Normal file
|
@ -0,0 +1,138 @@
|
|||
import org.junit.Rule
|
||||
import org.junit.rules.ExpectedException
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class DominoesTest {
|
||||
|
||||
@Test
|
||||
fun `empty input = empty output`() = Dominoes.formChain().should { haveSize(0) }
|
||||
|
||||
@Test
|
||||
fun `singleton input = singleton output`() {
|
||||
val input = listOf(Domino(1, 1))
|
||||
Dominoes.formChain(input).should { beValidDominoes(input) }
|
||||
}
|
||||
|
||||
@Test(ChainNotFoundException::class)
|
||||
fun `singleton can't be chained`() {
|
||||
Dominoes.formChain(Domino(1, 2))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `three elements`() {
|
||||
val input = listOf(Domino(1, 2), Domino(3, 1), Domino(2, 3))
|
||||
Dominoes.formChain(input).should { beValidDominoes(input) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can reverse dominoes`() {
|
||||
val input = listOf(Domino(1, 2), Domino(1, 3), Domino(2, 3))
|
||||
Dominoes.formChain(input).should { beValidDominoes(input) }
|
||||
}
|
||||
|
||||
@Test(expected = ChainNotFoundException::class)
|
||||
fun `can't be chained`() {
|
||||
Dominoes.formChain(Domino(1, 2), Domino(4, 1), Domino(2, 3))
|
||||
}
|
||||
|
||||
@Test(expected = ChainNotFoundException::class)
|
||||
fun `disconnected - simple`() {
|
||||
Dominoes.formChain(Domino(1, 1), Domino(2, 2))
|
||||
}
|
||||
|
||||
@Test(expected = ChainNotFoundException::class)
|
||||
fun `disconnected - double loop`() {
|
||||
Dominoes.formChain(Domino(1, 2), Domino(2, 1), Domino(3, 4), Domino(4, 3))
|
||||
}
|
||||
|
||||
@Test(expected = ChainNotFoundException::class)
|
||||
fun `disconnected - single isolated`() {
|
||||
Dominoes.formChain(Domino(1, 2), Domino(2, 3), Domino(3, 1), Domino(4, 4))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `need backtrack`() {
|
||||
val input = listOf(
|
||||
Domino(1, 2),
|
||||
Domino(2, 3),
|
||||
Domino(3, 1),
|
||||
Domino(2, 4),
|
||||
Domino(4, 2))
|
||||
Dominoes.formChain(input).should { beValidDominoes(input) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `separate loops`() {
|
||||
val input = listOf(
|
||||
Domino(1, 2),
|
||||
Domino(2, 3),
|
||||
Domino(3, 1),
|
||||
Domino(1, 1),
|
||||
Domino(2, 2),
|
||||
Domino(3, 3))
|
||||
Dominoes.formChain(input).should { beValidDominoes(input) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nine elements`() {
|
||||
val input = listOf(
|
||||
Domino(1, 2),
|
||||
Domino(5, 3),
|
||||
Domino(3, 1),
|
||||
Domino(1, 2),
|
||||
Domino(2, 4),
|
||||
Domino(1, 6),
|
||||
Domino(2, 3),
|
||||
Domino(3, 4),
|
||||
Domino(5, 6))
|
||||
Dominoes.formChain(input).should { beValidDominoes(input) }
|
||||
}
|
||||
|
||||
private fun List<Domino>.should(what: DominoListAsserter.() -> Unit) = what(DominoListAsserter(this))
|
||||
|
||||
private class DominoListAsserter(private val outputDominoes: List<Domino>) {
|
||||
|
||||
fun haveSize(n: Int) = assertEquals(n, outputDominoes.size)
|
||||
|
||||
fun beValidDominoes(inputDominoes: List<Domino>) {
|
||||
haveSameDominoesAs(inputDominoes)
|
||||
haveMatchingEnds()
|
||||
haveConsecutiveDominoes()
|
||||
}
|
||||
|
||||
private fun haveSameDominoesAs(inputDominoes: List<Domino>) {
|
||||
val errorMessage = "The number of dominoes in the input list (${inputDominoes.size}) needs to match the number of dominoes in the output chain (${outputDominoes.size})"
|
||||
assertEquals(inputDominoes.size, outputDominoes.size, errorMessage)
|
||||
inputDominoes.forEach { domino ->
|
||||
val inputFrequency: Int = dominoFrequency(inputDominoes, domino)
|
||||
val outputFrequency: Int = dominoFrequency(outputDominoes, domino)
|
||||
val frequencyErrorMessage = "The frequency of domino (${domino.left}, ${domino.right}) in the input is ($inputFrequency), but ($outputFrequency) in the output."
|
||||
assertEquals(inputFrequency, outputFrequency, frequencyErrorMessage)
|
||||
}
|
||||
}
|
||||
|
||||
private fun haveMatchingEnds() {
|
||||
val leftValueOfFirstDomino = outputDominoes.first().left
|
||||
val rightValueOfLastDomino = outputDominoes.last().right
|
||||
val errorMessage = "The left value of the first domino ($leftValueOfFirstDomino) needs to match the right value of the last domino ($rightValueOfLastDomino)."
|
||||
assertEquals(leftValueOfFirstDomino, rightValueOfLastDomino, errorMessage)
|
||||
}
|
||||
|
||||
private fun haveConsecutiveDominoes() {
|
||||
(0 until outputDominoes.size - 1).forEach { i ->
|
||||
val rightValueOfIthDomino = outputDominoes[i].right
|
||||
val leftValueOfNextDomino = outputDominoes[i + 1].left
|
||||
val errorMessage = "The right value of domino number $i ($rightValueOfIthDomino) needs to match the left value of domino number ${i + 1} ($leftValueOfNextDomino)."
|
||||
assertEquals(outputDominoes[i].right, outputDominoes[i + 1].left, errorMessage)
|
||||
}
|
||||
}
|
||||
|
||||
private fun dominoFrequency(list: List<Domino>, d: Domino) =
|
||||
list.count {
|
||||
(it.left == d.left && it.right == d.right) || (it.left == d.right && it.right == d.left)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
1
kotlin/kindergarten-garden/.exercism/metadata.json
Normal file
1
kotlin/kindergarten-garden/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"track":"kotlin","exercise":"kindergarten-garden","id":"8968cc6a00f84cf883cb56be6827b8cf","url":"https://exercism.io/my/solutions/8968cc6a00f84cf883cb56be6827b8cf","handle":"anthonycicc","is_requester":true,"auto_approve":false}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
0
kotlin/kindergarten-garden/.gradle/6.0.1/gc.properties
Normal file
0
kotlin/kindergarten-garden/.gradle/6.0.1/gc.properties
Normal file
Binary file not shown.
|
@ -0,0 +1,2 @@
|
|||
#Sat Mar 07 16:24:19 EST 2020
|
||||
gradle.version=6.0.1
|
Binary file not shown.
0
kotlin/kindergarten-garden/.gradle/vcs-1/gc.properties
Normal file
0
kotlin/kindergarten-garden/.gradle/vcs-1/gc.properties
Normal file
8
kotlin/kindergarten-garden/.idea/.gitignore
generated
vendored
Normal file
8
kotlin/kindergarten-garden/.idea/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
6
kotlin/kindergarten-garden/.idea/compiler.xml
generated
Normal file
6
kotlin/kindergarten-garden/.idea/compiler.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<bytecodeTargetLevel target="13" />
|
||||
</component>
|
||||
</project>
|
20
kotlin/kindergarten-garden/.idea/jarRepositories.xml
generated
Normal file
20
kotlin/kindergarten-garden/.idea/jarRepositories.xml
generated
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="BintrayJCenter" />
|
||||
<option name="name" value="BintrayJCenter" />
|
||||
<option name="url" value="https://jcenter.bintray.com/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
8
kotlin/kindergarten-garden/.idea/misc.xml
generated
Normal file
8
kotlin/kindergarten-garden/.idea/misc.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="false" />
|
||||
</project>
|
6
kotlin/kindergarten-garden/.idea/vcs.xml
generated
Normal file
6
kotlin/kindergarten-garden/.idea/vcs.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
78
kotlin/kindergarten-garden/README.md
Normal file
78
kotlin/kindergarten-garden/README.md
Normal file
|
@ -0,0 +1,78 @@
|
|||
# Kindergarten Garden
|
||||
|
||||
Given a diagram, determine which plants each child in the kindergarten class is
|
||||
responsible for.
|
||||
|
||||
The kindergarten class is learning about growing plants. The teacher
|
||||
thought it would be a good idea to give them actual seeds, plant them in
|
||||
actual dirt, and grow actual plants.
|
||||
|
||||
They've chosen to grow grass, clover, radishes, and violets.
|
||||
|
||||
To this end, the children have put little cups along the window sills, and
|
||||
planted one type of plant in each cup, choosing randomly from the available
|
||||
types of seeds.
|
||||
|
||||
```text
|
||||
[window][window][window]
|
||||
........................ # each dot represents a cup
|
||||
........................
|
||||
```
|
||||
|
||||
There are 12 children in the class:
|
||||
|
||||
- Alice, Bob, Charlie, David,
|
||||
- Eve, Fred, Ginny, Harriet,
|
||||
- Ileana, Joseph, Kincaid, and Larry.
|
||||
|
||||
Each child gets 4 cups, two on each row. Their teacher assigns cups to
|
||||
the children alphabetically by their names.
|
||||
|
||||
The following diagram represents Alice's plants:
|
||||
|
||||
```text
|
||||
[window][window][window]
|
||||
VR......................
|
||||
RG......................
|
||||
```
|
||||
|
||||
In the first row, nearest the windows, she has a violet and a radish. In the
|
||||
second row she has a radish and some grass.
|
||||
|
||||
Your program will be given the plants from left-to-right starting with
|
||||
the row nearest the windows. From this, it should be able to determine
|
||||
which plants belong to each student.
|
||||
|
||||
For example, if it's told that the garden looks like so:
|
||||
|
||||
```text
|
||||
[window][window][window]
|
||||
VRCGVVRVCGGCCGVRGCVCGCGV
|
||||
VRCCCGCRRGVCGCRVVCVGCGCV
|
||||
```
|
||||
|
||||
Then if asked for Alice's plants, it should provide:
|
||||
|
||||
- Violets, radishes, violets, radishes
|
||||
|
||||
While asking for Bob's plants would yield:
|
||||
|
||||
- Clover, grass, clover, clover
|
||||
|
||||
# Running the tests
|
||||
|
||||
You can run all the tests for an exercise by entering
|
||||
|
||||
```sh
|
||||
$ ./gradlew test
|
||||
```
|
||||
|
||||
in your terminal.
|
||||
|
||||
## Source
|
||||
|
||||
Random musings during airplane trip. [http://jumpstartlab.com](http://jumpstartlab.com)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
23
kotlin/kindergarten-garden/build.gradle.kts
Normal file
23
kotlin/kindergarten-garden/build.gradle.kts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
|
||||
|
||||
plugins {
|
||||
kotlin("jvm") version "1.3.60"
|
||||
}
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile(kotlin("stdlib"))
|
||||
|
||||
testImplementation("junit:junit:4.12")
|
||||
testImplementation(kotlin("test-junit"))
|
||||
}
|
||||
|
||||
tasks.withType<Test> {
|
||||
testLogging {
|
||||
exceptionFormat = TestExceptionFormat.FULL
|
||||
events("passed", "failed", "skipped")
|
||||
}
|
||||
}
|
5
kotlin/kindergarten-garden/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
kotlin/kindergarten-garden/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
188
kotlin/kindergarten-garden/gradlew
vendored
Normal file
188
kotlin/kindergarten-garden/gradlew
vendored
Normal file
|
@ -0,0 +1,188 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
#
|
||||
# Copyright 2015 the original author or authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
# Need this for relative symlinks.
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG=`dirname "$PRG"`"/$link"
|
||||
fi
|
||||
done
|
||||
SAVED="`pwd`"
|
||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD="java"
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
MAX_FD="$MAX_FD_LIMIT"
|
||||
fi
|
||||
ulimit -n $MAX_FD
|
||||
if [ $? -ne 0 ] ; then
|
||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||
fi
|
||||
else
|
||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||
fi
|
||||
fi
|
||||
|
||||
# For Darwin, add options to specify how the application appears in the dock
|
||||
if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||
SEP=""
|
||||
for dir in $ROOTDIRSRAW ; do
|
||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||
SEP="|"
|
||||
done
|
||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||
# Add a user-defined pattern to the cygpath arguments
|
||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||
fi
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
i=0
|
||||
for arg in "$@" ; do
|
||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||
|
||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=$((i+1))
|
||||
done
|
||||
case $i in
|
||||
(0) set -- ;;
|
||||
(1) set -- "$args0" ;;
|
||||
(2) set -- "$args0" "$args1" ;;
|
||||
(3) set -- "$args0" "$args1" "$args2" ;;
|
||||
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Escape application args
|
||||
save () {
|
||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||
echo " "
|
||||
}
|
||||
APP_ARGS=$(save "$@")
|
||||
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
|
||||
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
|
||||
cd "$(dirname "$0")"
|
||||
fi
|
||||
|
||||
exec "$JAVACMD" "$@"
|
100
kotlin/kindergarten-garden/gradlew.bat
vendored
Normal file
100
kotlin/kindergarten-garden/gradlew.bat
vendored
Normal file
|
@ -0,0 +1,100 @@
|
|||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
100
kotlin/kindergarten-garden/src/main/kotlin/KindergartenGarden.kt
Normal file
100
kotlin/kindergarten-garden/src/main/kotlin/KindergartenGarden.kt
Normal file
|
@ -0,0 +1,100 @@
|
|||
class KindergartenGarden(private val diagram: String) {
|
||||
private enum class Student(i: Int) {
|
||||
Alice(0),
|
||||
Bob(1),
|
||||
Charlie(2),
|
||||
David(3),
|
||||
Eve(4),
|
||||
Fred(5),
|
||||
Ginny(6),
|
||||
Harriet(7),
|
||||
Ileana(8),
|
||||
Joseph(9),
|
||||
Kincaid(10),
|
||||
Larry(12)
|
||||
}
|
||||
|
||||
private enum class Plant(s: String) {
|
||||
Violet("V") {
|
||||
override fun toString(): String = "violets"
|
||||
},
|
||||
Grass("G") {
|
||||
override fun toString(): String = "grass"
|
||||
},
|
||||
Clover("C") {
|
||||
override fun toString(): String = "clover"
|
||||
},
|
||||
Radish("R") {
|
||||
override fun toString(): String = "radishes"
|
||||
}
|
||||
}
|
||||
|
||||
private val studentToPlants: Map<Student, List<Plant>>
|
||||
init {
|
||||
val k = mutableMapOf<Student, MutableList<Plant>>()
|
||||
val rows = diagram.split("\n").map { row -> row.map { it.toString().toPlant() } }
|
||||
for (group in 0 until (rows[0].size / 2)){
|
||||
val plants = mutableListOf<Plant>()
|
||||
with(plants) {
|
||||
add(rows[0][2 * group])
|
||||
add(rows[0][(2 * group) + 1])
|
||||
add(rows[1][2 * group])
|
||||
add(rows[1][(2 * group) + 1])
|
||||
}
|
||||
k[group.toStudent()] = plants
|
||||
}
|
||||
studentToPlants = k
|
||||
}
|
||||
|
||||
private fun String.toPlant(): Plant {
|
||||
return when (this) {
|
||||
"V" -> Plant.Violet
|
||||
"G" -> Plant.Grass
|
||||
"C" -> Plant.Clover
|
||||
"R" -> Plant.Radish
|
||||
else -> throw IllegalArgumentException("Bad Plant Type. Must be one of V, G, C, R")
|
||||
}
|
||||
}
|
||||
private fun String.toStudent(): KindergartenGarden.Student {
|
||||
return when(this) {
|
||||
"Alice" -> Student.Alice
|
||||
"Bob" -> Student.Bob
|
||||
"Charlie" -> Student.Charlie
|
||||
"David" -> Student.David
|
||||
"Eve" -> Student.Eve
|
||||
"Fred" -> Student.Fred
|
||||
"Ginny" -> Student.Ginny
|
||||
"Harriett" -> Student.Harriet
|
||||
"Ileana" -> Student.Ileana
|
||||
"Joseph" -> Student.Joseph
|
||||
"Kincaid" -> Student.Kincaid
|
||||
"Larry" -> Student.Larry
|
||||
else -> throw IllegalArgumentException("Bad student name $this")
|
||||
}
|
||||
}
|
||||
|
||||
private fun Int.toStudent(): KindergartenGarden.Student {
|
||||
return when (this) {
|
||||
0 -> Student.Alice
|
||||
1 -> Student.Bob
|
||||
2 -> Student.Charlie
|
||||
3 -> Student.David
|
||||
4 -> Student.Eve
|
||||
5 -> Student.Fred
|
||||
6 -> Student.Ginny
|
||||
7 -> Student.Harriet
|
||||
8 -> Student.Ileana
|
||||
9 -> Student.Joseph
|
||||
10 -> Student.Kincaid
|
||||
11 -> Student.Larry
|
||||
else -> throw IllegalArgumentException("Bad student number $this")
|
||||
}
|
||||
fun getPlantsOfStudent(student: String): List<String> {
|
||||
TODO("Implement the function to complete the task")
|
||||
}
|
||||
}
|
||||
|
||||
fun getPlantsOfStudent(student: String): List<String> {
|
||||
return studentToPlants[student.toStudent()]?.map { it.toString() } ?: emptyList()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
import kotlin.test.Test
|
||||
import kotlin.test.Ignore
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class KindergartenGardenTest {
|
||||
|
||||
@Test
|
||||
fun `single student`() {
|
||||
val student = "Alice"
|
||||
val diagram = "RC\nGG"
|
||||
val expected = listOf("radishes", "clover", "grass", "grass")
|
||||
|
||||
assertEquals(expected, KindergartenGarden(diagram).getPlantsOfStudent(student))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `single student2`() {
|
||||
val student = "Alice"
|
||||
val diagram = "VC\nRC"
|
||||
val expected = listOf("violets", "clover", "radishes", "clover")
|
||||
|
||||
assertEquals(expected, KindergartenGarden(diagram).getPlantsOfStudent(student))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `two students`() {
|
||||
val student = "Bob"
|
||||
val diagram = "VVCG\nVVRC"
|
||||
val expected = listOf("clover", "grass", "radishes", "clover")
|
||||
|
||||
assertEquals(expected, KindergartenGarden(diagram).getPlantsOfStudent(student))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `one garden second student`() {
|
||||
val student = "Bob"
|
||||
val diagram = "VVCCGG\nVVCCGG"
|
||||
val expected = listOf("clover", "clover", "clover", "clover")
|
||||
|
||||
assertEquals(expected, KindergartenGarden(diagram).getPlantsOfStudent(student))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `one garden third student`() {
|
||||
val student = "Charlie"
|
||||
val diagram = "VVCCGG\nVVCCGG"
|
||||
val expected = listOf("grass", "grass", "grass", "grass")
|
||||
|
||||
assertEquals(expected, KindergartenGarden(diagram).getPlantsOfStudent(student))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `full garden first student`() {
|
||||
val student = "Alice"
|
||||
val diagram = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV"
|
||||
val expected = listOf("violets", "radishes", "violets", "radishes")
|
||||
|
||||
assertEquals(expected, KindergartenGarden(diagram).getPlantsOfStudent(student))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `full garden second student`() {
|
||||
val student = "Bob"
|
||||
val diagram = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV"
|
||||
val expected = listOf("clover", "grass", "clover", "clover")
|
||||
|
||||
assertEquals(expected, KindergartenGarden(diagram).getPlantsOfStudent(student))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `full garden second to last student`() {
|
||||
val student = "Kincaid"
|
||||
val diagram = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV"
|
||||
val expected = listOf("grass", "clover", "clover", "grass")
|
||||
|
||||
assertEquals(expected, KindergartenGarden(diagram).getPlantsOfStudent(student))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `full garden last student`() {
|
||||
val student = "Larry"
|
||||
val diagram = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV"
|
||||
val expected = listOf("grass", "violets", "clover", "violets")
|
||||
|
||||
assertEquals(expected, KindergartenGarden(diagram).getPlantsOfStudent(student))
|
||||
}
|
||||
|
||||
}
|
1
kotlin/yacht/.exercism/metadata.json
Normal file
1
kotlin/yacht/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"track":"kotlin","exercise":"yacht","id":"54cf6a94c14545848b8113a4eab84156","url":"https://exercism.io/my/solutions/54cf6a94c14545848b8113a4eab84156","handle":"anthonycicc","is_requester":true,"auto_approve":false}
|
BIN
kotlin/yacht/.gradle/6.0.1/executionHistory/executionHistory.bin
Normal file
BIN
kotlin/yacht/.gradle/6.0.1/executionHistory/executionHistory.bin
Normal file
Binary file not shown.
Binary file not shown.
BIN
kotlin/yacht/.gradle/6.0.1/fileChanges/last-build.bin
Normal file
BIN
kotlin/yacht/.gradle/6.0.1/fileChanges/last-build.bin
Normal file
Binary file not shown.
BIN
kotlin/yacht/.gradle/6.0.1/fileContent/fileContent.lock
Normal file
BIN
kotlin/yacht/.gradle/6.0.1/fileContent/fileContent.lock
Normal file
Binary file not shown.
BIN
kotlin/yacht/.gradle/6.0.1/fileHashes/fileHashes.bin
Normal file
BIN
kotlin/yacht/.gradle/6.0.1/fileHashes/fileHashes.bin
Normal file
Binary file not shown.
BIN
kotlin/yacht/.gradle/6.0.1/fileHashes/fileHashes.lock
Normal file
BIN
kotlin/yacht/.gradle/6.0.1/fileHashes/fileHashes.lock
Normal file
Binary file not shown.
0
kotlin/yacht/.gradle/6.0.1/gc.properties
Normal file
0
kotlin/yacht/.gradle/6.0.1/gc.properties
Normal file
BIN
kotlin/yacht/.gradle/buildOutputCleanup/buildOutputCleanup.lock
Normal file
BIN
kotlin/yacht/.gradle/buildOutputCleanup/buildOutputCleanup.lock
Normal file
Binary file not shown.
2
kotlin/yacht/.gradle/buildOutputCleanup/cache.properties
Normal file
2
kotlin/yacht/.gradle/buildOutputCleanup/cache.properties
Normal file
|
@ -0,0 +1,2 @@
|
|||
#Sat Mar 07 16:55:00 EST 2020
|
||||
gradle.version=6.0.1
|
BIN
kotlin/yacht/.gradle/buildOutputCleanup/outputFiles.bin
Normal file
BIN
kotlin/yacht/.gradle/buildOutputCleanup/outputFiles.bin
Normal file
Binary file not shown.
0
kotlin/yacht/.gradle/vcs-1/gc.properties
Normal file
0
kotlin/yacht/.gradle/vcs-1/gc.properties
Normal file
8
kotlin/yacht/.idea/.gitignore
generated
vendored
Normal file
8
kotlin/yacht/.idea/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
6
kotlin/yacht/.idea/compiler.xml
generated
Normal file
6
kotlin/yacht/.idea/compiler.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<bytecodeTargetLevel target="13" />
|
||||
</component>
|
||||
</project>
|
20
kotlin/yacht/.idea/jarRepositories.xml
generated
Normal file
20
kotlin/yacht/.idea/jarRepositories.xml
generated
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="BintrayJCenter" />
|
||||
<option name="name" value="BintrayJCenter" />
|
||||
<option name="url" value="https://jcenter.bintray.com/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
8
kotlin/yacht/.idea/misc.xml
generated
Normal file
8
kotlin/yacht/.idea/misc.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="false" />
|
||||
</project>
|
6
kotlin/yacht/.idea/vcs.xml
generated
Normal file
6
kotlin/yacht/.idea/vcs.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
67
kotlin/yacht/README.md
Normal file
67
kotlin/yacht/README.md
Normal file
|
@ -0,0 +1,67 @@
|
|||
# Score a single throw of dice in *Yacht*
|
||||
|
||||
The dice game [Yacht](https://en.wikipedia.org/wiki/Yacht_(dice_game)) is from
|
||||
the same family as Poker Dice, Generala and particularly Yahtzee, of which it
|
||||
is a precursor. In the game, five dice are rolled and the result can be entered
|
||||
in any of twelve categories. The score of a throw of the dice depends on
|
||||
category chosen.
|
||||
|
||||
## Scores in Yacht
|
||||
|
||||
| Category | Score | Description | Example |
|
||||
| -------- | ----- | ----------- | ------- |
|
||||
| Ones | 1 × number of ones | Any combination | 1 1 1 4 5 scores 3 |
|
||||
| Twos | 2 × number of twos | Any combination | 2 2 3 4 5 scores 4 |
|
||||
| Threes | 3 × number of threes | Any combination | 3 3 3 3 3 scores 15 |
|
||||
| Fours | 4 × number of fours | Any combination | 1 2 3 3 5 scores 0 |
|
||||
| Fives | 5 × number of fives| Any combination | 5 1 5 2 5 scores 15 |
|
||||
| Sixes | 6 × number of sixes | Any combination | 2 3 4 5 6 scores 6 |
|
||||
| Full House | Total of the dice | Three of one number and two of another | 3 3 3 5 5 scores 19 |
|
||||
| Four of a Kind | Total of the four dice | At least four dice showing the same face | 4 4 4 4 6 scores 16 |
|
||||
| Little Straight | 30 points | 1-2-3-4-5 | 1 2 3 4 5 scores 30 |
|
||||
| Big Straight | 30 points | 2-3-4-5-6 | 2 3 4 5 6 scores 30 |
|
||||
| Choice | Sum of the dice | Any combination | 2 3 3 4 6 scores 18 |
|
||||
| Yacht | 50 points | All five dice showing the same face | 4 4 4 4 4 scores 50 |
|
||||
|
||||
If the dice do not satisfy the requirements of a category, the score is zero.
|
||||
If, for example, *Four Of A Kind* is entered in the *Yacht* category, zero
|
||||
points are scored. A *Yacht* scores zero if entered in the *Full House* category.
|
||||
|
||||
## Task
|
||||
|
||||
Given a list of values for five dice and a category, your solution should return
|
||||
the score of the dice for that category. If the dice do not satisfy the requirements
|
||||
of the category your solution should return 0. You can assume that five values
|
||||
will always be presented, and the value of each will be between one and six
|
||||
inclusively. You should not assume that the dice are ordered.
|
||||
|
||||
## Setup
|
||||
|
||||
Go through the setup instructions for Kotlin to install the necessary
|
||||
dependencies:
|
||||
|
||||
[https://exercism.io/tracks/kotlin/installation](https://exercism.io/tracks/kotlin/installation)
|
||||
|
||||
## Making the test suite pass
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ ./gradlew test
|
||||
```
|
||||
|
||||
> Use `gradlew.bat` if you're on Windows
|
||||
|
||||
In the test suites all tests but the first have been skipped.
|
||||
|
||||
Once you get a test passing, you can enable the next one by removing the
|
||||
`@Ignore` annotation.
|
||||
|
||||
## Source
|
||||
|
||||
James Kilfiger, using wikipedia [https://en.wikipedia.org/wiki/Yacht_(dice_game)](https://en.wikipedia.org/wiki/Yacht_(dice_game))
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
|
||||
It's possible to submit an incomplete solution so you can see how others have
|
||||
completed the exercise.
|
23
kotlin/yacht/build.gradle.kts
Normal file
23
kotlin/yacht/build.gradle.kts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
|
||||
|
||||
plugins {
|
||||
kotlin("jvm") version "1.3.60"
|
||||
}
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile(kotlin("stdlib"))
|
||||
|
||||
testImplementation("junit:junit:4.12")
|
||||
testImplementation(kotlin("test-junit"))
|
||||
}
|
||||
|
||||
tasks.withType<Test> {
|
||||
testLogging {
|
||||
exceptionFormat = TestExceptionFormat.FULL
|
||||
events("passed", "failed", "skipped")
|
||||
}
|
||||
}
|
5
kotlin/yacht/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
kotlin/yacht/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
188
kotlin/yacht/gradlew
vendored
Normal file
188
kotlin/yacht/gradlew
vendored
Normal file
|
@ -0,0 +1,188 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
#
|
||||
# Copyright 2015 the original author or authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
# Need this for relative symlinks.
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG=`dirname "$PRG"`"/$link"
|
||||
fi
|
||||
done
|
||||
SAVED="`pwd`"
|
||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD="java"
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
MAX_FD="$MAX_FD_LIMIT"
|
||||
fi
|
||||
ulimit -n $MAX_FD
|
||||
if [ $? -ne 0 ] ; then
|
||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||
fi
|
||||
else
|
||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||
fi
|
||||
fi
|
||||
|
||||
# For Darwin, add options to specify how the application appears in the dock
|
||||
if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||
SEP=""
|
||||
for dir in $ROOTDIRSRAW ; do
|
||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||
SEP="|"
|
||||
done
|
||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||
# Add a user-defined pattern to the cygpath arguments
|
||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||
fi
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
i=0
|
||||
for arg in "$@" ; do
|
||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||
|
||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=$((i+1))
|
||||
done
|
||||
case $i in
|
||||
(0) set -- ;;
|
||||
(1) set -- "$args0" ;;
|
||||
(2) set -- "$args0" "$args1" ;;
|
||||
(3) set -- "$args0" "$args1" "$args2" ;;
|
||||
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Escape application args
|
||||
save () {
|
||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||
echo " "
|
||||
}
|
||||
APP_ARGS=$(save "$@")
|
||||
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
|
||||
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
|
||||
cd "$(dirname "$0")"
|
||||
fi
|
||||
|
||||
exec "$JAVACMD" "$@"
|
100
kotlin/yacht/gradlew.bat
vendored
Normal file
100
kotlin/yacht/gradlew.bat
vendored
Normal file
|
@ -0,0 +1,100 @@
|
|||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
34
kotlin/yacht/src/main/kotlin/Yacht.kt
Normal file
34
kotlin/yacht/src/main/kotlin/Yacht.kt
Normal file
|
@ -0,0 +1,34 @@
|
|||
object Yacht {
|
||||
|
||||
fun solve(category: YachtCategory, vararg dices: Int): Int {
|
||||
return when (category) {
|
||||
YachtCategory.YACHT -> (if (dices.all { it == dices[0] }) 50 else 0)
|
||||
YachtCategory.ONES -> 1 * dices.filter { it == 1 }.size
|
||||
YachtCategory.TWOS -> 2 * dices.filter { it == 2 }.size
|
||||
YachtCategory.THREES -> 3 * dices.filter { it == 3 }.size
|
||||
YachtCategory.FOURS -> 4 * dices.filter { it == 4 }.size
|
||||
YachtCategory.FIVES -> 5 * dices.filter { it == 5 }.size
|
||||
YachtCategory.SIXES -> 6 * dices.filter { it == 6 }.size
|
||||
YachtCategory.FULL_HOUSE -> calcFullHouse(dices.toList())
|
||||
YachtCategory.FOUR_OF_A_KIND -> dices.toList()
|
||||
.groupingBy { it }.eachCount()
|
||||
.filter { (_, frequency) -> frequency >= 4 }.map { (value, frequency) -> value * frequency.coerceAtMost(4) }.getOrNull(0) ?: 0
|
||||
YachtCategory.LITTLE_STRAIGHT -> if (listOf(1,2,3,4,5) == dices.sorted()) 30 else 0
|
||||
YachtCategory.BIG_STRAIGHT -> if (listOf(2,3,4,5,6) == dices.sorted()) 30 else 0
|
||||
YachtCategory.CHOICE -> dices.sum()
|
||||
}
|
||||
}
|
||||
|
||||
private fun calcFullHouse(dice: List<Int>): Int {
|
||||
val frequency = dice.groupingBy { it }.eachCount()
|
||||
val needsCalc = with(frequency) {
|
||||
Pair(any { (_, frequency) -> frequency == 3},
|
||||
any { (_, frequency) -> frequency == 2})
|
||||
}
|
||||
return if (needsCalc.first && needsCalc.second) {
|
||||
dice.sum()
|
||||
} else 0
|
||||
}
|
||||
}
|
||||
|
||||
|
14
kotlin/yacht/src/main/kotlin/YachtCategory.kt
Normal file
14
kotlin/yacht/src/main/kotlin/YachtCategory.kt
Normal file
|
@ -0,0 +1,14 @@
|
|||
enum class YachtCategory {
|
||||
YACHT,
|
||||
ONES,
|
||||
TWOS,
|
||||
THREES,
|
||||
FOURS,
|
||||
FIVES,
|
||||
SIXES,
|
||||
FULL_HOUSE,
|
||||
FOUR_OF_A_KIND,
|
||||
LITTLE_STRAIGHT,
|
||||
BIG_STRAIGHT,
|
||||
CHOICE
|
||||
}
|
93
kotlin/yacht/src/test/kotlin/YachtTest.kt
Normal file
93
kotlin/yacht/src/test/kotlin/YachtTest.kt
Normal file
|
@ -0,0 +1,93 @@
|
|||
import org.junit.Rule
|
||||
import org.junit.rules.ExpectedException
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import YachtCategory.*
|
||||
|
||||
class YachtTest {
|
||||
|
||||
@Test
|
||||
fun yacht() = assertEquals(50, Yacht.solve(YACHT, 5, 5, 5, 5, 5))
|
||||
|
||||
@Test
|
||||
fun `not yacht`() = assertEquals(0, Yacht.solve(YACHT, 1, 3, 3, 2, 5))
|
||||
|
||||
@Test
|
||||
fun ones() = assertEquals(3, Yacht.solve(ONES, 1, 1, 1, 3, 5))
|
||||
|
||||
@Test
|
||||
fun `ones out of order`() = assertEquals(3, Yacht.solve(ONES, 3, 1, 1, 5, 1))
|
||||
|
||||
@Test
|
||||
fun `no ones`() = assertEquals(0, Yacht.solve(ONES, 4, 3, 6, 5, 5))
|
||||
|
||||
@Test
|
||||
fun twos() = assertEquals(2, Yacht.solve(TWOS, 2, 3, 4, 5, 6))
|
||||
|
||||
@Test
|
||||
fun fours() = assertEquals(8, Yacht.solve(FOURS, 1, 4, 1, 4, 1))
|
||||
|
||||
@Test
|
||||
fun `yacht counted as threes`() = assertEquals(15, Yacht.solve(THREES, 3, 3, 3, 3, 3))
|
||||
|
||||
@Test
|
||||
fun `yacht of threes counted as fives`() = assertEquals(0, Yacht.solve(FIVES, 3, 3, 3, 3, 3))
|
||||
|
||||
@Test
|
||||
fun sixes() = assertEquals(6, Yacht.solve(SIXES, 2, 3, 4, 5, 6))
|
||||
|
||||
@Test
|
||||
fun `full house two small three big`() = assertEquals(16, Yacht.solve(FULL_HOUSE, 2, 2, 4, 4, 4))
|
||||
|
||||
@Test
|
||||
fun `full house three small two big`() = assertEquals(19, Yacht.solve(FULL_HOUSE, 5, 3, 3, 5, 3))
|
||||
|
||||
@Test
|
||||
fun `two pair is not a full house`() = assertEquals(0, Yacht.solve(FULL_HOUSE, 2, 2, 4, 4, 5))
|
||||
|
||||
@Test
|
||||
fun `four of a kind is not a full house`() = assertEquals(0, Yacht.solve(FULL_HOUSE, 1, 4, 4, 4, 4))
|
||||
|
||||
@Test
|
||||
fun `yacht is not a full house`() = assertEquals(0, Yacht.solve(FULL_HOUSE, 2, 2, 2, 2, 2))
|
||||
|
||||
@Test
|
||||
fun `four of a kind`() = assertEquals(24, Yacht.solve(FOUR_OF_A_KIND, 6, 6, 4, 6, 6))
|
||||
|
||||
@Test
|
||||
fun `yacht can be scored as four of a kind`() = assertEquals(12, Yacht.solve(FOUR_OF_A_KIND, 3, 3, 3, 3, 3))
|
||||
|
||||
@Test
|
||||
fun `full house is not four of a kind`() = assertEquals(0, Yacht.solve(FOUR_OF_A_KIND, 3, 3, 3, 5, 5))
|
||||
|
||||
@Test
|
||||
fun `little straight`() = assertEquals(30, Yacht.solve(LITTLE_STRAIGHT, 3, 5, 4, 1, 2))
|
||||
|
||||
@Test
|
||||
fun `little straight as big straight`() = assertEquals(0, Yacht.solve(BIG_STRAIGHT, 1, 2, 3, 4, 5))
|
||||
|
||||
@Test
|
||||
fun `four in order but not a little straight`() = assertEquals(0, Yacht.solve(LITTLE_STRAIGHT, 1, 1, 2, 3, 4))
|
||||
|
||||
@Test
|
||||
fun `no pairs but not a little straight`() = assertEquals(0, Yacht.solve(LITTLE_STRAIGHT, 1, 2, 3, 4, 6))
|
||||
|
||||
@Test
|
||||
fun `minimum is 1 maximum is 5 but not a little straight`() = assertEquals(0, Yacht.solve(LITTLE_STRAIGHT, 1, 1, 3, 4, 5))
|
||||
|
||||
@Test
|
||||
fun `big straight`() = assertEquals(30, Yacht.solve(BIG_STRAIGHT, 4, 6, 2, 5, 3))
|
||||
|
||||
@Test
|
||||
fun `big straight as little straight`() = assertEquals(0, Yacht.solve(LITTLE_STRAIGHT, 6, 5, 4, 3, 2))
|
||||
|
||||
@Test
|
||||
fun `no pairs but not a big straight`() = assertEquals(0, Yacht.solve(BIG_STRAIGHT, 6, 5, 4, 3, 1))
|
||||
|
||||
@Test
|
||||
fun choice() = assertEquals(23, Yacht.solve(CHOICE, 3, 3, 5, 6, 6))
|
||||
|
||||
@Test
|
||||
fun `yacht as choice`() = assertEquals(10, Yacht.solve(CHOICE, 2, 2, 2, 2, 2))
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue