From 9cc80581be4f568c54fab19079bce54cc56cadc0 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:13:28 +0530 Subject: [PATCH 01/11] upgraded gradle version from 4.10 to 7.3.3 , kotlin to 1.6.10 --- .idea/misc.xml | 2 +- build.gradle | 4 ++-- gradle/wrapper/gradle-wrapper.properties | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 4b49563..1600b7e 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/build.gradle b/build.gradle index a301a3b..1051c0c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id 'java' - id 'org.jetbrains.kotlin.jvm' version '1.3.21' + id 'org.jetbrains.kotlin.jvm' version '1.6.10' } group 'main.kotlin.com' @@ -14,7 +14,7 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" - testCompile group: 'junit', name: 'junit', version: '4.12' + testImplementation group: 'junit', name: 'junit', version: '4.12' } compileKotlin { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 115e6ac..fe753c9 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists +zipStorePath=wrapper/dists \ No newline at end of file From 921d4a8e7c77f784d73ad49c70a3820b6a357576 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:15:13 +0530 Subject: [PATCH 02/11] updated package name --- src/main/kotlin/math/Factorial.kt | 2 +- src/test/kotlin/math/FactorialTest.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/math/Factorial.kt b/src/main/kotlin/math/Factorial.kt index e33be00..1e64d74 100644 --- a/src/main/kotlin/math/Factorial.kt +++ b/src/main/kotlin/math/Factorial.kt @@ -1,4 +1,4 @@ -package mathematics +package math import java.security.InvalidParameterException diff --git a/src/test/kotlin/math/FactorialTest.kt b/src/test/kotlin/math/FactorialTest.kt index 7706828..907b4c3 100644 --- a/src/test/kotlin/math/FactorialTest.kt +++ b/src/test/kotlin/math/FactorialTest.kt @@ -1,4 +1,4 @@ -package mathematics +package math import org.junit.Test import java.security.InvalidParameterException From 1fd4cc63686ef83cf5c95d9413b157fa83604fb2 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:17:53 +0530 Subject: [PATCH 03/11] optimized imports --- src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt | 2 +- src/main/kotlin/math/Area.kt | 1 - src/test/kotlin/math/AreaTest.kt | 1 - src/test/kotlin/math/Median.kt | 3 +-- 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt b/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt index e292317..b29f509 100644 --- a/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt +++ b/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt @@ -30,4 +30,4 @@ fun AssemblyLineScheduling(n:Int,a:Array,t:Array,e:IntArray, L2[n-1] + x[1]); -} +} \ No newline at end of file diff --git a/src/main/kotlin/math/Area.kt b/src/main/kotlin/math/Area.kt index a318708..64f27ae 100644 --- a/src/main/kotlin/math/Area.kt +++ b/src/main/kotlin/math/Area.kt @@ -1,6 +1,5 @@ package math -import java.lang.IllegalArgumentException import kotlin.math.pow /** diff --git a/src/test/kotlin/math/AreaTest.kt b/src/test/kotlin/math/AreaTest.kt index 8871c84..d87737a 100644 --- a/src/test/kotlin/math/AreaTest.kt +++ b/src/test/kotlin/math/AreaTest.kt @@ -1,7 +1,6 @@ package math import org.junit.Test -import java.lang.IllegalArgumentException class AreaTest { @Test diff --git a/src/test/kotlin/math/Median.kt b/src/test/kotlin/math/Median.kt index dd36b12..3506781 100644 --- a/src/test/kotlin/math/Median.kt +++ b/src/test/kotlin/math/Median.kt @@ -1,6 +1,5 @@ package math -import math.median import org.junit.Test class Median { @@ -21,4 +20,4 @@ class Median { val array = intArrayOf( 2, 3, 4, -2, -8, -3) assert(median(array) == 0.0) } -} +} \ No newline at end of file From a0e9dda69008c1766adaa1eed9975ab596689e36 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:23:39 +0530 Subject: [PATCH 04/11] updated deprecated method --- src/main/kotlin/other/Palindrome.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/other/Palindrome.kt b/src/main/kotlin/other/Palindrome.kt index 4a2dc13..8204d22 100644 --- a/src/main/kotlin/other/Palindrome.kt +++ b/src/main/kotlin/other/Palindrome.kt @@ -1,6 +1,7 @@ package other import java.text.Normalizer +import java.util.Locale import java.util.regex.Pattern /** @@ -30,7 +31,7 @@ fun String.normalize(): String { .compile("\\p{InCombiningDiacriticalMarks}+") .matcher(nfdNormalizedString) .replaceAll("") - .toLowerCase() + .lowercase(Locale.getDefault()) .replace(" ", "") } \ No newline at end of file From af158e5d4a1c295de3eaefdbc173ebc7ae484ea6 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:25:22 +0530 Subject: [PATCH 05/11] removed redundant semicolon --- .../dynamicProgramming/AssemblyLineScheduling.kt | 16 ++++++++-------- src/main/kotlin/other/Palindrome.kt | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt b/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt index b29f509..9389f5d 100644 --- a/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt +++ b/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt @@ -10,24 +10,24 @@ import kotlin.math.min * */ fun AssemblyLineScheduling(n:Int,a:Array,t:Array,e:IntArray,x:IntArray):Int { - var L1:IntArray = IntArray(n); - var L2:IntArray = IntArray(n) ; - var i=0; + var L1:IntArray = IntArray(n) + var L2:IntArray = IntArray(n) + var i=0 - L1[0] = e[0] + a[0][0]; + L1[0] = e[0] + a[0][0] - L2[0] = e[1] + a[1][0]; + L2[0] = e[1] + a[1][0] for(i in 1..n-1) { L1[i] = min(L1[i - 1] + a[0][i], - L2[i - 1] + t[1][i] + a[0][i]); + L2[i - 1] + t[1][i] + a[0][i]) L2[i] = min(L2[i - 1] + a[1][i], - L1[i - 1] + t[0][i] + a[1][i]); + L1[i - 1] + t[0][i] + a[1][i]) } return min(L1[n-1] + x[0], - L2[n-1] + x[1]); + L2[n-1] + x[1]) } \ No newline at end of file diff --git a/src/main/kotlin/other/Palindrome.kt b/src/main/kotlin/other/Palindrome.kt index 8204d22..902c96d 100644 --- a/src/main/kotlin/other/Palindrome.kt +++ b/src/main/kotlin/other/Palindrome.kt @@ -21,7 +21,7 @@ fun isPalindrome(text: String): Boolean { for(i in normalizedText.indices) if(normalizedText[i] != normalizedText[normalizedText.length - (i + 1)]) return false - return true; + return true } From e7cd960ebdbbff8c31371975d97a27df7ac45d7a Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:26:01 +0530 Subject: [PATCH 06/11] removed explicit type --- src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt b/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt index 9389f5d..6a85371 100644 --- a/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt +++ b/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt @@ -10,8 +10,8 @@ import kotlin.math.min * */ fun AssemblyLineScheduling(n:Int,a:Array,t:Array,e:IntArray,x:IntArray):Int { - var L1:IntArray = IntArray(n) - var L2:IntArray = IntArray(n) + var L1 = IntArray(n) + var L2 = IntArray(n) var i=0 L1[0] = e[0] + a[0][0] From a3664ea94c57bbf0edf0f002cda76ee645e44d8f Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:29:09 +0530 Subject: [PATCH 07/11] renamed variable according to official nomenclature --- .../AssemblyLineScheduling.kt | 23 +++++++++---------- src/main/kotlin/dynamicProgramming/LCS.kt | 10 ++++---- .../MatrixChainMultiplication.kt | 14 +++++------ .../AssemblyLineSchedulingTest.kt | 8 +++---- .../MatrixChainMultiplicationTest.kt | 4 ++-- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt b/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt index 6a85371..b04f108 100644 --- a/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt +++ b/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt @@ -8,26 +8,25 @@ import kotlin.math.min * @Params n- number of stations, a- service time at each station, t- line switch time from each station, e- entry time, x- exit time * @Return minimum time to cross n stations * */ -fun AssemblyLineScheduling(n:Int,a:Array,t:Array,e:IntArray,x:IntArray):Int { +fun assemblyLineScheduling(n:Int, a:Array, t:Array, e:IntArray, x:IntArray):Int { - var L1 = IntArray(n) - var L2 = IntArray(n) - var i=0 + var line1 = IntArray(n) + var line2 = IntArray(n) - L1[0] = e[0] + a[0][0] + line1[0] = e[0] + a[0][0] - L2[0] = e[1] + a[1][0] + line2[0] = e[1] + a[1][0] for(i in 1..n-1) { - L1[i] = min(L1[i - 1] + a[0][i], - L2[i - 1] + t[1][i] + a[0][i]) - L2[i] = min(L2[i - 1] + a[1][i], - L1[i - 1] + t[0][i] + a[1][i]) + line1[i] = min(line1[i - 1] + a[0][i], + line2[i - 1] + t[1][i] + a[0][i]) + line2[i] = min(line2[i - 1] + a[1][i], + line1[i - 1] + t[0][i] + a[1][i]) } - return min(L1[n-1] + x[0], - L2[n-1] + x[1]) + return min(line1[n-1] + x[0], + line2[n-1] + x[1]) } \ No newline at end of file diff --git a/src/main/kotlin/dynamicProgramming/LCS.kt b/src/main/kotlin/dynamicProgramming/LCS.kt index 7db3edd..ea8a4db 100644 --- a/src/main/kotlin/dynamicProgramming/LCS.kt +++ b/src/main/kotlin/dynamicProgramming/LCS.kt @@ -10,14 +10,14 @@ import kotlin.math.max */ fun lcs(s1: String, s2: String): Int { - val L = Array(s1.length + 1) { IntArray(s2.length + 1) } + val l = Array(s1.length + 1) { IntArray(s2.length + 1) } for (i in 0..s1.length) { for (j in 0..s2.length) { - if (i == 0 || j == 0) L[i][j] = 0 - else if (s1[i - 1] == s2[j - 1]) L[i][j] = L[i - 1][j - 1] + 1 - else L[i][j] = max(L[i - 1][j], L[i][j - 1]) + if (i == 0 || j == 0) l[i][j] = 0 + else if (s1[i - 1] == s2[j - 1]) l[i][j] = l[i - 1][j - 1] + 1 + else l[i][j] = max(l[i - 1][j], l[i][j - 1]) } } - return L[s1.length][s2.length] + return l[s1.length][s2.length] } \ No newline at end of file diff --git a/src/main/kotlin/dynamicProgramming/MatrixChainMultiplication.kt b/src/main/kotlin/dynamicProgramming/MatrixChainMultiplication.kt index aa47061..1a81871 100644 --- a/src/main/kotlin/dynamicProgramming/MatrixChainMultiplication.kt +++ b/src/main/kotlin/dynamicProgramming/MatrixChainMultiplication.kt @@ -5,13 +5,13 @@ package dynamicProgramming * @Params p- array which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i] * @Return minimum number of multiplications needed to multiply the chain * */ -fun MatrixChainOrder(p: IntArray): Int { +fun matrixChainOrder(p: IntArray): Int { val m = Array(p.size) { IntArray(p.size) } var i: Int var j: Int var k: Int - var L: Int + var l: Int var q: Int i = 1 @@ -20,11 +20,11 @@ fun MatrixChainOrder(p: IntArray): Int { i++ } - L = 2 - while (L < p.size) { + l = 2 + while (l < p.size) { i = 1 - while (i < p.size - L + 1) { - j = i + L - 1 + while (i < p.size - l + 1) { + j = i + l - 1 if (j == p.size) { i++ continue @@ -40,7 +40,7 @@ fun MatrixChainOrder(p: IntArray): Int { } i++ } - L++ + l++ } return m[1][p.size - 1] } \ No newline at end of file diff --git a/src/test/kotlin/dynamicProgramming/AssemblyLineSchedulingTest.kt b/src/test/kotlin/dynamicProgramming/AssemblyLineSchedulingTest.kt index daaa628..d0880fe 100644 --- a/src/test/kotlin/dynamicProgramming/AssemblyLineSchedulingTest.kt +++ b/src/test/kotlin/dynamicProgramming/AssemblyLineSchedulingTest.kt @@ -6,16 +6,16 @@ class AssemblyLineSchedulingTest{ @Test fun testWith6Stations() { - assert(AssemblyLineScheduling(6,arrayOf(intArrayOf(3, 7, 2, 5,5,3), intArrayOf(4, 1, 6, 7,5,3)),arrayOf(intArrayOf(0,2,3,4,5,6), intArrayOf(0,5,4,3,2,1)), intArrayOf(15,11), intArrayOf(16,5))== 42) + assert(assemblyLineScheduling(6,arrayOf(intArrayOf(3, 7, 2, 5,5,3), intArrayOf(4, 1, 6, 7,5,3)),arrayOf(intArrayOf(0,2,3,4,5,6), intArrayOf(0,5,4,3,2,1)), intArrayOf(15,11), intArrayOf(16,5))== 42) } @Test fun testWith2Stations() { - assert(AssemblyLineScheduling(2,arrayOf(intArrayOf(3, 7), intArrayOf(4, 1)),arrayOf(intArrayOf(0,2), intArrayOf(0,5)), intArrayOf(5,1), intArrayOf(6,5))== 11) + assert(assemblyLineScheduling(2,arrayOf(intArrayOf(3, 7), intArrayOf(4, 1)),arrayOf(intArrayOf(0,2), intArrayOf(0,5)), intArrayOf(5,1), intArrayOf(6,5))== 11) } @Test fun testWith4Stations() { - assert(AssemblyLineScheduling(4,arrayOf(intArrayOf(3, 7,6,2), intArrayOf(4, 1,6,2)),arrayOf(intArrayOf(0,2,6,1), intArrayOf(0,5,3,4)), intArrayOf(7,4), intArrayOf(5,8))== 25) + assert(assemblyLineScheduling(4,arrayOf(intArrayOf(3, 7,6,2), intArrayOf(4, 1,6,2)),arrayOf(intArrayOf(0,2,6,1), intArrayOf(0,5,3,4)), intArrayOf(7,4), intArrayOf(5,8))== 25) } -} +} \ No newline at end of file diff --git a/src/test/kotlin/dynamicProgramming/MatrixChainMultiplicationTest.kt b/src/test/kotlin/dynamicProgramming/MatrixChainMultiplicationTest.kt index a70f81f..ee6bdd4 100644 --- a/src/test/kotlin/dynamicProgramming/MatrixChainMultiplicationTest.kt +++ b/src/test/kotlin/dynamicProgramming/MatrixChainMultiplicationTest.kt @@ -5,11 +5,11 @@ import org.junit.Test class MatrixChainMultiplicationTest { @Test fun testWith5Matrices() { - assert(MatrixChainOrder(intArrayOf(30, 20, 40, 10, 30)) == 23000) + assert(matrixChainOrder(intArrayOf(30, 20, 40, 10, 30)) == 23000) } @Test fun testWith4Matrices() { - assert(MatrixChainOrder(intArrayOf(50, 20, 10, 30)) == 25000) + assert(matrixChainOrder(intArrayOf(50, 20, 10, 30)) == 25000) } } \ No newline at end of file From 4bd37dbb47b67f2d5ff456e73204ed9de06742f3 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:35:44 +0530 Subject: [PATCH 08/11] updated minor snippets to better align code with Kotlin --- .../dynamicProgramming/AssemblyLineScheduling.kt | 6 +++--- .../PalindromePartitioning.kt | 16 ++++++++-------- src/main/kotlin/search/BinarySearch.kt | 4 ++-- src/main/kotlin/sort/InsertionSort.kt | 2 +- src/test/kotlin/dynamicProgramming/isPrime.kt | 8 ++++---- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt b/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt index b04f108..1577ae1 100644 --- a/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt +++ b/src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt @@ -10,14 +10,14 @@ import kotlin.math.min * */ fun assemblyLineScheduling(n:Int, a:Array, t:Array, e:IntArray, x:IntArray):Int { - var line1 = IntArray(n) - var line2 = IntArray(n) + val line1 = IntArray(n) + val line2 = IntArray(n) line1[0] = e[0] + a[0][0] line2[0] = e[1] + a[1][0] - for(i in 1..n-1) + for(i in 1 until n) { line1[i] = min(line1[i - 1] + a[0][i], line2[i - 1] + t[1][i] + a[0][i]) diff --git a/src/main/kotlin/dynamic_programming/PalindromePartitioning.kt b/src/main/kotlin/dynamic_programming/PalindromePartitioning.kt index b4dd1f1..251bb1e 100644 --- a/src/main/kotlin/dynamic_programming/PalindromePartitioning.kt +++ b/src/main/kotlin/dynamic_programming/PalindromePartitioning.kt @@ -13,9 +13,9 @@ package dynamic_programming /** - * @param String is the string to be checked - * @param Int is the starting index of the string in consideration - * @param Int is the ending index of the string in consideration + * @param string is the string to be checked + * @param i is the starting index of the string in consideration + * @param j is the ending index of the string in consideration * @return whether string is a palindrome or not **/ fun isPalindrome(string: String, i: Int, j: Int): Boolean { @@ -29,9 +29,9 @@ fun isPalindrome(string: String, i: Int, j: Int): Boolean { /** - * @param String is the string to be checked - * @param Int is the starting index of the string in consideration - * @param Int is the ending index of the string in consideration + * @param string is the string to be checked + * @param i is the starting index of the string in consideration + * @param j is the ending index of the string in consideration * @return minimum number of partitions required **/ fun palindromePartition(string: String, i: Int, j: Int): Int { @@ -63,9 +63,9 @@ lateinit var dp: Array> /** - * @param String the string on which algorithm is to be operated + * @param string the string on which algorithm is to be operated */ fun initialize(string: String): Int { dp = Array(string.length) { Array(string.length) { -1 } } return palindromePartition(string, 0, string.length - 1) -} +} \ No newline at end of file diff --git a/src/main/kotlin/search/BinarySearch.kt b/src/main/kotlin/search/BinarySearch.kt index d3d5c5d..103d51a 100644 --- a/src/main/kotlin/search/BinarySearch.kt +++ b/src/main/kotlin/search/BinarySearch.kt @@ -32,7 +32,7 @@ fun > binarySearchHelper(array: Array, key: T, start: Int, return when { array[mid].compareTo(key) == 0 -> mid - array[mid].compareTo(key) > 0 -> binarySearchHelper(array, key, start, mid - 1) + array[mid] > key -> binarySearchHelper(array, key, start, mid - 1) else -> binarySearchHelper(array, key, mid + 1, end) } -} +} \ No newline at end of file diff --git a/src/main/kotlin/sort/InsertionSort.kt b/src/main/kotlin/sort/InsertionSort.kt index c3d4f04..1d69a9d 100644 --- a/src/main/kotlin/sort/InsertionSort.kt +++ b/src/main/kotlin/sort/InsertionSort.kt @@ -19,7 +19,7 @@ fun > insertionSort(array: Array) { var idx = i for (j in i - 1 downTo 0) { - if (array[j].compareTo(key) > 0) { + if (array[j] > key) { array[j + 1] = array[j] idx = j } else { diff --git a/src/test/kotlin/dynamicProgramming/isPrime.kt b/src/test/kotlin/dynamicProgramming/isPrime.kt index eed411f..b237fba 100644 --- a/src/test/kotlin/dynamicProgramming/isPrime.kt +++ b/src/test/kotlin/dynamicProgramming/isPrime.kt @@ -2,20 +2,20 @@ package dynamicProgramming import org.junit.Test -internal class isPrimeTest { +internal class IsPrimeTest { @Test fun testPrime1(){ - assert(2.isPrime()==true) + assert(2.isPrime()) } @Test fun testPrime2(){ - assert(53.isPrime()==true) + assert(53.isPrime()) } @Test fun testPrime3(){ - assert(4.isPrime()==false) + assert(!4.isPrime()) } } \ No newline at end of file From 91db505161e143ed13ba82168131464910a3757f Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:40:50 +0530 Subject: [PATCH 09/11] renamed code according to official nomenclature --- README.md | 4 ++-- .../dynamicProgramming/ZeroOneKnapsackProblem.kt | 2 +- src/main/kotlin/sort/HeapSort.kt | 11 +++++------ .../dynamicProgramming/ZeroOneKnapsackProblemTest.kt | 8 ++++---- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b0ceb78..ce708e6 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,11 @@ - Please do not add a signature inside the code. The commit history is sufficient to determine who has added the code to the repo. - Make sure the algorithm which is getting added comes under a certain domain of Algorithms. Please don't create a package with a name such as Misc, Others, etc. - While making a PR, make sure you are committing the Kotlin files only and not any project specific files. If you feel that your IDE is generating some extra files, then either don't add them to git, or add the extensions to ```.gitignore```. - - Please don't raise a PR for solutions to problems from online judges such as Hackerrank, Leetcode, etc. + - Please don't raise a PR for solutions to problems from online judges such as HackerRank, LeetCode, etc. ## Steps to raise a PR - Fork the [Kotlin Repo](https://github.com/TheAlgorithms/Kotlin) - Open the forked repo on your local machine through IntelliJ by importing the project as a Gradle project - Make the changes on your local machine - Push the changes to the forked repository -- Raise a PR against the master branch +- Raise a PR against the master branch \ No newline at end of file diff --git a/src/main/kotlin/dynamicProgramming/ZeroOneKnapsackProblem.kt b/src/main/kotlin/dynamicProgramming/ZeroOneKnapsackProblem.kt index 5460183..d1bf3be 100644 --- a/src/main/kotlin/dynamicProgramming/ZeroOneKnapsackProblem.kt +++ b/src/main/kotlin/dynamicProgramming/ZeroOneKnapsackProblem.kt @@ -8,7 +8,7 @@ import kotlin.math.max * @return Maximum value that can be obtained */ -fun zerooneknapsack(W:Int, weight: IntArray, values:IntArray, n:Int):Int{ +fun zeroOneKnapsack(W:Int, weight: IntArray, values:IntArray, n:Int):Int{ if (W<0) return 0 val k = Array(n+1) {IntArray(W+1) {0} } for (i in 0..n) diff --git a/src/main/kotlin/sort/HeapSort.kt b/src/main/kotlin/sort/HeapSort.kt index 7f70e65..6f9f80b 100644 --- a/src/main/kotlin/sort/HeapSort.kt +++ b/src/main/kotlin/sort/HeapSort.kt @@ -23,7 +23,7 @@ fun > heapSort(array: Array) { * @param array The array containing the elements * @param index Index of the currently largest element */ -fun > maxheapify(array: Array, heapSize: Int, index: Int) { +fun > maxHeapify(array: Array, heapSize: Int, index: Int) { val left = 2 * index + 1 val right = 2 * index + 2 var largest = index @@ -34,7 +34,7 @@ fun > maxheapify(array: Array, heapSize: Int, index: Int) { largest = right if (largest != index) { swapElements(array, index, largest) - maxheapify(array, heapSize, largest) + maxHeapify(array, heapSize, largest) } } @@ -46,7 +46,7 @@ fun > maxheapify(array: Array, heapSize: Int, index: Int) { private fun > buildMaxHeap(array: Array) { val n = array.size for (i in (n / 2 - 1) downTo 0) - maxheapify(array, n, i) + maxHeapify(array, n, i) } /** @@ -58,7 +58,6 @@ private fun > buildMaxHeap(array: Array) { private fun > transformMaxHeapToSortedArray(array: Array) { for (i in (array.size - 1) downTo 0) { swapElements(array, i, 0) - maxheapify(array, i, 0) + maxHeapify(array, i, 0) } -} - +} \ No newline at end of file diff --git a/src/test/kotlin/dynamicProgramming/ZeroOneKnapsackProblemTest.kt b/src/test/kotlin/dynamicProgramming/ZeroOneKnapsackProblemTest.kt index 101122f..b327dcc 100644 --- a/src/test/kotlin/dynamicProgramming/ZeroOneKnapsackProblemTest.kt +++ b/src/test/kotlin/dynamicProgramming/ZeroOneKnapsackProblemTest.kt @@ -5,21 +5,21 @@ import org.junit.Test class ZeroOneKnapsackProblemTest { @Test fun testBothWeightAndValueArrayHasDifferentValuesGivesExpectedOutput() { - assert(zerooneknapsack(5, intArrayOf(3,2,1), intArrayOf(20,50,30),3) == 80) + assert(zeroOneKnapsack(5, intArrayOf(3,2,1), intArrayOf(20,50,30),3) == 80) } @Test fun testBothWeightAndValueArrayHasSameValuesGivesExpectedOutput(){ - assert(zerooneknapsack(3, intArrayOf(2,2), intArrayOf(3,3),2)==3) + assert(zeroOneKnapsack(3, intArrayOf(2,2), intArrayOf(3,3),2)==3) } @Test fun testNegativeCapacityGivesZero(){ - assert(zerooneknapsack(-3, intArrayOf(2,2), intArrayOf(3,3),2)==0) + assert(zeroOneKnapsack(-3, intArrayOf(2,2), intArrayOf(3,3),2)==0) } @Test fun testZeroCapacityGivesZero(){ - assert(zerooneknapsack(0, intArrayOf(2,2), intArrayOf(3,3),2)==0) + assert(zeroOneKnapsack(0, intArrayOf(2,2), intArrayOf(3,3),2)==0) } } \ No newline at end of file From bc3aa8dbd8479ea1592a99cfef24cf60e7f71bb8 Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:51:24 +0530 Subject: [PATCH 10/11] Only documentation changes. Made all 'search' code doc in sync, formatted and more readable. --- src/main/kotlin/search/BinarySearch.kt | 6 +++--- src/main/kotlin/search/InterpolationSearch.kt | 6 +++--- src/main/kotlin/search/LinearSearch.kt | 10 +++++----- src/main/kotlin/search/TernarySearch.kt | 10 ++++------ 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/search/BinarySearch.kt b/src/main/kotlin/search/BinarySearch.kt index 103d51a..7c97a03 100644 --- a/src/main/kotlin/search/BinarySearch.kt +++ b/src/main/kotlin/search/BinarySearch.kt @@ -3,9 +3,9 @@ package search /** * Binary search is an algorithm which finds the position of a target value within an array (Sorted) * - * Worst-case performance O(log(n)) - * Best-case performance O(1) - * Average performance O(log(n)) + * Worst-case performance O(log(n)) + * Best-case performance O(1) + * Average performance O(log(n)) * Worst-case space complexity O(1) */ diff --git a/src/main/kotlin/search/InterpolationSearch.kt b/src/main/kotlin/search/InterpolationSearch.kt index e309334..9174b4a 100644 --- a/src/main/kotlin/search/InterpolationSearch.kt +++ b/src/main/kotlin/search/InterpolationSearch.kt @@ -3,9 +3,9 @@ package search /** * Interpolation search is an algorithm which finds the position of a target value within an array (Sorted) * - * Worst-case performance O(n) - * Best-case performance O(1) - * Average performance O(log log n) + * Worst-case performance O(n) + * Best-case performance O(1) + * Average performance O(log(log(n))) * Worst-case space complexity O(1) */ diff --git a/src/main/kotlin/search/LinearSearch.kt b/src/main/kotlin/search/LinearSearch.kt index b82837d..b284e16 100644 --- a/src/main/kotlin/search/LinearSearch.kt +++ b/src/main/kotlin/search/LinearSearch.kt @@ -3,11 +3,11 @@ package search /** * Linear search is an algorithm which finds the position of a target value within an array (Usually unsorted) * - * Worst-case performance O(n) - * Best-case performance O(1) - * Average performance O(n) + * Worst-case performance O(n) + * Best-case performance O(1) + * Average performance O(n) * Worst-case space complexity O(1) - */ + **/ /** * @param array is an array where the element should be found @@ -31,4 +31,4 @@ fun > linearSearchImpl(array: Array, key: T): Int { } return -1 -} +} \ No newline at end of file diff --git a/src/main/kotlin/search/TernarySearch.kt b/src/main/kotlin/search/TernarySearch.kt index 2d26e6a..7ba1789 100644 --- a/src/main/kotlin/search/TernarySearch.kt +++ b/src/main/kotlin/search/TernarySearch.kt @@ -3,11 +3,9 @@ package search /** * Complexity Analysis: * - * Time Complexity: O(log3 n) - * Space Complexity: O(1) - * - */ - + * Time Complexity: O(log3 n) + * Space Complexity: O(1) + **/ fun ternarySearch(l: Double, r: Double, func: (Double) -> Double, eps: Double = 1e-3): Double { var left = l var right = r @@ -21,4 +19,4 @@ fun ternarySearch(l: Double, r: Double, func: (Double) -> Double, eps: Double = } } return left -} +} \ No newline at end of file From fb8340a00a35556bc4cc1dd2fc17d8854e48466a Mon Sep 17 00:00:00 2001 From: Aman Date: Tue, 18 Jan 2022 22:51:52 +0530 Subject: [PATCH 11/11] Only documentation changes. Made all 'sort' code doc in sync, formatted and more readable. --- src/main/kotlin/sort/BrickSort.kt | 7 +++---- src/main/kotlin/sort/BubbleSort.kt | 8 ++++---- src/main/kotlin/sort/HeapSort.kt | 4 ++-- src/main/kotlin/sort/InsertionSort.kt | 6 +++--- src/main/kotlin/sort/MergeSort.kt | 8 ++++---- src/main/kotlin/sort/QuickSort.kt | 4 ++-- src/main/kotlin/sort/SelectionSort.kt | 8 ++++---- 7 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/main/kotlin/sort/BrickSort.kt b/src/main/kotlin/sort/BrickSort.kt index 47c222e..9550dda 100644 --- a/src/main/kotlin/sort/BrickSort.kt +++ b/src/main/kotlin/sort/BrickSort.kt @@ -6,12 +6,11 @@ package sort * @param array The array to be sorted * Sorts the array in increasing order * - * Worst-case performance O(n^2) - * Best-case performance O(n) - * Average performance O(n^2) + * Worst-case performance O(n^2) + * Best-case performance O(n) + * Average performance O(n^2) * Worst-case space complexity O(1) **/ - fun > oddEvenSort(array: Array) { var isSorted = false while (!isSorted) { diff --git a/src/main/kotlin/sort/BubbleSort.kt b/src/main/kotlin/sort/BubbleSort.kt index c9bc384..344b6f1 100644 --- a/src/main/kotlin/sort/BubbleSort.kt +++ b/src/main/kotlin/sort/BubbleSort.kt @@ -6,9 +6,9 @@ package sort * @param array The array to be sorted * Sorts the array in increasing order * - * Worst-case performance O(n^2) - * Best-case performance O(n) - * Average performance O(n^2) + * Worst-case performance O(n^2) + * Best-case performance O(n) + * Average performance O(n^2) * Worst-case space complexity O(1) **/ fun > bubbleSort(array: Array) { @@ -39,4 +39,4 @@ fun > swapElements(array: Array, idx1: Int, idx2: Int) { array[idx1] = array[idx2].also { array[idx2] = array[idx1] } -} +} \ No newline at end of file diff --git a/src/main/kotlin/sort/HeapSort.kt b/src/main/kotlin/sort/HeapSort.kt index 6f9f80b..f1aae09 100644 --- a/src/main/kotlin/sort/HeapSort.kt +++ b/src/main/kotlin/sort/HeapSort.kt @@ -8,9 +8,9 @@ package sort * * Worst-case performance O(n*log(n)) * Best-case performance O(n*log(n)) - * Average-case performance O(n*log(n)) + * Average performance O(n*log(n)) * Worst-case space complexity O(1) (auxiliary) - */ + **/ fun > heapSort(array: Array) { buildMaxHeap(array) transformMaxHeapToSortedArray(array) diff --git a/src/main/kotlin/sort/InsertionSort.kt b/src/main/kotlin/sort/InsertionSort.kt index 1d69a9d..a2bc0fd 100644 --- a/src/main/kotlin/sort/InsertionSort.kt +++ b/src/main/kotlin/sort/InsertionSort.kt @@ -6,9 +6,9 @@ package sort * @param array The array to be sorted * Sorts the array in increasing order * - * Worst-case performance O(n^2) - * Best-case performance O(n) - * Average performance O(n^2) + * Worst-case performance O(n^2) + * Best-case performance O(n) + * Average performance O(n^2) * Worst-case space complexity O(1) **/ fun > insertionSort(array: Array) { diff --git a/src/main/kotlin/sort/MergeSort.kt b/src/main/kotlin/sort/MergeSort.kt index c99bcf9..8cbca40 100644 --- a/src/main/kotlin/sort/MergeSort.kt +++ b/src/main/kotlin/sort/MergeSort.kt @@ -6,11 +6,11 @@ package sort * @param array The array to be sorted * It is a Divide and Conquer algorithm. It sorts the array by dividing it into two halves and comparing the elements. * - * Worst-case performance O(n log n) - * Best-case performance O(n log n) - * Average performance O(n log n) + * Worst-case performance O(n*log(n)) + * Best-case performance O(n*log(n)) + * Average performance O(n*log(n)) * Worst-case space complexity O(n) - */ + **/ fun > mergeSort(array: Array, start: Int, end: Int) { val temp = arrayOfNulls>(array.size) as Array diff --git a/src/main/kotlin/sort/QuickSort.kt b/src/main/kotlin/sort/QuickSort.kt index 7cb7fa7..555d98a 100644 --- a/src/main/kotlin/sort/QuickSort.kt +++ b/src/main/kotlin/sort/QuickSort.kt @@ -7,8 +7,8 @@ package sort * It is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. * * Worst-case performance O(n^2) - * Best-case performance O(nLogn) - * Average performance O(nLogn) + * Best-case performance O(n*log(n)) + * Average performance O(n*log(n)) * Worst-case space complexity O(1) **/ fun > quickSort(array: Array, low: Int, high: Int) { diff --git a/src/main/kotlin/sort/SelectionSort.kt b/src/main/kotlin/sort/SelectionSort.kt index ffe0a85..0525546 100644 --- a/src/main/kotlin/sort/SelectionSort.kt +++ b/src/main/kotlin/sort/SelectionSort.kt @@ -6,9 +6,9 @@ package sort * @param array The array to be sorted * Sorts the array by repeatedly finding the minimum element from unsorted part and putting in the beginning * - * Worst-case performance O(n^2) - * Best-case performance O(n^2) - * Average performance O(n^2) + * Worst-case performance O(n^2) + * Best-case performance O(n^2) + * Average performance O(n^2) * Worst-case space complexity O(1) **/ fun > selectionSort(array: Array) { @@ -24,4 +24,4 @@ fun > selectionSort(array: Array) { swapElements(array, i, idx) } -} +} \ No newline at end of file