5 kyu
80's Kids #6: Rock 'Em, Sock 'Em Robots
968 of 2,495NateBrady23
Loading description...
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.
Today, I want to share a pivotal moment in my coding journey that has transformed my understanding and appreciation of while loops. Not long ago, I found myself grappling with the complexities of these seemingly simple constructs. My mental models felt muddled, and I often struggled to visualize how they operated within my code. The frustration was real, and I was yearning for clarity.
Then came THIS coding challenge that introduced me to the beauty of nested loops. As I engaged with the problem, something magical happened. One line of code led to another, and with each nested loop, I began to unravel the intricate dance of logic and flow. The once-confusing world of while loops became a vivid tapestry in my mind, each loop representing a vital thread in the action sequence I was constructing.
In the process, I discovered that while loops don’t just execute a block of code—they echo the rhythm of our thoughts and actions. They represent a series of choices, each one leading us closer to a solution. As I worked through the challenge, I saw my ideas take shape and my confidence grow. I no longer viewed loops as mere constructs; instead, they became a harmonious sequence of actions, each contributing to a greater purpose.
Now, I possess a clear, structured mental model of while loops and their nested counterparts. It’s as if a fog has lifted, revealing a pathway of logic that was once obscured. This experience has not only enhanced my coding skills but has also inspired me to embrace challenges with renewed enthusiasm. Each loop now symbolizes a journey, a series of decisions leading to discovery.
Thank you, nested loops, for illuminating my path and reshaping my understanding. You've not only changed my approach to coding but have also fueled a passion that drives me to explore and learn even more. Here’s to the beauty of loops and the clarity they can bring to our minds!
Hi Just a remark : robots are not objects as said in the Details, but dictionnaries : code is less simple and if they were objects , we could have written a method for the fight
I've forked and done a major rework of this kata, waiting for review here:
https://www.codewars.com/kumite/651df75aefd0b637976a502a
Included:
I believe this addresses all open issues for JS on this kata. I also believe it's going to invalidate a few existing solutions, but (hopefully) that's only because neither they nor the old test harness actually fit the requirements.
approved
This comment has been hidden.
The underlying issue is that the "random" tests are only testing random
health
andspeed
values, and there are no fixed tests with unequal-but-not-zero tactics lengths. I'll fork and correct this now.Resolved in latest fork.
python new test framework
Approved
You must add a test that forces you to continue rotating turns even if one of the robots does not attack. In this example must be win Robot2, but if you only change round when it was attacked had been produced, in that case robot 2 only excute one of all attacks, and how his attack is less he lost. My initial code worked whitout change round if attack is not produced and pass the all test when i tryed 2 or 3 times.
Add a test that looks like this example and returns that robot two won. Don't leave it to chance...
Resolved in latest fork.
Spent good hour because of 1 spelling mistakee.. Also, like others mentioned. Felt like something is wrong with random test. But this was a nice kya.
Really enjoyed this Kata - thanks!
Thanks for this kata, it was a nice challenge with a great theme! I used it to practice Python classes but some of the short solutions are very interesting also.
Node 18 (
mocha
+chai
) should be enabled (Refer this and this for more info)Ruby 3.0 should be enabled (Refer this & this for more detail)
JS Node 18/Mocha/Chai added in latest fork. I presume Ruby is still outstanding.
updated Ruby
My solution passes all of the 100+ randomized tests, but fails on fixed test #7, which I suspect is the same one user szpakusik posted 2 years ago (see a few comments below). Don't know how to access the test parameters, but the output was 'Rocky has won the fight.' should equal 'Missile Bob has won the fight.'
not enough info to reproduce (language ? input ? why is the expected answer wrong ?)
This comment has been hidden.
Hi. Thanks for this detailed presentation. I didn't analyze it deeply, but could this be the same problem as the issue reported by B1ts? Several users reported errors in JS tests. My code (and others, I guess), doesn't seem to be impacted, so it is possible tests need to be seriously reviewed.
Yes, I believe it's the same issue B1ts reported. My guess would be that there's a bug in the random test generator, because it appears that some random tests expect the wrong answer. However, if that's the case then I don't really get why some people's code is not impacted. Have you tried submitting your code several times (about 10-15)? It may just be that you haven't run enough random tests to find the faulty ones.
Yes, I ran my code many times again on attempt tests and I could never catch an error.
Resolved in latest fork. Only took 2 years!
JAVASCRIPT
TESTS Fight 3: One or both robots have no tactics Fight 4: Everything is the same.
ERROR Expected: ''The fight was a draw.'', instead got: ''The fight was a draw''
Thanks Chrono79 !
Possible duplicate of issues below, but reference solution might have a hole in it. Also, tests are vulnerable to input modification.
Posting an example of a failing test below:
This comment has been hidden.
Resolved in latest fork.
Some random cases are wrong; I submitted the same code several times and the 3rd time it passed all the tests, and I can't find any obvious mistakes in it.
Please say which language you're using and provide a clear example of a bad test when raising an issue, rather that supposing tests are bad without a proof. It is also reasonnable to suppose your code is bad and passes sometimes tests because some cases your code doesn't handle do not always appear in random tests.
This is a non-issue. Please don't raise an issue, if you can't specify the exact problem.
So I'm not sure why test#4 should result in a draw. If everything is the same for both robots then robot1 should attack first - so this robot should win. Only case of a draw in this situation would be if they both have enough health to be above 0 health after all the attacks are done.
Can someone explain how I am not understanding? Thank you!
Anyone else got stuck on timeout execution error (python) and tried everything to fix it but couldn't?
I think you probably need to account for both robots running out of attacks without a KO :) This could result in an infinte loop.
Add "Space ray" in tactics for 6th Fixed test. else mention in Kata Description to store tactics for future use.
you should not mutate the
tactics
dictionaryPython: I passed all tests except one. It's the the third one from the fixed tests.
Weird cause after each attack i check if somebody has won (and if somebody reached health==0, i print the other guy won). Please check the test case. Thanks :)
Need help. (Python) I pass every Test but 1. The Robots do have the same Health, the same Attack(s), the same speed. My function returns a draw but expected is Robot 1 to win. I dont get it. Is this just a faulty test?
#Roboter 1 ['space ray'] r1tac 100 r1.hp 21 r1.speed #Roboter 2 ['space ray'] r2tac 100 r2.hp 21 r2.speed
like I said the Test Failes with something like "expected 'Bob won the fight.'"
There is no draw in that case:
Robot1 wins over Robot2.
This is the input value of that test:
First robot attacks, second robot's health becomes 0 and the fight is over.
But why does Robot1 attack first if they have exactly the same speed?
Can't pass fixed tests 6 and 7. My code passes the rest of the tests (fixed/random) just fine. Is there any way to see what values are being passed into my functions?
I'm not sure how to troubleshoot, as I'm only getting "'The fight was a draw.' should have been 'Rocky/Missile Bob has won the fight'" as a test result. Any ideas?
Out of curiosity, does this kata encourage the use of OOP?
Good kata!
Great kata with a random error in the tests as flagged by many.
It's absolutely brilliant! Brings you right back to 80s. Great you have many more of them.
In the JavaScript version, some of the random test case may produce a wrong "Expected"?
May the author fixing that?
In the Ruby version, I found a typo in test #6: Tactic order matters, where missile is spelled like missle. The task is not specific what happens when a tactic is not in tactic hash. I did not use that tactic if not found, however, then the wrong robot wins according to the test.
Anyone else had the same issue?
fixed
There might be a problem with the kata. I failed some of the random tests. So I attempted my code multiple times without changing anything. The fifth attempt was a success...
I can't find any obvious mistake in my solution, so I'm not sure if there is a bug in the random testing.
Intermittent test failures resolved in latest fork.
{ name: 'Rocky', health: 10, speed: 59, tactics: [ 'punch',
{ name: 'Missile Bob', health: 25, speed: 18, tactics: [ 'punch',
{ punch: 20, laser: 30, missile: 35 }
Expected: ''Rocky has won the fight.'', instead got: ''Missile Bob has won the fight.''
Seems wrong as few per 100 all the times
Good Kata. I enjoyed it :)
You should give the Robot the mehtod or told the Users that there is none: (Java)
public void setHealth(int health){ this.health = helath; }
Hi there,
I believe there might be a mistake with Fight 5: Speed is Everything - Case 1 should be a 'draw', but for some reason it is not. Both robots have same health and same speed, and same tactics.
Why should Rocky win at this case and not be a draw?
There can't be a draw with no health.
But why is Rocky a winner if they both have 0 health ?
This comment has been hidden.
Should the Robot class have setters for this Kata?
Nope
This comment has been hidden.
Can you not just print out the inputs in the function to see them?
?
kata was fun. thanks;)
Hi! Why in the description does not say anything about using the tactics of the "space ray"?
This comment has been hidden.
OP solved it, closing
my code fails on test "SpeedIsEverything" can anyone give me a hint, please?:c upd : I solved it, kata is so smart :) respect to the author:)
@NateBrady23, C++ translation ready for review.
Is it possible that the first test in the third category ("One or both robots have no tactics") is faulty? I'm passing 106 of 107 tests, with my code accounting for all three possible outcomes: robot1's health > robot2's health, robot2's health > robot1's health, robot1's health = robot2's health. The test I mentioned expected "Missile Bob has won the fight" but instead returned "undefined."
One of the random tests internally returned the wrong result. Attempting a few times got them all to pass. Missible bob had -1 health and The other guy was still alive, yet the random test said (falsely) missile bob was the winner. Also small hint: if you are getting timeouts, you have not provided an exit condition where there is a draw or both of them are out of tactics but still alive.
JS (-> don't forget to tell about the language when you raise an issue are ask for a question, or...)
Crap ! Sorry about that, I'm still kinda new to all this.
Kata description typo cut/paste wrong for all languages
that should be
missile
Fixed.
Python translation is ready. Please, review and approve.
Note: includes the "missle" typo fix.
.
error: health has private access in Robot error: speed has private access in Robot error: tactics has private access in Robot ...
Why would they be private? How are we supposed to even check the value if it's private?
maybe because, as usual in java, there are getters for that? :p
ok tnx
@NateBrady23, C++ translation ready for review.
Missile is spelt wrong in the kata description:
"tactics": ["missle", "missle", "missle", "missle"]
C# translation added!
Kata could be updated to use Ruby objects :)
This comment has been hidden.
Robot objects are now copied for each test; I believe this one was already resolved sometime in the last 7 years but it's definitely resolved now.
Fight 5: Speed is everything. ✘ Expected: ''Rocky has won the fight.'', instead got: ''The fight was a draw.'' ✘ Expected: ''Missile Bob has won the fight.'', instead got: ''The fight was a draw.''
I passed all except this...,and why?
I found it's my fault.Sorry!
Fun kata, be sure and fix spelling errors (missle is spelled missle in the examples) which can throw off answers.
Nice kata; it could be forked into one where the attack sequence is based on the
speed
for each respective robot instead of alternating order.For example, if we have robot #1 with
speed: 10, health: 100
and robot #2 withspeed: 15, health: 125
(let's assume the attack damage is10
and thetactics
array is the same for both robots with a length of 15), then robot #1 would emerge as winner.I'm getting occasional (appx 1 in 100) errors where my answer seems right. One example (using console.log on the arguments):
Another example:
I got one or two errors out of 107, then passed the kata on my third try. Then refreshed and tried again, got one error out of 107.
Enjoyable kata, thanks.
I am also stuck at same problem.please tell what to do
The numbers do appear to side with the winners you calculated.
You should ask @NateBrady23 to look at his tests, there are seemingly a couple of bad apples in a large barrel.
It seems wrong that the
Java
robot object health member cannot be set. So these are only "what if" battles - since in a real battle the robot instance health remains unchanged no matter how many hits it takes."Missile" is spelt wrong. The penultimate point should be 'If both robots run out of tactics, whoever has the most health wins. Return the message " has won the fight."'
I assume you were putting some tag in " has won the fight."
Spelling fixed, thanks.
yes I was: I put "winner name". Silly Codewars!
Really good kata! Just a little typo on the second paragraph (java): "Each robot will have a name, hit points, speed, and then a list of battle tacitcs they are to perform in order." tacitcs -> tactics, no big deal.
fixed
This (or equivalent) should be a test case for ruby:
I was passing the random tests half the times I hit submit, and it took me a while to realize that it was because I was using
pop
instead ofshift
. Hence, this test checks that tactics are executed in the right order.I believe the description is clear ("Tactics are used in order") and you using
pop
instead ofshift
isn't indicitive of a problem with the kata. I think this should be a suggestion not an issue, as it doesn't result in a correct algorithm failing. Though I couldn't reproduce passing the kata by doing tactics in reverse, since you've done the work I've added the test to Ruby. Thanks for the feedback.The description is clear, but I was passing on a whim despite them. Felt like an issue to me...
"If both robots run out of tactics and are tied for health, the fight is a draw. Return "The fight is a draw."" or "The fight was a draw."
Thank U for your tasks!! I's rather fun solving and useful!
"robot2 uses the first tactic, "missle" because he has the most speed. This reduces robot1's health by 20" but " "missile" => 35" 20 or 35 ?
Fixin' that. Typo in the details. Shouldn't matter in your solution, as number will vary.
Java translation available!
Robot
is an immutable class that represents a robot.Rock on! Thank you!
Some of the test cases look wrong
They look wrong? Just a little elaboration required here. Language, test that fails?
@NateBrady Its on coffeescript. My solution sometimes works, and sometimes doesnt.
I think my solution is wrong, since i used Array::sort to get the order of the bots, instead of "A robot with the most speed attacks first. If they are tied, the first robot passed in attacks first."
Yes, it's that. Redid the sorting part so it does not rely on Array::sort being stable https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
From description:
I think you meant to say 35, not 20.
fixed!
Nice kata! Upvoted and translated into ruby for your approval. Happy to make any revisions you might want.
Looks great! Thanks for the translation! :)
coffeescript too :)
Wow, you're on fire!
enjoying a few days off :)
I had to fork and submit because of description merge conflicts. Do you still get the honor for that?
looks like it :)
oh, that's interesting... I should have fixed the description problem reported in the translation... duh...