6 kyu
Midpoint Sum
771 of 1,307AlejandorLazaro
Loading description...
Lists
Fundamentals
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.
python fork:
https://www.codewars.com/kumite/62c8246876acd6005611fb8a?sel=62c8246876acd6005611fb8a
Test.assert_equals
should be used instead.No random tests.
You need to clarify the answer in cases like [1, 0, 0, 0, 1]. Should it be first index or any or what? What is the right answer for this test? Also can't see any random tests for Python (I used version 3.4.3).
Now a duplicate of http://www.codewars.com/kata/equal-sides-of-an-array.
I wonder if there is a better solution for this than a greedy one. Mapping and summing
n
seems like big time over"clever"ing (and iterating).You might be right... in fact, I could see very large examples timing out on many of the solutions that tend to sum n for each element traversed (giving around a n^2/2 expected complexity).
I would think that maybe solving the problem should only have to be done in O(n) time... even say, with a maximum bound of 2 complete traversals of the list.
However, I already made a more "difficult" problem that uses the idea I'm hinting at in the Sum of Pairs kata.
If you want to experiment yourself, I'd say make a playground for your solution at ideone and test it on a list with around 6 to 10 million elements. (Here's a handy comment that explains why)
I wonder how people consider as "best practice" and "clever", for this exercise, a bunch of solutions that sum the list of numbers again and again...
This comment has been hidden.
In the final test I am getting 1 as the midpoint sum and I dont really understand how that isn't the correct answer. [1, (1), 1, ...] 1 == 1 when comparing from the index (1) within the parenthesis.
It seems your program is returning a pivot value when there is none. You may want to check and see if your program works for large lists with similar numbers that contains an even number of elements.
def midpoint_sum(ints): if len(ints) <= 2:
return None for i in range(len(ints)): left = sum(range(ints[i-1])) right = sum(range(ints[i+1,len(ints)])) if sum(left)==sum(right): return i return None
Hey peeps. I am getting an error because it is encountering tuples, any clue why?
You have a small mistake in the test cases: Test.expect(midpointSum([-10,3,7,8,-6,-13,21]) == 4, "[8,-6,-13,21] should equal 4")
should be Test.expect(midpointSum([-10,3,7,8,-6,-13,21]) == 4, "[-10,3,7,8,-6,-13,21] should equal 4")
Fixed the test message to match the input given. Thanks for catching that error!
I think you should specify (and perhaps provide an example) that the integers may be negative, as this will drastically change what kind of implementation is possible. Also, the zeros are a bit confusing as it is. Instead of
Don't treat the list as if it had floating zeros before/after the first/last elements
, you could simply saythe first or last index cannot be considered the "midpoint"
. This would cover what you explained, but much clearer, and easier to slot into the current understanding of the problem. Along the same lines, the "Ending in non-zero" is just super-confusing, so should be changed.Still, I had a lot of fun figuring this out, so good job on coming up with it in the first place!
Added in a more descriptive rule set to guide solutions. Hopefully that will help people out in the future!
Also changed "Ending in non-zero" to "Trailing zeros", as cases with sequences of preceding and trailing zeros are an important edge case people may often miss.
Hope that clears things up! :)
Yeah, that's a bit better. Thanks.
I am not 100% ok with edge cases: you either state in the description that the first and last item can't be the solution or you let them pass, as they could properly be the required midpoints.
Other than than, simple and interesting kata :)
In regards to the possible solutions: would you suggest I clarify that any list with 2 or less elements cannot have a midpoint that is acceptable, since even if [0, 0] were sent in, we don't want either zero to match with some 'floating zero' sum outside of the items actually in the list? (It's the same reason [0] shouldn't have a midpoint of 0 either, even though someone may set the left/right sums to 0 in their code, rather it is invalidated.)
Edit: I went ahead and added some extra information to the description. Hopefully that helps!
To me just telling that the midpoint can't be the first or the last element is ok (though I thought few days ago I saw an old test that included negative numbers and the midpoint being the last) is more than fine.
That said, if that's your final saying on the matter, I presume I can take some time and do a Ruby and Python translation of it.
I also flagged the kata as "ready", as I see no other point which needs fixing :)
The kata is now kumited in both js and Ruby; as I am not proficient with them as I am with Python and given that the editor was a bit bitchy today, getting stuck a lot, feel free to tell me if there is any trouble with it.
And, of course, any other improvement/correction you may want to suggest is gladly welcome :)