5 kyu
Haskell List Dot Notation
473 of 474surtich
Loading description...
Functional Programming
Arrays
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
These edge cases should also be tested since they are present in example tests:
[1..9,12..15] -- invalid since one single range is allowed
[1,2..20,25] -- invalid since a range has to be the final item
[1,2,3..20] -- invalid since at most one inidivual element can be provided before a range
assertSimilar is deprecated, use assertDeepEquals
the empty list test is confusing. coded it if options generator is an empty string to return an empty array and it deemed the empty list undefined in the output? since when is an empty string undefined?
It's very disappointing to see great description, examples, just to find out that you only need to implement a fraction of the given task. And then, no random tests...
Needs random tests
This comment has been hidden.
I agree. And I would add that I see no point in using a map instead of a single string as argument.
The first point in "[1.3..7]" is supposed to be a comma, right?
Yes... I changed it for surtich...
Would be nice to add the generator expression to the error messages.
Is a default negative step used? So that
[5..1]
==[5,4,3,2,1]
, or does this result in[1]
?Looks like it isn't. Might be worth putting that into the description. Really enjoyed this one. Good job.
The logic behind the .. notation is a bit counter-intuitive, you should describe it a bit more. I am used to notation like a:step:b or from a to b by step.
Here you use (correct me if I am wrong): a,c..b to say from a to b by step with step = c - a.
[1..5] // Goesforward step one 1,2,3,4,5 [1.3..7] // Goes forward step two (3 - 1). SO 1,3,5,7 [5..3] // Bad, if the step is not one, Haskell calculates it [index1] - [index 0] [6,5..3] // Goes backward step -1 (5 - 6) 6,5,4,3 [6,4..0] // Goes backward step -2 (4 -6) 6, 4, 2, 0
Is tt best explained like this?
Thanks for your commet.
What about ['1,2..6,8..11,10..0'] ? and ['1,1..10'] ? My current solution infinitely loops on the last one.
I thought I understood it, until I saw this explanation. Would really be useful if you just explained the whole thing. It's pretty hard to grasp, even with your more detailed code comments.
If I've figured it out correctly... @nivolviev's examples should go like this:
1,2..6,8..11,10..0
== [1,2,3,4,5,6,8,10,11,10,9,8,7,6,5,4,3,2,1,0]1,1..10
== (bad form, defaults to 1) [1,1,2,3,4,5,6,7,8,9,10]Okay, now I've really confused myself @.@"
Both are right, the explanation is not very good but my English either. Sorry.
I added more tests and improved the explanation with more examples. I hope now you understand it better.
Sorry, nivoliev, your solution is not valid now.
No problem. I find it weird now that
1,3..4
gives[1,3]
whereas3,4..0
gives[3]
. What is the behaviour of1,2..2
and3,2..2
then ?I believe you should define formal specifications for the descriptor, something like "a valid descriptor is made of a (possibly empty) list of individual numeric elements, plus a single optional final range with format 'start..end', where start and end are numbers. A range is increasing if end ≥ start, decreasing otherwise. The provided individual elements provide the first elements of the output list. The range elements are appended to these, and are defined using a step. The step is defined as start-a, where a is the last individual element provided. In case no individual element is provided, the step is 1. An increasing (resp. decreasing) range with a negative (resp. positive) step is empty. Otherwise the elements of the range are defined as
start + k * step
, with k starting at 0 and increasing one by one as long as the result is in between start and end."These are the answers at your questions tested against my Haskell console:
Test.assertSimilar(ArrayComprehension({generator: '1,3..4'}), [1,3]); Test.assertSimilar(ArrayComprehension({generator: '1,2..2'}), [1,2]); Test.assertSimilar(ArrayComprehension({generator: '3,2..2'}), [3,2]); Test.assertSimilar(ArrayComprehension({generator: '3,4..0'}), []); // I think this is nonsense and I not going to add this test.
Thanks a lot for your help. I would not know how to explain it so well. I copied it verbatim in the exercise.
Since the ranges in both 1,2..2 and 3,2..2 contain 2, the specification I described is wrong about increasing and decreasing sequences. It should say that a sequence is constant if start === end, increasing if start < end and decreasing otherwise. It should also be added somewhere that a constant sequence has one single element, which is its start. In the text, I used the word descriptor, while you use generator in the kata description. Finally, given your example, I also realize from your examples that if a range is provided, only one single individual element is allowed. I would therefore reformulate my previous description as
A valid generator can be either
'start..end'
: ifend >= start
, the list is[start, start+1, start+2, ..., end]
otherwise the result is[]
a
followed by a range : letstep = start - a
start === end
the list is[a,start]
step
is positive andend > start
then the list is[a, a+step, a+2*step, ...]
as long asa+k*step <= end
step
is negative andend < start
then the list is[a, a+step, a+2*step, ...]
as long asa-k*step >= end
[]
I would put the examples after the formal definition, and maybe use
Wow! That's a lot of work! You are really helping me a lot. I think this is much more clean now. Thank you very much.
What is the logic behind 8,4..40 => [8] ? Negative step and increasing range should return [], no ?
Yes, you are right.By the moment I have commented this test because my solution does not pass this test.