4 kyu
Alphabet wars - trench assault
41 of 309Expurple
Loading description...
Fundamentals
Strings
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.
I have a question What happens when attackers enter the trench and get defeated?
suppose the attack is:
"ss "
"--|dzp|"
as d is 2 but also the first one in the trench it's defending power becomes 4 successfully defending. but now what? what will be it's attacking power? will it be (4 - 2) or (2 - 2), as you've said "it's not allowed to use more points than usual."?
Look, we can be sure that the "remaining defending power" of
d
is 4-2=2. The rule about "more points than usual" means that if it was greater thand
's usual power (2), we would need to bring it down to 2 before attacking. In this case, it's not greater, so the remaining power stays unchanged and the rule doesn't really change the outcome. Similarly, ifsss
were attacking, the remaining power ofd
would stay at 1. That rule makes a difference in cases like attack from a singles
. The "remaining defending power" ofd
would be 4-1=3, but the attacking potential ofd
is limited at 2.Any ideas about how I can shortly express this in the description?
You can write this: With double defensive power, if the fist defender in the trench wins, then it's attacking power is equal to the power remaining after the double defense or it's original power, whichever one's lower.
Here is my JavaScript translation: https://www.codewars.com/kumite/6545ce33b3e7770024f678e2/edit
Hi, thanks. Looks good to me. Submit it here (so that you get the credit) and I'll accept it
No problem.
in JS in random tests first line sometimes lacks one last space so it is wrongly padded. Is it intended ? Looks like a bug for me
I do think it's a bug
I suggest to add 100 random tests instead of 30. My solution can't handle every case, yet it passes from time to time with so few random tests.
Done. I also suggest adding one of the failing cases as a fixed test. Cound you print it (then maybe reduce to a minimal reproducible example) and post it?
Added this to fixed tests
You should add more examples with complete walkthrough in the description. The fixed tests are black boxes and have zero added value in debugging.
Which mechanics would you like to be covered in a walkthrough?
Other cases seem to be already covered.
Yes those are good examples, keep them very small, so users could see the effect of these cases. After adding these cases, this kata can be approved.
As I think more about it, I think I'd prefer to edit the sample tests instead of bloating the description. Minimal examples don't really need a step-by-step walkthrough, as they only consist of 1-2 steps. What if I just minify the sample tests and give them descriptive names that directly reference the tested mechanic? For example, I'd turn
into
Would that be enough?
I believe so yes
example cases were added
You mean
"If it survives, then that's enough of advantage"
? Added that to the C version. Later I'll do renaming/minimization as discussedThere were enough cases to get approved. You may always add more cases if you wish to.
Kata's like this should have an impeccable specification.
I've edited the description. Let me know if the issue is resolved or suggest how I should improve it further.
No. Old wording: "attackers [...] continue to attack or join forces with allies. This includes going from the ground deep into the trench". This was hidden under "If defending letter has less power than the attacking letters", so I've extracted the last sentence and merged it into the general rule about never leaving trenches: "While joining forces or attacking, letters can stay on the same level or go down into a trench, but they never leave trenches. They stop at the right end of the trench, and the next letter on the ground starts a new chain of attacks".
All letters to the left will attack. In my mental model, this is a single attack by a group that has joined their forces beforehand. Added rule that enforces the "beforehand" part: "Letters act in order, from left to right".
Yes. See my first answer for references.
No. Letters to the left of the trench should act first, i.e. enter the trench. Added rule that enforces this: "Letters act in order, from left to right".
this looks ok to me
C :
the assertion messages are not very helpful, because we dont see the input passed to our function. I suggest something like this :
Fixed. Thanks for coming with a concrete example
that's good, but since there is whitespace in your input strings it makes it hard to spot in the messages. you could enclose the string within quotes and insert a newline before and after it , i.e. :
C :
The sample tests throw warnings because you forgot to declare the user's function; add this line :
True, forgot to copy that from full tests, thanks
A question though (I have not solved the kata and I cannot see C translation, so I cannot check by myself): the returned value is going to be a pointer to a what, exactly? Are you sure it should be
const char*
, and notchar*
?Note that a dynamically allocated, freeable buffer probably should not be returned as
const
, because it won't be possible to pass it tofree
without casting the constness away.It's supposed to be freeable. I'll remove the
const
Generally, the setup of C version is... far from ideal.
If I get the rules correctly, the length of the output is perfectly known upfront. If so, why dynamic allocation in the solution would be even needed? Passing in a preallocated output buffer would be much better, and would make tests much simpler.
If, for some reason, the solution would still be expected to return a dynamically allocated buffer, it should be somehow explained (for example in a comment in the solution setup). The statically allocated example answer is additionally misleading. On top of that, tests leak and pretend it's fine only because they created the problem in the first place :)
My suggestion would be to provide an output buffer preallocated by tests. If author does not like this suggestion and dynamic allocation is going to stay, this requirement should be documented, misleading example should be removed, and leaking tests should be fixed.
I prefer to keep the function interface without an output buffer parameter. Dynamic allocation could be indeed avoided, but I don't think that it's an issue.
I agree with you about misleading static example, though. It will be fixed soon, as well as leaking tests. I'll also add a note about dynamic allocation to the description.
It always makes me wonder why C authors prefer such approach. In cases like this kata (i.e. when size of a result is known upfront) I cannot see a single advantage of it, and I can see many disadvantages. I cannot help but wonder what makes dynamic allocation in a solution so attractive to authors, as all it does is creating obstacles.
I am not going to override your decision in any way, but I would like to honestly ask you to honestly reconsider and see if you can come with any honest argument supporting currently preferred approach.
what are the disavantages ? the user can still write out-of-bounds,forget to nul-terminate, allocate arbitrary memory in his solution and not free it, and so on
I know that not all of below apply to Codewars kata, but it's good to keep general picture also in mind, is it? Requirement to allocate in a solution sucks, because:
free
after every test case, instead just allocate on stack (or heap for really large cases) once at the beginning and forget. At least try it, and you will hopefully see how much simpler it is to write tests.malloc
from a callee cannot be matched withfree
by a caller.malloc
andfree
are not always symmetrical.malloc
in a solution is a Java or Python cliche.I know that dynamic allocation in a callee has its place, but I just think not in this case. I am aware that some of above points apply to Codewars kata, and some not, but why should we lose a view of a general picture? And still, I cannot think of even a single one advantage.
what happens in case of strict equality ?
Attackers get the land, but attack is exhaused. Added this to the description
What does 'more points than usual' mean here ? Say a letter is the first letter in a trench. It has power
4
and gets attacked by power6
. Thanks to the defensive bonus it fights with power2 * 4 = 8
and wins. What is its new attacking power ?4 - 6 = - 2
?0
?8 - 6 = 2
?4
?It this case, it's as simple as
8 - 6 = 2
.This rule about
"more points than usual"
is useful when attackers are weak, e.g.3
. Defender then has8 - 3 = 5
points left, but can only attack with4
, becase5
would be greater than the "base level" of4
.I'll think how to reword this rule better in the description
it's not clear to me what happens if there are several sides in a ground group. which group attacks the trench ? the last ? the biggest ? none ? same question for the trench, can they join the next attack ? can they help to attack their own trench ?
We have an "attack state", which is calculated letter by letter. It just snowballs from left to right, gaining points on allies and losing them in fighting ememies. When attack is exhaused, the defender who won starts his own attack. Trenches don't matter that much, they only:
So, back to your question, who enters the trench? The attacking force at the moment, not any particular letter or group of letters. In this example, we have a right side letter before the trench, but actually the left enters it:
i dont understand if's it's guaranted that trenches and groups are always composed of same side letters i.e. is this invalid ?
-
below the opening"
, but I'm sure that's not intentional)I'll add point 1 to the description. The "whole battlefield" case from point 2 is covered in the example tests, and I should probably add a "no groups" case too. Thanks for your feedback!
Hi,
The specs are lacking some important information:
The definition of a "group" isn't given precisely enough. For instance:
=> could that happen? is that 1 or 2 groups?
=> 1 or 3 groups?
could that happen? How many groups?
Only one trench? Possible to have more than one in the same input? Are they always "well formed"? => All of this should be in the description
example:
Thank you for the attention. I've updated the description of input terrain, groups and attacking. About your examples in short:
'|'
srr|rr|r
to scatteredlr|rl|r
. The last letter won't be able to get any power boost from allies in the trench, though.I'm marking this as resolved, you can reopen it with remaining suggestions, if you have any
why this difference with the original series?
I had no simple idea for deciding the attack direction and the kata seemed complicated enough as it is. If you have a good idea, we can make a part 2!
that actually doesn't change much: compute everything in every direction, resolve everything at the same time, look at what's left. Curently, you're computing step by step, the result of one step affecting what might happen in later steps. No?
Yes, currently it's step by step. But I don't understand what do you mean by
in relation to this kata. Sounds like a very dirrerent approach that wouldn't support chained attacks, etc.
This comment has been hidden.
This comment has been hidden.
Yeah, it's probably me but having examples of a first line defender winning (with and without bonus) would have helped. ^^