You're going on a trip with some students and it's up to you to keep track of how much money each Student has. A student is defined like this:
class Student
:
def __init__(self, name, fives, tens, twenties)
:
self.name = name
self.fives = fives
self.tens = tens
self.twenties = twenties
As you can tell, each Student has some fives, tens, and twenties. Your job is to return the name of the student with the most money. If every student has the same amount, then return "all"
.
Notes:
- Each student will have a unique name
- There will always be a clear winner: either one person has the most, or everyone has the same amount
- If there is only one student, then that student has the most money
This challenge comes from MrAppa on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (16)
there is nothing more beautiful than Python
Usage:
Hi, check this decorator :
docs.python.org/3/library/functool...
Will make you avoid writing some functions.
Thank you Héctor i didn't know about this.
I've tried to apply it but it looks is a little shady for me, i didn't quite understand what is does and how it "magically" does the comparison
My first attempt at F#
I couldn't get the syntax highlighting for F# so if anyone can point me in the right direction that would be much appreciated.
renders as
(HEY, dev.to maintainers, this is one of those "won't fix bugs!" 😠 Why can't I nest backticks as in the github flavored markdown spec?)
Thank you
I put myself in here as a test case as a joke because I didn't get an allowance as a kid 😂
education.go
education_test.go
Javascript:
Usage:
Thank you Michael. I have been following your F# posts in this series and found them very helpful.
I thought about using a record type but thought it would be easier to use a class property for the match function.
If you get chance please could you show how you would approach the full solution.
Many thanks.
Ridiculously over-engineered Kotlin version:
It's typesafe, and we could change the comparator out if we wanted to. A little kludgey in the fold section (I should probably involve Option to avoid people accidentally calling money on the
Empty
andError
types. 🤷♀️While I love the
sealed class
idea in Kotlin, type erasure and lack of true pattern matching really hampers the readability. If I was able to rely onStudentAccumulator
auto-casting to its internal types, then I could remove a when statement that just dispatches out to the same overloaded call with the proper type.Ok, I fixed it. It's not too much uglier with proper Option protection...
My try with python
In Scala -
Here is the Python code snippets:
My Python attempt :
Using total_ordering decorator from func_tools, which only requires two implementations of the comparison methods.