Kotlin - PigLatin WIP
This commit is contained in:
parent
64d8bde6cd
commit
444047d3f2
20 changed files with 172 additions and 8 deletions
16
kotlin/flatten-array/.idea/workspace.xml
generated
16
kotlin/flatten-array/.idea/workspace.xml
generated
|
@ -32,8 +32,8 @@
|
|||
<file leaf-file-name="Flattener.kt" pinned="false" current-in-tab="true">
|
||||
<entry file="file://$PROJECT_DIR$/src/main/kotlin/Flattener.kt">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="286">
|
||||
<caret line="13" column="1" lean-forward="true" selection-start-line="13" selection-start-column="1" selection-end-line="13" selection-end-column="1" />
|
||||
<state relative-caret-position="220">
|
||||
<caret line="10" column="14" lean-forward="false" selection-start-line="10" selection-start-column="14" selection-end-line="10" selection-end-column="14" />
|
||||
<folding />
|
||||
</state>
|
||||
</provider>
|
||||
|
@ -474,7 +474,7 @@
|
|||
<component name="PropertiesComponent">
|
||||
<property name="WebServerToolWindowFactoryState" value="false" />
|
||||
<property name="aspect.path.notification.shown" value="true" />
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$/../pig-latin" />
|
||||
<property name="project.structure.last.edited" value="Project" />
|
||||
<property name="project.structure.proportion" value="0.0" />
|
||||
<property name="project.structure.side.proportion" value="0.2" />
|
||||
|
@ -640,7 +640,7 @@
|
|||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1501015451954</updated>
|
||||
<workItem from="1501015455717" duration="2058000" />
|
||||
<workItem from="1501015455717" duration="2516000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
|
@ -677,7 +677,7 @@
|
|||
</history-entry>
|
||||
</component>
|
||||
<component name="TimeTrackingManager">
|
||||
<option name="totallyTimeSpent" value="2058000" />
|
||||
<option name="totallyTimeSpent" value="2516000" />
|
||||
</component>
|
||||
<component name="ToolWindowManager">
|
||||
<frame x="-1" y="0" width="2562" height="1391" extended-state="6" />
|
||||
|
@ -692,7 +692,7 @@
|
|||
<window_info id="Capture Analysis" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
|
||||
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
|
||||
<window_info id="Maven Projects" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
|
||||
<window_info id="Run" active="true" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.32981315" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
|
||||
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32981315" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
|
||||
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
|
||||
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32981315" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
|
||||
<window_info id="Capture Tool" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
|
||||
|
@ -757,8 +757,8 @@
|
|||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/main/kotlin/Flattener.kt">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="286">
|
||||
<caret line="13" column="1" lean-forward="true" selection-start-line="13" selection-start-column="1" selection-end-line="13" selection-end-column="1" />
|
||||
<state relative-caret-position="220">
|
||||
<caret line="10" column="14" lean-forward="false" selection-start-line="10" selection-start-column="14" selection-end-line="10" selection-end-column="14" />
|
||||
<folding />
|
||||
</state>
|
||||
</provider>
|
||||
|
|
BIN
kotlin/pig-latin/.gradle/4.0.1/fileChanges/last-build.bin
Normal file
BIN
kotlin/pig-latin/.gradle/4.0.1/fileChanges/last-build.bin
Normal file
Binary file not shown.
BIN
kotlin/pig-latin/.gradle/4.0.1/fileHashes/fileHashes.bin
Normal file
BIN
kotlin/pig-latin/.gradle/4.0.1/fileHashes/fileHashes.bin
Normal file
Binary file not shown.
BIN
kotlin/pig-latin/.gradle/4.0.1/fileHashes/fileHashes.lock
Normal file
BIN
kotlin/pig-latin/.gradle/4.0.1/fileHashes/fileHashes.lock
Normal file
Binary file not shown.
0
kotlin/pig-latin/.gradle/buildOutputCleanup/built.bin
Normal file
0
kotlin/pig-latin/.gradle/buildOutputCleanup/built.bin
Normal file
|
@ -0,0 +1,2 @@
|
|||
#Tue Jul 25 17:26:14 EDT 2017
|
||||
gradle.version=4.0.1
|
|
@ -0,0 +1 @@
|
|||
|
27
kotlin/pig-latin/README.md
Normal file
27
kotlin/pig-latin/README.md
Normal file
|
@ -0,0 +1,27 @@
|
|||
# Pig Latin
|
||||
|
||||
Implement a program that translates from English to Pig Latin.
|
||||
|
||||
Pig Latin is a made-up children's language that's intended to be
|
||||
confusing. It obeys a few simple rules (below), but when it's spoken
|
||||
quickly it's really difficult for non-children (and non-native speakers)
|
||||
to understand.
|
||||
|
||||
- **Rule 1**: If a word begins with a vowel sound, add an "ay" sound to
|
||||
the end of the word.
|
||||
- **Rule 2**: If a word begins with a consonant sound, move it to the
|
||||
end of the word, and then add an "ay" sound to the end of the word.
|
||||
|
||||
There are a few more rules for edge cases, and there are regional
|
||||
variants too.
|
||||
|
||||
See <http://en.wikipedia.org/wiki/Pig_latin> for more details.
|
||||
|
||||
|
||||
|
||||
## Source
|
||||
|
||||
The Pig Latin exercise at Test First Teaching by Ultrasaurus [https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/](https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
28
kotlin/pig-latin/build.gradle
Normal file
28
kotlin/pig-latin/build.gradle
Normal file
|
@ -0,0 +1,28 @@
|
|||
buildscript {
|
||||
ext.kotlin_version = '1.1.1'
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
testCompile 'junit:junit:4.12'
|
||||
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
|
||||
}
|
||||
test {
|
||||
testLogging {
|
||||
exceptionFormat = 'full'
|
||||
events = ["passed", "failed", "skipped"]
|
||||
}
|
||||
}
|
1
kotlin/pig-latin/build/kotlin-build/caches/version.txt
Normal file
1
kotlin/pig-latin/build/kotlin-build/caches/version.txt
Normal file
|
@ -0,0 +1 @@
|
|||
11001
|
Binary file not shown.
BIN
kotlin/pig-latin/out/production/classes/PigLatin$Companion.class
Normal file
BIN
kotlin/pig-latin/out/production/classes/PigLatin$Companion.class
Normal file
Binary file not shown.
BIN
kotlin/pig-latin/out/production/classes/PigLatin.class
Normal file
BIN
kotlin/pig-latin/out/production/classes/PigLatin.class
Normal file
Binary file not shown.
BIN
kotlin/pig-latin/out/production/classes/PigLatinKt.class
Normal file
BIN
kotlin/pig-latin/out/production/classes/PigLatinKt.class
Normal file
Binary file not shown.
BIN
kotlin/pig-latin/out/test/classes/PigLatinTest$Companion.class
Normal file
BIN
kotlin/pig-latin/out/test/classes/PigLatinTest$Companion.class
Normal file
Binary file not shown.
BIN
kotlin/pig-latin/out/test/classes/PigLatinTest.class
Normal file
BIN
kotlin/pig-latin/out/test/classes/PigLatinTest.class
Normal file
Binary file not shown.
13
kotlin/pig-latin/pig-latin.iml
Normal file
13
kotlin/pig-latin/pig-latin.iml
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id="pig-latin" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/out" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
0
kotlin/pig-latin/src/main/kotlin/.keep
Normal file
0
kotlin/pig-latin/src/main/kotlin/.keep
Normal file
31
kotlin/pig-latin/src/main/kotlin/PigLatin.kt
Normal file
31
kotlin/pig-latin/src/main/kotlin/PigLatin.kt
Normal file
|
@ -0,0 +1,31 @@
|
|||
class PigLatin {
|
||||
companion object {
|
||||
fun translate(inpString: String): String {
|
||||
val stringList: List<String> = inpString.split(' ')
|
||||
val returnStringBuilder = StringBuilder()
|
||||
for (i in stringList){
|
||||
returnStringBuilder.append(i.convertToPigLatin()).append(" ")
|
||||
}
|
||||
return String(returnStringBuilder).trim()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun Char.isVowel(): Boolean{
|
||||
val lowercase = this.toLowerCase()
|
||||
return ((lowercase == 'a') or (lowercase == 'e') or (lowercase == 'i') or (lowercase == 'o') or (lowercase == 'u'))
|
||||
}
|
||||
|
||||
fun String.convertToPigLatin(): String{
|
||||
val returnStringBuilder = StringBuilder()
|
||||
if (this.first().isVowel()){
|
||||
returnStringBuilder.append(this + "ay")
|
||||
}
|
||||
else {
|
||||
// TODO: Start accounting for edge cases
|
||||
val (first, temp) = this.partition { it === this.first() }
|
||||
returnStringBuilder.append(temp + first + "ay")
|
||||
}
|
||||
return String(returnStringBuilder)
|
||||
}
|
61
kotlin/pig-latin/src/test/kotlin/PigLatinTest.kt
Normal file
61
kotlin/pig-latin/src/test/kotlin/PigLatinTest.kt
Normal file
|
@ -0,0 +1,61 @@
|
|||
import org.junit.Test
|
||||
import org.junit.Ignore
|
||||
import org.junit.runner.RunWith
|
||||
import org.junit.runners.Parameterized
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@RunWith(Parameterized::class)
|
||||
class PigLatinTest(val input: String, val expectedOutput: String) {
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
@Parameterized.Parameters(name = "{index}: translate({0})={1}")
|
||||
fun data() = arrayOf(
|
||||
// Ay is added to words that start with vowels
|
||||
arrayOf("apple", "appleay"),
|
||||
arrayOf("ear", "earay"),
|
||||
arrayOf("igloo", "iglooay"),
|
||||
arrayOf("object", "objectay"),
|
||||
arrayOf("under", "underay"),
|
||||
|
||||
// First letter and ay are moved to the end of words that start with consonants
|
||||
arrayOf("pig", "igpay"),
|
||||
arrayOf("koala", "oalakay"),
|
||||
arrayOf("yellow", "ellowyay"),
|
||||
arrayOf("xenon", "enonxay"),
|
||||
|
||||
// Ch is treated like a single consonant
|
||||
arrayOf("chair", "airchay"),
|
||||
|
||||
// Qu is treated like a single consonant
|
||||
arrayOf("queen", "eenquay"),
|
||||
|
||||
// Qu and a single preceding consonant are treated like a single consonant
|
||||
arrayOf("square", "aresquay"),
|
||||
|
||||
// Th is treated like a single consonant
|
||||
arrayOf("therapy", "erapythay"),
|
||||
|
||||
// Thr is treated like a single consonant
|
||||
arrayOf("thrush", "ushthray"),
|
||||
|
||||
// Sch is treated like a single consonant
|
||||
arrayOf("school", "oolschay"),
|
||||
|
||||
// Yt is treated like a single vowel
|
||||
arrayOf("yttria", "yttriaay"),
|
||||
|
||||
// Xr is treated like a single vowel
|
||||
arrayOf("xray", "xrayay"),
|
||||
|
||||
// Phrases are translated
|
||||
arrayOf("quick fast run", "ickquay astfay unray")
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
assertEquals(expectedOutput, PigLatin.translate(input))
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue