diff --git a/kotlin/binary-search/.idea/workspace.xml b/kotlin/binary-search/.idea/workspace.xml deleted file mode 100644 index 34791e5..0000000 --- a/kotlin/binary-search/.idea/workspace.xml +++ /dev/null @@ -1,754 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @Ignore - - - - - - - - - - - - - - - - - - true - DEFINITION_ORDER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1501810006237 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1.8 - - - - - - - - \ No newline at end of file diff --git a/kotlin/sublist/README.md b/kotlin/sublist/README.md deleted file mode 100644 index 42089a9..0000000 --- a/kotlin/sublist/README.md +++ /dev/null @@ -1,24 +0,0 @@ -# Sublist - -Given two lists determine if the first list is contained within the second -list, if the second list is contained within the first list, if both lists are -contained within each other or if none of these are true. - -Specifically, a list A is a sublist of list B if by dropping 0 or more elements -from the front of B and 0 or more elements from the back of B you get a list -that's completely equal to A. - -Examples: - - * A = [1, 2, 3], B = [1, 2, 3, 4, 5], A is a sublist of B - * A = [3, 4, 5], B = [1, 2, 3, 4, 5], A is a sublist of B - * A = [3, 4], B = [1, 2, 3, 4, 5], A is a sublist of B - * A = [1, 2, 3], B = [1, 2, 3], A is equal to B - * A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B - * A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B - - - - -## Submitting Incomplete Solutions -It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/kotlin/sublist/build.gradle b/kotlin/sublist/build.gradle deleted file mode 100644 index 16c36c0..0000000 --- a/kotlin/sublist/build.gradle +++ /dev/null @@ -1,28 +0,0 @@ -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"] - } -} diff --git a/kotlin/sublist/build/kotlin-build/caches/version.txt b/kotlin/sublist/build/kotlin-build/caches/version.txt new file mode 100644 index 0000000..01aabac --- /dev/null +++ b/kotlin/sublist/build/kotlin-build/caches/version.txt @@ -0,0 +1 @@ +11001 \ No newline at end of file diff --git a/kotlin/sublist/out/production/classes/ListExtKt.class b/kotlin/sublist/out/production/classes/ListExtKt.class new file mode 100644 index 0000000..6c1af62 Binary files /dev/null and b/kotlin/sublist/out/production/classes/ListExtKt.class differ diff --git a/kotlin/sublist/out/production/classes/Relationship.class b/kotlin/sublist/out/production/classes/Relationship.class new file mode 100644 index 0000000..135efd6 Binary files /dev/null and b/kotlin/sublist/out/production/classes/Relationship.class differ diff --git a/kotlin/sublist/out/test/classes/SublistTest.class b/kotlin/sublist/out/test/classes/SublistTest.class new file mode 100644 index 0000000..ffb5706 Binary files /dev/null and b/kotlin/sublist/out/test/classes/SublistTest.class differ diff --git a/kotlin/sublist/src/main/kotlin/ListExt.kt b/kotlin/sublist/src/main/kotlin/ListExt.kt index 94cdefe..11a2674 100644 --- a/kotlin/sublist/src/main/kotlin/ListExt.kt +++ b/kotlin/sublist/src/main/kotlin/ListExt.kt @@ -1,43 +1,41 @@ -fun List.relationshipTo(inpList: List): Relationship{ - - if (this == inpList){ +fun List.relationshipTo(testList: List): Relationship { + if (testList == this) { return Relationship.EQUAL } - if (this.isEmpty()){ - return Relationship.SUBLIST - } - - if (inpList.isEmpty()){ + if (this.isSuperlist(testList)){ return Relationship.SUPERLIST } - if (this.subtract(inpList).isNotEmpty()){ - return Relationship.SUPERLIST - } - - if (inpList.subtract(this).isNotEmpty()) { + if (testList.isSuperlist(this)){ return Relationship.SUBLIST } return Relationship.UNEQUAL } -fun List.isUnequal(inpList: List): Boolean { - for (i in 0..this.size - 1){ - val droppedList = this.drop(i) - val droppedBackList = this.dropLast(i) - if (droppedList == inpList || droppedBackList == inpList){ - return false +fun List.isSuperlist(testList: List): Boolean{ + if (testList.isEmpty()){ + return true + } + + if (this.size < testList.size){ + return false + } + + if (testList.size >= this.size) { + if ((this == testList.subList(0, this.size)) or + (this == testList.subList(testList.size - this.size, testList.size))) { + return true } } - for (i in 0..inpList.size - 1){ - val droppedList = inpList.drop(i) - val droppedBackList = this.dropLast(i) - if (droppedList == inpList || droppedBackList == inpList){ - return false + val sizeDelta = this.size - testList.size + for (i in 0..sizeDelta){ + if (this.subList(i, i + testList.size) == testList){ + return true } } - return true + + return false } \ No newline at end of file diff --git a/kotlin/sublist/src/main/kotlin/Relationship.kt b/kotlin/sublist/src/main/kotlin/Relationship.kt index b8f5ab4..14b8b10 100644 --- a/kotlin/sublist/src/main/kotlin/Relationship.kt +++ b/kotlin/sublist/src/main/kotlin/Relationship.kt @@ -2,4 +2,4 @@ enum class Relationship { EQUAL, SUBLIST, SUPERLIST, UNEQUAL -} +} \ No newline at end of file diff --git a/kotlin/sublist/src/test/kotlin/SublistTest.kt b/kotlin/sublist/src/test/kotlin/SublistTest.kt index eee3eb9..e03c0e1 100644 --- a/kotlin/sublist/src/test/kotlin/SublistTest.kt +++ b/kotlin/sublist/src/test/kotlin/SublistTest.kt @@ -14,6 +14,7 @@ class SublistTest { emptyList().relationshipTo(emptyList())) } + @Test fun testEmptyListIsSublistOfNonEmptyList() { assertEquals( @@ -21,6 +22,7 @@ class SublistTest { emptyList().relationshipTo(listOf(1, 2, 3))) } + @Test fun testNonEmptyListIsSuperlistOfEmptyList() { assertEquals( @@ -28,6 +30,7 @@ class SublistTest { listOf('1', '2', '3').relationshipTo(emptyList())) } + @Test fun testListIsEqualToItself() { val anyList = listOf("1", "2", "3")