Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d l mcbride solutions for day 2 to day 6 #380

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Day2/Kotlin/ReverseAndPalinedrome.kts
Original file line number Diff line number Diff line change
@@ -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")
43 changes: 43 additions & 0 deletions Day2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,49 @@ public class StringReverseAndPalin {
}
```

## Kotlin Implementation

### [ReverseAndPalindrome.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.
Expand Down
27 changes: 27 additions & 0 deletions Day3/Kotlin/HammingDistance.kts
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to verify that the two strings are of the same length before checking for the hamming distance between the two strings.

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)}")
39 changes: 39 additions & 0 deletions Day3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down Expand Up @@ -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?

Expand Down
50 changes: 50 additions & 0 deletions day4/Kotlin/NumVowelsAndMaxChars.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @author: d-l-mcbride
* @date: 10/11/2020
**/

fun numberOfVowels(subject: String):Int{
val vowels = hashSetOf<Char>('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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should ideally have test cases where the character appearing most frequently does not appear in consecutive positions.

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)}")
61 changes: 60 additions & 1 deletion day4/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Char>('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) :)
In case you have an alternative way to solve this problem, do contribute to this repository (https://github.com/CodeToExpress/dailycodebase) :)
31 changes: 31 additions & 0 deletions day5/Kotlin/Patterns.kts
Original file line number Diff line number Diff line change
@@ -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){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun patterThree(longestLine: Int){
fun patternThree(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} ") }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(line<=5) (1..line).forEach { pos ->print("${pos} ") } else (1..(2*longestLine-line)).forEach {less ->print("${less} ") }
if(line<=longestLine) (1..line).forEach { pos ->print("${pos} ") } else (1..(2*longestLine-line)).forEach {less ->print("${less} ") }

println()
}
}
patternOne(5)
patternTwo(lines = 4)
patterThree(5)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
patterThree(5)
patternThree(5)

50 changes: 50 additions & 0 deletions day6/Kotlin/Day6.kts
Original file line number Diff line number Diff line change
@@ -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")}")
Loading