Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Racket, actually, but both are Scheme/Lisp dialects. And certainly that corner case should be tested for any language...
Just for completeness, the language is Clojure?
The tests are missing a corner case -- I had an accepted solution that actually has a bug.
Namely: if the lists are of unequal length, and all the "extra" elements are larger than all the other elements, and the other elements match up, my solution says they are the same when they are not, because
for/fold
terminates on the shorter input.Here's a test case:
(comp '(1 2 5) '(1 25 4 26))
. The extra 26 in the second list sorts after 1, 4, and 25, and my fold ignores and returns true. Of course, the same thing happens if the first list has extra elements that are all bigger than the "other" elements:(comp '(1 2 5 6) '(1 25 4))
.There should be test cases that verify this corner case so that buggy solutions (like mine!) aren't accepted.
Uh oh, I discovered a bug in this solution that the tests did not flag: if the lists are of unequal length, and all the "extra" elements are larger than all the other elements, and the other elements match up, my solution says they are the same when they are not, because
for/fold
terminates on the shorter input.Here's a test case:
(comp '(1 2 5) '(1 25 4 26))
. The extra 26 in the second list sorts after 1, 4, and 25, and my fold ignores and returns true.This comment is hidden because it contains spoiler information about the solution
You have quite the "pyramid of doom" here. :)
https://en.wikipedia.org/wiki/Pyramid_of_doom_(programming)
Just as a style / formatting issue, have you considered using the "threading" module? That gives you a macro ~> that lets you express this data flow pipeline in a more natural style: rather than (fn3 (fn2 (fn1 input))) where things accumulate on the left, or you get further and further indented, you can write:
(~> input
fn1
fn2
fn3)
And it's very readable: take input, feed it into fn1, feed the output from that into fn2, and so on.
Just a drive-by comment. :)
This solution is O(n^2), correct? When building x-filtered, the filter is mapping a function over x-lst, and it does so once for each unique character, which in general we can assume has length O(n). (In particular, if there are no duplicates, it's the same length.)
So you're doing an O(n) operation once for every element of an O(n)-sized list.
I don't know the exact intent here; for a 6 kyu problem, perhaps this very simple solution, which uses standard library functions like remove-duplicates and filter (with perhaps a somehwat-obscure use of curry), could be considered a good one. But if we're aiming for the best algorithm, I believe you can do this in O(n) time.