From 5bccf391c610e91209347bcbd3ee5bbde51b036a Mon Sep 17 00:00:00 2001 From: d-l-mcbride Date: Sun, 11 Oct 2020 22:15:56 -0700 Subject: [PATCH 1/5] Add Kotlin solution for string reversal and palindrome validation. --- Day2/Kotlin/ReverseAndPalinedrome.kts | 34 +++++++++++++++++++++ Day2/README.md | 43 +++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 Day2/Kotlin/ReverseAndPalinedrome.kts diff --git a/Day2/Kotlin/ReverseAndPalinedrome.kts b/Day2/Kotlin/ReverseAndPalinedrome.kts new file mode 100644 index 00000000..cc542d6c --- /dev/null +++ b/Day2/Kotlin/ReverseAndPalinedrome.kts @@ -0,0 +1,34 @@ +/** + * @author: d-l-mcbride + * @date: 10/11/2020 + **/ + +fun reverse(subject:String):String { + var reversed = StringBuilder() + + for( j in (subject.length -1) downTo 0){ + reversed.append(subject[j]) + } + return reversed.toString() +} + + +fun palindrome(subject:String){ + val reversed = reverse(subject) + if (reversed == subject) println("${subject} is a palindrome") else println("${subject} is not a palindrome") +} + +val hello = "Hello" +val goodbye = "Goodbye" +val single = "S" + +println("Reverse String Tests") +println("${hello} reversed is ${reverse(hello)}") +println("${goodbye} reversed is ${reverse(goodbye)}") +println ("${single} reversed is ${reverse(single)}") + +println("\n\nPalindrome Tests") +palindrome("I") +palindrome("otto") +palindrome("tacocat") +palindrome("dog") diff --git a/Day2/README.md b/Day2/README.md index c0743faf..7834d9f4 100644 --- a/Day2/README.md +++ b/Day2/README.md @@ -981,6 +981,49 @@ public class StringReverseAndPalin { } ``` +## Kotlin Implementation + +### [FizzBuzz.kts](./Kotlin/ReverseAndPalindrome.kts) + +##### To run this script `kotlinc -script ReverseAndPalindrome.kts` + _*Prereq: Kotlin must be installed._ +``` +/** + * @author: d-l-mcbride + * @date: 10/11/2020 + **/ + +fun reverse(subject:String):String { + var reversed = StringBuilder() + + for( j in (subject.length -1) downTo 0){ + reversed.append(subject[j]) + } + return reversed.toString() +} + + +fun palindrome(subject:String){ + val reversed = reverse(subject) + if (reversed == subject) println("${subject} is a palindrome") else println("${subject} is not a palindrome") +} + +val hello = "Hello" +val goodbye = "Goodbye" +val single = "S" + +println("Reverse String Tests") +println("${hello} reversed is ${reverse(hello)}") +println("${goodbye} reversed is ${reverse(goodbye)}") +println ("${single} reversed is ${reverse(single)}") + +println("\n\nPalindrome Tests") +palindrome("I") +palindrome("otto") +palindrome("tacocat") +palindrome("dog") +``` + ### Have Another solution? The beauty of programming lies in the fact that there is never a single solution to any problem. From 6e0e2ecb4169450f1c42753970a2e21a3c8b9c1f Mon Sep 17 00:00:00 2001 From: d-l-mcbride Date: Mon, 12 Oct 2020 06:45:02 -0700 Subject: [PATCH 2/5] Add Kotlin solution for Day3 and fix Typo in Day2 README.md --- Day2/README.md | 2 +- Day3/Kotlin/HammingDistance.kts | 27 +++++++++++++++++++++++ Day3/README.md | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 Day3/Kotlin/HammingDistance.kts diff --git a/Day2/README.md b/Day2/README.md index 7834d9f4..e065cd0f 100644 --- a/Day2/README.md +++ b/Day2/README.md @@ -983,7 +983,7 @@ public class StringReverseAndPalin { ## Kotlin Implementation -### [FizzBuzz.kts](./Kotlin/ReverseAndPalindrome.kts) +### [ReverseAndPalindrome.kts](./Kotlin/ReverseAndPalindrome.kts) ##### To run this script `kotlinc -script ReverseAndPalindrome.kts` _*Prereq: Kotlin must be installed._ diff --git a/Day3/Kotlin/HammingDistance.kts b/Day3/Kotlin/HammingDistance.kts new file mode 100644 index 00000000..122aa459 --- /dev/null +++ b/Day3/Kotlin/HammingDistance.kts @@ -0,0 +1,27 @@ +/** + * @author: d-l-mcbride + * @date: 10/12/2020 + **/ + +fun hammingDistance(first: String, second: String ):Int { + var distance = 0 + for(i in first.indices) { + if (first[i] != second[i]) distance++ + } + return distance +} + +val DOG = "dog" +val LOG = "log" +val LARRY = "larry" +val LASSI ="lassi" +val EMPTY = "" +val ALSO_EMPTY = "" +val ABCDE = "abcde" +val BADEC = "badec" + +println("Hamming Distance of ${DOG} and ${LOG} = ${hammingDistance(DOG,LOG)}") +println("Hamming Distance of ${LOG} and ${LOG} = ${hammingDistance(LOG,LOG)}") +println("Hamming Distance of ${LARRY} and ${LASSI} = ${hammingDistance(LARRY,LASSI)}") +println("Hamming Distance of ${ABCDE} and ${BADEC} = ${hammingDistance(ABCDE,BADEC)}") +println("Hamming Distance of two empty strings = ${hammingDistance(EMPTY,ALSO_EMPTY)}") diff --git a/Day3/README.md b/Day3/README.md index dfcc3a21..2a81cde2 100644 --- a/Day3/README.md +++ b/Day3/README.md @@ -402,6 +402,8 @@ print("list of unmatched characters are:",k) print("number of characters not matching are:",len(k)) ``` + +``` /* @date 4/09/2020 * @author Shashwat Gupta (shashwatxdevelop) */ @@ -434,7 +436,44 @@ public class HammingDistance { } } +``` +## Kotlin Implementation + +### [HammingDistance.kts](./Kotlin/HammingDistance.kts) + +##### To run this script `kotlinc -script HammingDistance.kts` + _*Prereq: Kotlin must be installed._ + +``` +/** + * @author: d-l-mcbride + * @date: 10/12/2020 + **/ + +fun hammingDistance(first: String, second: String ):Int { + var distance = 0 + for(i in first.indices) { + if (first[i] != second[i]) distance++ + } + return distance +} + +val DOG = "dog" +val LOG = "log" +val LARRY = "larry" +val LASSI ="lassi" +val EMPTY = "" +val ALSO_EMPTY = "" +val ABCDE = "abcde" +val BADEC = "badec" + +println("Hamming Distance of ${DOG} and ${LOG} = ${hammingDistance(DOG,LOG)}") +println("Hamming Distance of ${LOG} and ${LOG} = ${hammingDistance(LOG,LOG)}") +println("Hamming Distance of ${LARRY} and ${LASSI} = ${hammingDistance(LARRY,LASSI)}") +println("Hamming Distance of ${ABCDE} and ${BADEC} = ${hammingDistance(ABCDE,BADEC)}") +println("Hamming Distance of two empty strings = ${hammingDistance(EMPTY,ALSO_EMPTY)}") +``` ## Why Hamming Distance? From 4999757934b4b415610677f09e221598d71478af Mon Sep 17 00:00:00 2001 From: d-l-mcbride Date: Mon, 12 Oct 2020 18:27:33 -0700 Subject: [PATCH 3/5] Add Kotlin solution for day4. --- day4/Kotlin/NumVowelsAndMaxChars.kts | 50 +++++++++++++++++++++++ day4/README.md | 61 +++++++++++++++++++++++++++- 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 day4/Kotlin/NumVowelsAndMaxChars.kts diff --git a/day4/Kotlin/NumVowelsAndMaxChars.kts b/day4/Kotlin/NumVowelsAndMaxChars.kts new file mode 100644 index 00000000..58e4d08b --- /dev/null +++ b/day4/Kotlin/NumVowelsAndMaxChars.kts @@ -0,0 +1,50 @@ +/** + * @author: d-l-mcbride + * @date: 10/11/2020 + **/ + +fun numberOfVowels(subject: String):Int{ + val vowels = hashSetOf('a','e','i','o','u') + var count = 0 + subject.forEach { + when(it.toLowerCase()) { + in vowels -> count++ + } + } + return count +} + +fun maxChars(subject: String):Char { + var maxChar = subject[0] + var maxCount = 0 + var count:Int + for(i in 0 until subject.length -1) { + count = 0 + for(j in i+1 until subject.length) { + if (subject[i] == subject[j]) count++ + } + if( count > maxCount) { + maxCount = count + maxChar = subject[i] + } + } + return maxChar +} + + + +val TWO_VOWELS = "hello" +val TWO_VOWELS_WITH_CAPS = "HELLO" +val NO_VOWELS = "qrzmtlnz" +println("${TWO_VOWELS} contains ${numberOfVowels(TWO_VOWELS)} vowels.") +println("${TWO_VOWELS_WITH_CAPS} contains ${numberOfVowels(TWO_VOWELS_WITH_CAPS)} vowels.") +println("${NO_VOWELS} contains ${numberOfVowels(NO_VOWELS)} vowels.") + +val THREE_MID = "helllo" +val ONE_EACH = "abcd" +val FOUR_END = "byeeee" +val FOUR_BEG = "yyyyes" +println("The letter with the most occurences in ${THREE_MID} is ${maxChars(THREE_MID)}") +println("The letter with the most occurences in ${ONE_EACH} is ${maxChars(ONE_EACH)}") +println("The letter with the most occurences in ${FOUR_END} is ${maxChars(FOUR_END)}") +println("The letter with the most occurences in ${FOUR_BEG} is ${maxChars(FOUR_BEG)}") diff --git a/day4/README.md b/day4/README.md index e9c5f930..dd577e03 100644 --- a/day4/README.md +++ b/day4/README.md @@ -887,10 +887,69 @@ print "Enter a string: " str = gets str.chomp! puts "The most frequent character in #{str} is : #{most_frequent_character(str)}" +``` +## Kotlin Implementation + +### [NumVowelsAndMaxChars.kts](./Kotlin/NumVowelsAndMaxChars.kts) + +##### To run this script `kotlinc -script NumVowelsAndMaxChars.kts` + +``` +/** + * @author: d-l-mcbride + * @date: 10/11/2020 + **/ + +fun numberOfVowels(subject: String):Int{ + val vowels = hashSetOf('a','e','i','o','u') + var count = 0 + subject.forEach { + when(it.toLowerCase()) { + in vowels -> count++ + } + } + return count +} + +fun maxChars(subject: String):Char { + var maxChar = subject[0] + var maxCount = 0 + var count:Int + for(i in 0 until subject.length -1) { + count = 0 + for(j in i+1 until subject.length) { + if (subject[i] == subject[j]) count++ + } + if( count > maxCount) { + maxCount = count + maxChar = subject[i] + } + } + return maxChar +} + + + +val TWO_VOWELS = "hello" +val TWO_VOWELS_WITH_CAPS = "HELLO" +val NO_VOWELS = "qrzmtlnz" +println("${TWO_VOWELS} contains ${numberOfVowels(TWO_VOWELS)} vowels.") +println("${TWO_VOWELS_WITH_CAPS} contains ${numberOfVowels(TWO_VOWELS_WITH_CAPS)} vowels.") +println("${NO_VOWELS} contains ${numberOfVowels(NO_VOWELS)} vowels.") + +val THREE_MID = "helllo" +val ONE_EACH = "abcd" +val FOUR_END = "byeeee" +val FOUR_BEG = "yyyyes" +println("The letter with the most occurences in ${THREE_MID} is ${maxChars(THREE_MID)}") +println("The letter with the most occurences in ${ONE_EACH} is ${maxChars(ONE_EACH)}") +println("The letter with the most occurences in ${FOUR_END} is ${maxChars(FOUR_END)}") +println("The letter with the most occurences in ${FOUR_BEG} is ${maxChars(FOUR_BEG)}") + ``` ### Have Another solution? The beauty of programming lies in the fact that there is never a single solution to any problem. -In case you have an alternative way to solve this problem, do contribute to this repository (https://github.com/CodeToExpress/dailycodebase) :) \ No newline at end of file +In case you have an alternative way to solve this problem, do contribute to this repository (https://github.com/CodeToExpress/dailycodebase) :) From 610717333d30f5a46cf70190649f241af4f2a0f9 Mon Sep 17 00:00:00 2001 From: d-l-mcbride Date: Fri, 30 Oct 2020 17:03:11 -0700 Subject: [PATCH 4/5] day 3 wip --- day5/Kotlin/Patterns.kts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 day5/Kotlin/Patterns.kts diff --git a/day5/Kotlin/Patterns.kts b/day5/Kotlin/Patterns.kts new file mode 100644 index 00000000..87ae6673 --- /dev/null +++ b/day5/Kotlin/Patterns.kts @@ -0,0 +1,31 @@ +/** + * @author: d-l-mcbride + * @date: 10/13/2020 + **/ + +fun patternOne(lines: Int) { + for(i in 1..lines){ + for(j in 1..i){ + print(if (j != i) "${j} " else j ) + } + println() + } +} +fun patternTwo(lines: Int) { + var count = 1 + for(i in 1..lines){ + for(j in 1..i){ + print( if(j != i) "${count++} " else count++) + } + println() + } +} +fun patterThree(longestLine: Int){ + (1..(longestLine*2-1)).forEach {line -> + if(line<=5) (1..line).forEach { pos ->print("${pos} ") } else (1..(2*longestLine-line)).forEach {less ->print("${less} ") } + println() + } +} +patternOne(5) +patternTwo(lines = 4) +patterThree(5) From 1de9d1b40c66cfdf3cffb6561693407dde4507e6 Mon Sep 17 00:00:00 2001 From: d-l-mcbride Date: Fri, 30 Oct 2020 23:56:57 -0700 Subject: [PATCH 5/5] Add Kotlin implementation for Daily Codes Day 6 --- day6/Kotlin/Day6.kts | 50 +++++++++++++++++++++++++++++++++++ day6/README.md | 62 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 day6/Kotlin/Day6.kts diff --git a/day6/Kotlin/Day6.kts b/day6/Kotlin/Day6.kts new file mode 100644 index 00000000..53d5b8e3 --- /dev/null +++ b/day6/Kotlin/Day6.kts @@ -0,0 +1,50 @@ +/** + * @author: d-l-mcbride + * @date: 10/30/2020 + **/ + +fun capitalizeEachWord(sentence: String): String { + if (sentence.length == 0) return sentence + + return sentence.split(" ").map { + it.capitalize() + }.joinToString(" ") +} + +fun reverseEachWords(sentence : String):String { + if (sentence.length == 0) return sentence + + return sentence.split(" ").map { + it.reversed() + }.joinToString(" ") +} + +fun anagramCheck(firstWord: String, secondWord: String): Boolean { + if(firstWord.length != secondWord.length) return false + var temp = secondWord + firstWord.forEach { + if(temp.contains(it)) temp = temp.replaceFirst(it.toString(),"") + } + return temp.length == 0 +} + +val testSentence = "this all is capitalized" +val shortSentence ="short" +val emptySentence ="" + +println("Capitalize Each Word Tests") +println(capitalizeEachWord(testSentence)) +println(capitalizeEachWord(shortSentence)) +println(capitalizeEachWord(emptySentence)) + +println("Reverse Each Word Tests") +println(reverseEachWords(testSentence)) +println(reverseEachWords(shortSentence)) +println(reverseEachWords(emptySentence)) + +println("Anagram Tests") +println("Is anagram ${anagramCheck("one","one")}") +println("Is anagram ${anagramCheck("one","oNe")}") +println("Is anagram ${anagramCheck("exit","xeti")}") +println("Is anagram ${anagramCheck("exxit","xetix")}") +println("Is anagram ${anagramCheck("exxit","retix")}") diff --git a/day6/README.md b/day6/README.md index 00b49ce9..bb9e9e25 100644 --- a/day6/README.md +++ b/day6/README.md @@ -1096,10 +1096,70 @@ puts "Enter two strings: " first_str = gets().chomp second_str = gets().chomp puts "\nAre #{first_str} and #{second_str} anagrams? #{check_anagram(String.new(first_str), String.new(second_str))}" +``` +## Kotlin Implementation + +### [Day6.kts](./Kotlin/Day6.kts) + +##### To run this script `kotlinc -script Day6.kts` + _*Prereq: Kotlin must be installed._ + +``` +/** + * @author: d-l-mcbride + * @date: 10/30/2020 + **/ + +fun capitalizeEachWord(sentence: String): String { + if (sentence.length == 0) return sentence + + return sentence.split(" ").map { + it.capitalize() + }.joinToString(" ") +} + +fun reverseEachWords(sentence : String):String { + if (sentence.length == 0) return sentence + + return sentence.split(" ").map { + it.reversed() + }.joinToString(" ") +} + +fun anagramCheck(firstWord: String, secondWord: String): Boolean { + if(firstWord.length != secondWord.length) return false + var temp = secondWord + firstWord.forEach { + if(temp.contains(it)) temp = temp.replaceFirst(it.toString(),"") + } + return temp.length == 0 +} + +val testSentence = "this all is capitalized" +val shortSentence ="short" +val emptySentence ="" + +println("Capitalize Each Word Tests") +println(capitalizeEachWord(testSentence)) +println(capitalizeEachWord(shortSentence)) +println(capitalizeEachWord(emptySentence)) + +println("Reverse Each Word Tests") +println(reverseEachWords(testSentence)) +println(reverseEachWords(shortSentence)) +println(reverseEachWords(emptySentence)) + +println("Anagram Tests") +println("Is anagram ${anagramCheck("one","one")}") +println("Is anagram ${anagramCheck("one","oNe")}") +println("Is anagram ${anagramCheck("exit","xeti")}") +println("Is anagram ${anagramCheck("exxit","xetix")}") +println("Is anagram ${anagramCheck("exxit","retix")}") + ``` ### Have Another solution? The beauty of programming lies in the fact that there is never a single solution to any problem. -In case you have an alternative way to solve this problem, do contribute to this repository (https://github.com/CodeToExpress/dailycodebase) :) \ No newline at end of file +In case you have an alternative way to solve this problem, do contribute to this repository (https://github.com/CodeToExpress/dailycodebase) :)