-
Notifications
You must be signed in to change notification settings - Fork 153
Day two Kotlin Hacktoberfest #359
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
Open
d-l-mcbride
wants to merge
17
commits into
CodeToExpress:master
Choose a base branch
from
d-l-mcbride:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3a7a843
Add @d-l-mcbride as a contributor
1982f90
Add Kotlin solution for string reversal and palindrome validation.
5099b85
Add Kotlin solution for Day3 and fix Typo in Day2 README.md
69485f4
Add Kotlin solution for day4.
3c48b37
Day one Kotlin for Hacktoberfest (#358)
d-l-mcbride 5626a6b
Add Kotlin solution for string reversal and palindrome validation.
816033c
Add Kotlin solution for Day3 and fix Typo in Day2 README.md
946689a
Add Kotlin solution for day4.
d6f5829
Merge branch 'master' of https://github.com/d-l-mcbride/dailycodebase
37cbb90
Add Kotlin solution for string reversal and palindrome validation.
c157ecb
Add Kotlin solution for Day3 and fix Typo in Day2 README.md
acafbd6
Add Kotlin solution for day4.
4857b80
Add Kotlin solution for string reversal and palindrome validation.
fa75ee1
Add Kotlin solution for Day3 and fix Typo in Day2 README.md
79ab4f4
Add check for different length strings
5495a78
Update README.md to reflect updated Kotlin solution
6f24e69
Merge branch 'master' of https://github.com/d-l-mcbride/dailycodebase
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @author: d-l-mcbride | ||
* @date: 10/11/2020 | ||
**/ | ||
|
||
fun fizzBuzz (fizzList: List<Int>) { | ||
|
||
var output: String | ||
|
||
fizzList.forEach { | ||
output = when { | ||
it % 15 == 0 -> "FizzBuzz" | ||
it % 5 == 0 -> "Buzz" | ||
it % 3 == 0 -> "Fizz" | ||
else -> "${it}" | ||
} | ||
println(output) | ||
} | ||
} | ||
|
||
fizzBuzz((1..15).toList()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @author: d-l-mcbride | ||
* @date: 10/12/2020 | ||
**/ | ||
|
||
fun hammingDistance(first: String, second: String ):Int { | ||
if (first.length != second.length) return -1 | ||
|
||
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" | ||
val DIFF_ONE ="ab" | ||
val DIFF_TWO ="abcd" | ||
|
||
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)}") | ||
println("Hamming Distance of different length strings = ${hammingDistance(DIFF_ONE,DIFF_TWO)}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
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)}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to check whether the strings are of equal length before you calculate the Hamming distance between them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aaditkamat I have made the changes that were suggested
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aaditkamat or @MadhavBahl I know you are probably busy but could you verify my changes, since I was trying to get my pull request done for Hacktoberfest. Unfortunately this pull request is actually many day2-4 (forgot to make branches for each). Could you add the "hacktoberfest-accepted" label to this. Thank you for your help!