-
Notifications
You must be signed in to change notification settings - Fork 0
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
Toni Day 4 #11
base: master
Are you sure you want to change the base?
Toni Day 4 #11
Conversation
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.
Quite good. Using Kotlin collection operators will help you a lot 👍
|
||
fun valid(passphrase: String): Boolean { | ||
val list = LinkedList(passphrase.split(" ")) | ||
val element = list.poll() |
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.
I suppose you are using LinkedList
to get Dequeue
behaviour. But did you know you could the same with an immutable list? (Just a bit uglier though, but in Kategory they have implemented methods for (x : xs)
haskell notation like)
val list = listOf(1,2,3)
val (head, tail) = Pair(list.first, list.drop(1))
// head -> 1
// tail -> listOf(2,3)
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 are droping in the same way, Yes I did with LinkedList to work like (x :xs)
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.
Except, drop
is immutable. It returns a new list.
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.
Yes, In kategory, we have several issues with OutOfMemory with drop in ListKW.fold
for many elements 👎
else { | ||
val element = passphrase.poll() | ||
validR(passphrase, element) | ||
} |
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.
Why are you using tailrec
? Did the IDE suggested it to you or you knew you had to use it?
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.
No! tailrec is for recursion optimization, tailrec coerce to call the method only in the last operation in return
https://kotlinlang.org/docs/reference/functions.html and https://medium.com/@JorgeCastilloPr/tail-recursion-and-how-to-use-it-in-kotlin-97353993e17f
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.
Interesting, I will search if Scala has a tailrec
reserved word too 👍
} | ||
|
||
fun countValid(passphrase: List<String>): Int = passphrase | ||
.fold(0) { acc, a -> if (valid(a)) acc + 1 else acc } |
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 could simplify this like this
fun countValid(passphrase: List<String>): Int = passphrase.count(::valid)
acc | ||
} | ||
} | ||
} |
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.
I guess you could simplify this to
return words.all { word -> words.count { it == word } <= 1 }
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.
wooooot?
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.
all
checks all the elements in a collection fulfill a predicate.
count
counts how many items in a collection fulfill a predicate.
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.
too much magic behind this methods
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.
Declarative magic 😉
fun `in list with 5 passphrase elements only 2 are valid`() { | ||
assertEquals(2, countValid(listOf( | ||
"aa bb cc dd cc", | ||
"miguel say my name miguel", |
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.
💘
|
||
fun equalsInAnyOrder(a: String, b: String): Boolean = a.fold(true) { acc, it -> | ||
if (a.length != b.length) false else acc && b.contains(it) | ||
} |
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.
This is okay but you can check if you strings are anagrams if you sort them and they are equal. That will simplify your solution a lot 👍
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.
Yes but I don't what is better for performance? good thought
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.
I don't know what will be the cost of sort
integrated method. But right now you have 4 nested loops. @JoseLlorensRipolles and me only have 2, I think, and we are using this approach.
Please NOT MERGE THIS PR I want see the @Cotel simplifications another day! |
Toni's solution for day 4 challenge
Description
Easy day, I have created the recursive and iterative solution.
But the Recursive one, has parts with iterative loops :(
any ideas
??Solution
Part 1
Part 2
Tests battery
always the same hahah
Happy Advent of Code!