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

Solving Challenge #0 and #1 with Kotlin #1289

Open
wants to merge 2 commits into
base: main
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
17 changes: 17 additions & 0 deletions app/src/main/java/com/daniel/weeklychallenge2022/Challenge0.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Escribe un programa que muestre por consola (con un print) los
* números de 1 a 100 (ambos incluidos y con un salto de línea entre
* cada impresión) sustituyendo los siguientes:
* - Múltiplos de 3 por la palabra "fizz".
* - Múltiplos de 5 por la palabra "buzz".
* - Múltiplos de 3 y de 5 a la vez por la palabra "fizzbuzz".
*/

fun main() {
for (i in 1..100) {
if (i % 3 == 0 && i % 5 == 0) println("fizzbuzz")
else if (i % 3 == 0) println("buzz")
else if (i % 5 == 0) println("fizz")
else println(i)
}
}
34 changes: 34 additions & 0 deletions app/src/main/java/com/daniel/weeklychallenge2022/Challenge1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Escribe una función que reciba dos palabras (String) y retorne
* verdadero o falso (Bool) según sean o no anagramas.
* - Un Anagrama consiste en formar una palabra reordenando TODAS
* las letras de otra palabra inicial.
* - NO hace falta comprobar que ambas palabras existan.
* - Dos palabras exactamente iguales no son anagrama.
*/

fun main() {
println(verifyAnagrama("letras", "lastre")) // true
println(verifyAnagrama("aguardentosa", "engatusadora")) // true
println(verifyAnagrama("nacionalista", "altisonancia")) // true
println(verifyAnagrama("kklla", "kkadd")) // false
println(verifyAnagrama("pedrp", "pedro")) // false
}

fun verifyAnagrama (word1: String, word2: String): Boolean {
if (word1.length != word2.length) return false
if (word1 == word2) return false

var words1: MutableList<Char> = word1.map{it}.toMutableList()
var words2: MutableList<Char> = word2.map{it}.toMutableList()

for (char in word1) {
if (char in words1) {
words1.remove(char)
words2.remove(char)
}
}

if (words1.size == words2.size) return true
else return false
}