7 kyu
Mean Means
745 of 1,252tibor_r
Loading description...
Mathematics
Algorithms
Algebra
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 new test framework is required. updated in this fork
Approved
My code passes all tests except one, but I believe this is true.
"8.015856437398833 should equal 8.015856437398835." This is an error on the second check
What should I do to pass the kata?
JS: function and parameter name should use
camelCase
C#:
Method name should be
PascalCase
(Please refer to implementation of backward compatibility here )Floating point comparison should be used in assertions instead of rounding both actual and expected results
14.3259057835044 should equal 14.325905783504401
7.733044285559698 should equal 7.7330442855597
all other tests passed (python)
There are floats among the given numbers; thus, using
eval
andstr
to multiply the numbers, like you did, loses precision due to rounding.Try to find another way of multiplying the numbers that doesn't involve strings.
Do note that this is only a problem due to floats, and if you were given only integers, there would be no problem with your approach, except for suboptimal performance, of course.
This comment has been hidden.
This comment has been hidden.
i tried just using the statistics module, I don't know why it failed
The statistics module doesn't accept non-positive (zero and negative numbers). It also performs operation on floats and doesn't guarantee exact results. See here.
I was able to provide solution for the basic test and random tests but the extended test was given me this error: Test Failed Expected: 7.7330442855596999d But was: 6.8737933528262785d
What could be the probable C# solutions?, i tried some Math methods like Floor(), Ceiling(), Abs(), Round() already. Thank you
OP solved it, closing
python:
@johan: very good job...
I guess this year's Christmas miracle is this kata finally getting fixed.
Fixed in latest fork
This comment has been hidden.
Read the error message. It tells you exactly what's wrong, and it's in your code, not the tests.
Did you know there's a Troubleshooting Guide in the Docs?
This comment has been hidden.
This comment has been hidden.
The random generator should just not fscking generate negative numbers. I just fixed this for JS. Generating a completely random
mean
is also bullsh!t. Anyone fixing this, please have a look at how JS does it. ( Testing should probably be updated for the new test framework and explicitly importing things as modules as well. )Why do these problems surface after five years of silence ?!?
It seems this kata got a wave of new solves after the recent JS update pushed it to the top of the catalogue - I myself just solved it 1 hour ago for that reason, but didn't notice the weird behavior.
The Python tests are a mess: old framework, testing exact
assert_equals
with custom rounding etc. I don't have time until after the 25th but I can update when I get back if no-one else does.Rounding too? Just k!ll me now.
round
andabs
, this passes random tests:This comment has been hidden.
Don't think. Prove. Closing.
Using prod from the math module solved it for me.
Hey @joshred83 - it seems you had the same thing as @PDrich101 so see my comment to him just a few minutes ago:
https://www.codewars.com/kata/57c6b44f58da9ea6c20003da/discuss#63a60d1d9803f25147a80faf
TL;DR - basically don't use Numpy
This comment has been hidden.
Proof would be a generated testcase and an expected return value that are bullsh!t, separately or in combination. Your, or anyone's, solution code has nothing to do with that. Please stop posting solutions.
@Johan - I've raised the issue above; it seems like there are 4 threads for this same discussion so posting here to close out the sub-thread.
This comment has been hidden.
Please markup your code correctly. Please state your language as Python (?).
If you can positively confirm a case where inputs lead to
product(nums) <= 0
(nums
includingmissing
), please raise anIssue
because the kata has a problem. Be sure to post relevant information with your issue. Please consult the Troubleshooting Guide raising that issue; it tells you what, how, and what not, to do.Wikipedia: The geometric mean applies only to positive numbers. So the adjusted input should at least multiply to a positive number, or the random generation in
$LANGUAGE
is broken.I don't see how to make a post as raising an issue, or how to edit an existing post. Do I need to repost it?
edit: Figured out how to post it correctly. Ignore me.
I tried so hard to tell you how to post a useful
Issue
. And you gave us nothing to work with.If it doesn't say "post an
Issue
", do not post anIssue
. If you do post anIssue
, include relevant information. Input, actual output, expected output, and language, at least. There is no need to post your code if the kata really is wrong.@Johan I raised an issue myself above.
@joshred83 - the idea is to use data from tests to show that the error is with the kata; if you read my post hopefully it's clear what the "reporting" should look like.
passed every test in Python, nonetheless not able to submit due to this error:
Traceback (most recent call last): File "/workspace/default/tests.py", line 43, in test.assert_equals(round(abs(geo_mean(list(arr), mean)), 5), TypeError: bad operand type for abs(): 'NoneType'
That seems a problem with your code returning None for some input.
If testing throws an error, you did not pass every test.
Not a kata issue. Closing.
Hey @PDrich101 - I spent some time trying your solution; there is some weird behaviour and I think it seems to be with your use of NumPy; I noticed for some tests your code was producing complex numbers.
I reset the trainer and tried to repaste your old solution but you have since updated so I can't continue testing.
My advice therefore would be: replace all your calculations with inbuilt Python types rather than Numpy ones. Note by the way that there is a
prod
function inmath
- you canfrom math import prod
you don't need the Numpy one.This comment has been hidden.
Kata very much needs the below fork approved.
done
JS fork with
Node 18.x
approved
one of the best katas I've seen so far!
Description should to be improved " Arithmetic mean = (1 + 3 + 9 + 27 + 81) / 5 = 121 / 5 = 24.2 Geometric mean = (1 * 3 * 9 * 27 * 81) ^ (1/5) = 59049 ^ (1/5) = 9" Why you are dividing and powering the result two times? "/ 5 = 121 / 5 = 24.2; (1/5) = 59049 ^ (1/5) = 9"
"Why you are dividing and powering the result two times?"
He isn't.
(1 + 3 + 9 + 27 + 81) = 121
He simplified part of the equation and kept the rest unchanged.
In order to solve this task we only need to understand how test work. There is some double call the function in the tests.
?
Random tests were a disaster. You had to push a number to the input array or the tests would fail.
This comment has been hidden.
( JS ) Update available
JS random tests is generating lots of noise which results in
undefined
.Can you please validate your random test cases before passing it to user function to eliminate the noise?
Update available
python... There is definitely something wrong with random tests... I tried printing out the result before yielding it and i see something like: (my computed result printed) log: res -> -23.111 (test result) Error 24.653 should equal -23.111 Either you swapped the arguments in the test or i dont get it
Try again, if you'd like. The random test cases were broken, but I updated them and your solution should work now.
Python: I can't figure out why this is True: Test.assert_equals(geo_mean([1, 2], 3), 2.2894284851066637) The missing number is 6, because (1+2 + X) / 3 = 3; X = 6 So, the answer is sqrt(6 * 2) ~ 3.464
Any help?
That's because it's not square root, but cubic root of
1*2*6
(as those are three numbers).In the description you mention "imaginary" numbers multiple times -- however, I believe you mean "irrational" ( or simply "real") numbers. I suggest to remove it altogether, as it may be confusing, and it doesn't affect the kata at all.
I see that the author is inactive since months, so I updated the description myself...
I don't think that your random tests works, although I didn't test it
This comment has been hidden.
Is this for Javascript? You must use the new function that is automatically implemented for Javascript because Math.pow() can't handle negative powers.
Ofcourse in JS, I post this because you resolved my another issue comment, but your random code contains errors ;-)
This is one of your random testcase(I bet that you haven't use console.log to see them):
The problem is that you want to test the two groups above are equals;
So it verifies solutions when they don't actually work or the opposite? What sort of solutions get past the tests/fail the tests when they should be failing/passing?
This comment has been hidden.
Update available
C#-Translation kumited! (With random tests)
https://www.codewars.com/kumite/57cc296d58a06bf62200021d
Please check and approve it! :-)
I'm unable to re-publish - server keeps timing out.
This comment has been hidden.
This comment has been hidden.
Thanks for that - it's what I was doing... think it was the system at work playing up again :D
This comment has been hidden.
In Javscript I've added a funnction to fix that issue. For Python, I don't notice any instances where the result was incalculable.
On one of the random tests I get nan should equal nan. Is this an error in the test case or in my code? Nan is designed to not equal itself in order to prevent errors returning values, so i dont see how this can ever be passed.
Yeah that's should be an issue only with javascript. I'll fix it right now.
my solution is in Python, which has the same method for dealing with NaN as JS. I see other people have passed in python so it may just be me!
Maybe post your answer - be sure to mark it as a spoiler and I'll see if its the test cases or your code.
This comment has been hidden.
Hi, few things to Python version:
I've edited the description e. g.
[1,4,7,4]
is not a set in Python or in maths.Regards,
suic
This comment has been hidden.
Yeah absolutely. Sorry first day on codewars but loving it so far.
I've added random tests
Great! :)
Add some random tests please(JS)
Sorry, it won't allow me to re-publish - server keeps timing out
Random tests added :)