2 kyu
Fastest Code : Equal to 24
531 of 962myjinxin2015
Loading description...
Puzzles
Games
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.
Very nice kata! The task seemed innocuous at first, but it turned out to be quite a tough nut to crack. However, with persistence and the right idea, I managed to solve it after all. Thanks for this experience. I learned a lot!
больше чем 220 я не успеваю за 12 секунд либо я реально не могу быстрее сделать либо сервакам урезали мощность
I pressed "attempt" 30 times :)
This comment has been hidden.
Traceback (most recent call last): File "/workspace/default/tests.py", line 83, in A,B,C,D = filter(lambda x: len(x), re.split("[^\d.]+", useran)) ^^^^^^^ ValueError: not enough values to unpack (expected 4, got 1)
Is your function returning 4 numbers plus some operations? Read this: https://www.codewars.com/kata/574e890e296e412a0400149c/discuss#5ef2d3d88ea1ac00338142b7
Execution Timed Out (12000 ms) :/
fair warning, I am brute forcing with 10000+ lines... receiving the following after passing basic tests:
TypeError: s.match is not a function at isValidR at /home/codewarrior/index.js:10920:16 at /home/codewarrior/index.js:10942:5 at Object.handleError
/runner/frameworks/javascript/cw-2.js:237 throw ex; ^
TypeError: s.match is not a function at isValidR (/home/codewarrior/index.js:10935:8) at /home/codewarrior/index.js:10920:16 at /home/codewarrior/index.js:10942:5 at Object.handleError (/runner/frameworks/javascript/cw-2.js:233:11) at Object. (/home/codewarrior/index.js:3:6) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at [eval]:1:1 at ContextifyScript.Script.runInThisContext (vm.js:50:33) at Object.runInThisContext (vm.js:139:38) at Object. ([eval]-wrapper:6:22)
This comment has been hidden.
I have the same error when running my test. My logs show that I found the right answer though.
This can be marked as resolved.
It is now clear that I need to return a mathematical equation as as string and not just the number 24 🤦♂️. Now I just need to have this not time out at the 200th test.
This kata was great ! Would you happen to know some other cool katas with performance constraints ?
this is really intresting. if you have complete the primer question. you can try to decrease your complexity, it will get pass the test.
the primer question code may only pass half of test cases.
Test case is incorrect.
This is a mistake since
9*(4-(40/30))
is indeed 24.Do not use intermediate floats. Closing.
Is there a chance that the author can remove the print statements?
Author hasn't been seen for as long as you haven't. Closing.
I see some posted (python) solutions that only passed because they hit attempt a few times in a row until they got a set that didn't have the test case they missed.
Suggest you add these test cases:
Log prints "--------20 Random Test--------" when it's actually performing 500. Correcting this would help users know how far off the mark they are.
If you meet the time limit but fail a test you get the "Congratulations, You pass the test!" message
I got the error message "'80/((76/3)-22)' should equal "It's not possible!"" but 80/((76/3)-22) is in fact equal to 24.
Depends on how the test are made:
Yeah, I checked that. It's still a problem with the test though, even if it's down to the imprecision of python's floating point arithmetic, right?
Calculations are not supposed to use intermediate floats. Closing.
This comment has been hidden.
The description is badly formatted.
Can you fix this issue. All cases are passed but i see this STDERR while running random test cases.
Traceback (most recent call last): File "main.py", line 83, in A,B,C,D = filter(lambda x: len(x), re.split("[^\d.]+", useran)) ValueError: not enough values to unpack (expected 4, got 1)
to the author. can i get some help. i am getting issue with running random test case. all cases are fine but i see this error.
Traceback (most recent call last): File "main.py", line 83, in A,B,C,D = filter(lambda x: len(x), re.split("[^\d.]+", useran)) ValueError: not enough values to unpack (expected 4, got 1)
i am getting issue with running random test case. all cases are fine but i see this error
Traceback (most recent call last): File "main.py", line 83, in A,B,C,D = filter(lambda x: len(x), re.split("[^\d.]+", useran)) ValueError: not enough values to unpack (expected 4, got 1)
Print the input so we can check if it is a kata problem, and please, don't open multiple post about the same. Read this too.
Hi Chrono - thanks for the reply. sorry for posting multiple - it was not showing me when i first posted and i re-posted. i will make sure i dont do that. Below is my calling code to solve the problem: def equal_to_24(a,b,c,d): print(f"input is {a},{b},{c},{d}") if call24(a,b,c,d) is True: return '24' else: return "It's not possible!"
i am printing the input and i see input displayed for all cases until the last step where it throws STDERR. I am showing last 2 cases output along with error at the end. hope this helps.
Test Passed Log input is 4,3,1,6 this one can return 6/(1-(3/4)), your answer is: 24 input is 4,3,1,6 Test Passed Log input is 1,1,1,1 Test Passed Log input is 13,13,13,13 Test Passed Log
--------20 Random Test--------
input is 21,28,21,53
Test for: a=21,b=28,c=21,d=53 Example solution: "53-(28+(21/21))" STDERR Traceback (most recent call last): File "main.py", line 83, in A,B,C,D = filter(lambda x: len(x), re.split("[^\d.]+", useran)) ValueError: not enough values to unpack (expected 4, got 1)
I tested just for the last random case alone and i see its working Log input is 21,28,21,53 this one can return 53 - (28 + (21 / 21)), your answer is: 24 input is 21,28,21,53 Test Passed
Well, the error you see is because you're returning a single number instead of 4 when there is a possible solution.
You should return
'53 - (28 + (21 / 21))'
, not'24'
Hi Chrono - thank you very much. I understood the problem and you are right. appreciate your help.
Hi,
What I got: Test for: a=28,b=32,c=3,d=88 Example solution: "It's not possible!" '32/((88/3)-28)' should equal "It's not possible!"
However 32/((88/3)-28) == 32/((88/3)-(84/3)) == 32/(4/3) == 8 * 3 == 24 ... well, according to math
.
Do not use intermediate floats.
Hi, Python test cases doesn't work without "import math". It should be fixed.
It's fixed, now.
Python:
eval
is not protected (can be redefined);Thx for bringing that up.
I fixed points 1, 4, and (I think) 5. Do you have any tips to improve the reference solution?
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
just build a simple math parser and evaluate the string yourself. If you wanna ban eval for the user, you cannot use it yourself (or you'll see some users raising issues because
eval is still accessible through...
)This comment has been hidden.
Added. Is it ok to close the issue, yet?
.
I get always SyntraxError EOL and it points to end of return "It's not possible!". But I couldn't get what is wrong. I checked everything. It seems to be an error of your test script. It seems because "It's not possible!" is not evaluable ...
Tests are correct and problem is your code returns
It's not possible!
(which doesn't work witheval
) when there is an answer (which works witheval
).Thank you! I am so stoopit
Ruby translation
Please check and approve if possible :)
And I raised random testcases amount up to 3000 cause of Ruby performance :)
Any feedback will be greatly appreciated.
Problem is both JS and python have 1000 tests. This is not enough for python because you can pass relying purely on built-ins (in JS you have to implement them yourself which is definitely more demanding). So either your version can be approved and other upped accordingly (invalidating many solutions) or you can lower yours to fit what there is right now (which IMO makes this kata overranked). Any input from someone else?
can you update it to
3.0
?SWIFT translation finished. Thanks a lot! https://www.codewars.com/kumite/5dcc91737baeb6002831c2ef?sel=5dcc91737baeb6002831c2ef
This is hard and fun! The test is also very useful. Thanks a lot!
This comment has been hidden.
You might have missed the fact that operator precedence and addition/multiplication being commutative make some parentheses redundant, limiting number of fixed cases. Also random tests are, well, random, so some things may appear multiple times while other not at all.
python translation: https://www.codewars.com/kumite/5c8cd0e4f711be2ca76c528c?sel=5c8cd0e4f711be2ca76c528c
approved
Thanks!! Would you also care to review my translation for the 4kyu one? :D
https://codewars.com/kumite/5c8cca00f191ba73e37ecbd2?sel=5c8cca00f191ba73e3
See issues above
C++ translation published :)
(It provides a Math::eval function (since C++ doesn't have eval()), which only works with mathematical expressions, and should be fast enough. I hope you don't mind ?)
random test give 8/(1-(48/72)), so try
Test.assertSimilar(eval("8/(1-(48/72))") , 24);
but i got
Expected: '24', instead got: '23.999999999999996'
But that's Not my test method, I'm using the following code to check it:
This comment has been hidden.
Check out this solution
Hahahah, 8000??? 🤯
This comment has been hidden.
Coffeescript translation
Approved. Thanks ;-)
This comment has been hidden.
Hah, my check code is too weak :-0 Thanks for the feedback ;-) Now it's fixed, I think. Please confirm ;-)
Sure, all good now. :smile:
Thanks ;-)
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
对于大多数人来说,这个时间限制是足够的。请不要炫耀,亲。;-)
Please forgive me. It's not a kind of parade. Just want to know what the best solution is.
I only use a very straightforward one without any optimizations, and you know, reading other person's solutions is hard to understand.
On a first attempt your solution failed three tests:
1,72,4,60
,3,8,3,8
,4,85,56,51
.Right! I have to admit that is not exactly COPY.
But it is due to floating point precision as you mentioned, instead of algorithm thinking.
this one can return 2*(13-(7/7)), your answer is: 2*(13-7/7) what's difference between 2*(13-(7/7)) and 2*(13-7/7)?
It seems that they are both right. this result doesn't pass the testcase?
It's my fault. The result can pass the testcase now. but there is new problem with node v8.1.3
The output
and
that's a good wish. Please ignore the congrats information ;-)
I use node v6 can pass all testcase with the same code which failed with node v8.1.3
this kata written at 2016 with node v6.
Because of the way the test suite is setup, if your function returns
"It's not possible!"
but the test is expecting an expression that it caneval
, then CW throws an syntax error. At first glance, it doesn't really make any sense, and I wonder if it could confuse other people as well. I would suggest adjusting the tests to catch this error and return a more approriate error to the user.OK. I've made some changes. Hope it's fixed. Thanks for your feedback ;-)
Sorry, it still occurs. @myjinxin2015
Approved
Thanks, the kata sleeping in the dust for one year ;-)
I think you should mention that result could be not strictly equal to 24, e.g. here 36*(1-8/24)
Thank you for the nice kata. It would be nice to have version of this kata not limited by 4 arguments and 24.
Thanks. Happy coding
^_^
Is there a Python solution? How can I code it in Python?
Sorry, no Python reanslation exist ;-)
Holy crap this was hard. I'll share my solution tomorrow :) thank you so much! No idea how you come up with all those clever solutions... took me a while to get it working ;) Rated as 2kyu, will think of rank a bit more after there'll be some more solutions.
Yes, This is indeed a bit difficult ;-) When @smile67 knew that this version had 1000 random tests, he thought I was crazy :]
hey gabbek i know this isnt code related but i love your profile pic! one of my fave anime!!! just saying...
No, not thought... still think it:-)... Next time i will try all these missing harder katas again, so less ouput, but more quality?:-) I put it on my list...
This comment has been hidden.