3 kyu
The boolean order
361 of 1,388KenKamau
Loading description...
Strings
Algorithms
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.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Ken, I see your below comment is actually the part of task specification. Otherwize it has another understandings & solutions. -Add to spec please something like your answer below-. Nice Kata, but has not clear spec))
UPDATED!!! The following author comment (that you may find in the answers several year ago) actually is wrong OR confusing! My final solution counts both
((1&0)&0)
&(1&(0&0))
variants..... For instance, there is no real difference between the first and the second line given above. Adding extra parentheses does not change this fact. Look at them again:
The main differences are at the ends of the two equations.
Extra parentheses would have made sense if the last two operators were different. If they are the same, as in this case, extra parentheses result in duplicates.
I hope this helps.
I also found this comment by the author below. However, I don't understand how these 2 examples are considered the same. It surely can't be because the result is the same ...
can somebody give me a little hint? am I supposed to find the most effecient way to list all the parenthesis combination and then eval() it one by one OR is there a clever way to just find a True-result parenthesis combination? (my answer works but time out )
You can find the answer by going through every combination of parens and summing up how many are true. But to avoid time outs you need to not repeat yourself too much. The hint is that each set of parentheses you add splits the problem into parts that you probably already solved before.
Damnit, I'm almost there. (works but times out)
I just need a formula to calculate how many combinations (wether true or false doesn't matter)
I can make of a (remaining) part of the equation, to speed up things...
I get weird numbers when I do it with the solver: for x times True and (x-1) times & : on the 4, 8 and 16 times True I get odd results. The rest are even.
(I only went to 16, next would have taken 10 minutes or so)
What college degree math theory am I missing here? (math noob here)
This comment has been hidden.
Can someone explain to me how there can be more than 300 possible parenthesis combinations for an "s" of length 8?
There should be at most 64 combinations for "s" of length 8, and some of them will be false.
If you go by the examples provided in the description everything must be enclosed in parenthesis. And the number of combinations of parenthesis go 1,2,4,8,16, etc for length 2,3,4,5,6, etc. It doesnt seem to make sense.
What am I missing? Thanks.
how do you get to the amount of the combinations?
for l=4 there are more than 4 combinations:
(((xx)x)x)
((xx)(xx))
((x(xx))x)
(x((xx)x))
(x(x(xx)))
How many braces am I supposed to put into the equation? The test "t", "" (true with no operator at all) has unlimited results, depending on the number of braces to be used: (t), ((t)), (((t))), ((((t)))), etc.
Reading the other comments, I also wonder what the operator precedence shall be in order to sort out the duplicates.
very nice kata, I learnt something from this
I wrote some good amount of mess to solve it... As far as I understand my own code (which I forgot right after writing) the algorithm seems to be working just fine! Well the results are of cource not correct 😝. Here are a few tests
If anyone has a clue, please help me out 😁.
This comment has been hidden.
!
u may cal the all arrangements, but some arrangements are written the same. in the case(1100 , &^|), the arrangemnt &|^ and |&^ are written the same as(( 1 & 1 ) ^ ( 0 | 0 )). this may result in extra cnts.
This one looks scary as hell!
I'm with you here. I've always thought that.
I'm not sure I'm getting the instructions correctly. In the second case, Test.assert_equals(solve("ttftff","|&^&&"),16), why are there supposed to be 16 arrangements that evaluate to True? These are the possible arrangements of parentheses:
((((1|1)&0)^1)&0)&0 (((1|1)&0)^1)&(0&0) ((1|1)&0)^((1&0)&0) ((1|1)&0)^(1&(0&0)) (1|1)&(((0^1)&0)&0) (1|1)&((0^1)&(0&0)) (1|1)&(0^((1&0)&0)) (1|1)&(0^(1&(0&0))) 1|((((1&0)^1)&0)&0) 1|(((1&0)^1)&(0&0)) 1|((1&0)^((1&0)&0)) 1|((1&0)^(1&(0&0))) 1|(1&(((0^1)&0)&0)) 1|(1&((0^1)&(0&0))) 1|(1&(0^((1&0)&0))) 1|(1&(0^(1&(0&0))))
There are 16 of them in total, and at least one of them is False (namely, the first one). So... is there something I'm completely missing? And if so, what is it?
There are more of them, for example: 1|(1&((0^(1&0))&0)) 1|((1&(0^1))&(0&0)) 1|((1&(0^(1&0)))&0) 1|((1&((0^1)&0))&0) 1|(((1&0)^(1&0))&0) 1|(((1&(0^1))&0)&0)
i'm sorry, I read the wrong answer
Im exactly in the same boat as you, did you ever figure that out? If you go by the examples provided in the intro everything must be enclosed in parenthesis. And the combinations of parenthesis go 1,2,4,8,16, etc for length 2,3,4,5,6, etc. It desnt seem to make sense.
well,that is very funny.i have one solution not be tested which is that you can select one operator and than calcuate the result that the right number and the left,and then put the result in the new number list and reselect one operator and repeat so on.The only case what i concern is that this alogrithm will overtime>.....
Fun Kata! I think my solution was memory over efficiency but oh well.
I'm glad you enjoyed it. It's a real brain-teaser.
Hella challenging. I feel embarrassed, but I had to inspire myself from a solution I found online. Definitely a mind bender. Thanks for the challenge!
This comment has been hidden.
Making this comment to test if I can read the comment since my question is hidden from me because I haven't solved this kata yet (I think).
I think there must be an issue with the acceptance test. The basic test is passing fine for me but the remote test is failing with warnings that say warning: comparison of integers of different signs: 'int' and 'std::__cxx11::basic_string<char, std::char_traits, std::allocator >::size_type' (aka 'unsigned long') [-Wsign-compare]. It goes on to reference code that I didn't write. I tried every concievable change to variable types but I keep getting the exact same error.
It's not possible that this is an issue with the cpp version. 44 users have passed the tests.
Please post your code with a spoiler flag and someone famiiar with the language should address your question. And please use the
question
tag, not theissue
tag.I have the same issue(
The message you receive is not an error, it's a compilation warning. It probably does not cause your solution to fail. If all of your tests are green and you still see this message, then it does not harm your solution in any way, it's still OK. If your tests turn out red, on the other hand, then the mentioned message is not the cause of failure, and your solution is probably incorrect.
Also saying that "tests are OK because 44 users passed them" is the most lame excuse someone can give. It definitely is the issue with C++ version, it simply does not cause tests to fail (probably). But it's still misleading, because when tests fail, then compilation report filled with warnings can put someone looking for the problem on the wrong track.
This comment has been hidden.
How about an advanced variant of this kata where it doesn't count (a&b)&c different to a&(b&c) Test.assertEquals(solve("tft","^&"),2); Test.assertEquals(solve("ttt","&&"),1);
Feel free to explore. A new Kata is always welcome!
If it's not a duplicate and it has a good testing suite, sure ;) Otherwise it'll be shot down in the head and burned in a few hours.
looks like I can't make katas. maybe you need a certain level before it lets you?
Yes, you need a certain amount of honor points. Unfortunately, the limit is rather low...
looks like it is 300. so as low as that is, I am EVEN LOWER :D
What if you added this to the sample tests: Test.assertEquals(solve("ttt","&&"),2);
I think that might eliminate a lot of confusion about what counts. This is assuming that your answer (like mine) does return 2 for that test.
I've got a solution that passses all the sample tests in ~0.3 seconds. It times out in the full test though. If it isn't too much to ask, could you possibly tell what the maximum length of 's' is that we're supposed to handle, so i can know whether or not im on the right track? Thanks!
Each input can have a max length of 30.
Thanks, I appreciate it :)
Great Kata. Thank you! For days I was a useless father and husband :)
It happens 😄
C# Translation added.Please review and approve~
Many thanks. Approved!
Some of the test sample results' orders of magnitude too big num = 945766470799 for i in range(num): i += 1
In second case
Test.assertEquals(solve("ttftff","|&^&&"),16);
Which of these should be considered as distinct?distinct??
The Kata wants you to figure out how many evaluate to
True
.All of the above strings evaluate to True.
There are 36 listed, but the test case only wants 16. All of the above test cases match the criteria:
I think the instructions need to implicitly state what else must be considered?
How does the problem define an "Arrangement" such that it excludes several of the above cases?
The answer is that 20 of the 36 listed above are duplicates.
Here is a suggestion of an explanation for the instructions that define a distinct arrangement:
I would really like an answer to the question gkucmierz is raising. Some clarification with examples would be very helpful.
For this particular case, there are numerous duplicates. For instance, there is no real difference between the first and the second line given above. Adding extra parentheses does not change this fact. Look at them again:
The main differences are at the ends of the two equations.
Extra parentheses would have made sense if the last two operators were different. If they are the same, as in this case, extra parentheses result in duplicates.
I hope this helps.
This line:
[ '(1|((1&(0^1))&(0&0)))' ]
is in your list 3 times, there are probably more duplicates.
solve("ttftff","|&^&&") == 16 Im really cant get it =) As I understand if first argument is true and last action is ( | ) than we can have variations of last 4 actions (&^&&) = 1 * 2 * 3 * 4 = 24 and all this solutions are true. But in test we got only 16. Help me please !
I also cant get 16, my result jes 36 in this case.
so what's the problem? do we misunderstand something? I cant get it...
This is a great challenge, thanks, but I'm struggling to optimise my code! Similarly to some comments, I solved this for the order in which terms are evaluated - giving multiple solutions for the same parenthetical expression, e.g. (t&t)|(f&f) - this can be evaluated in two orders, but only counts as one solution.
I made my function translate the order into parenthetical notation, then filter out duplicates, so it works, but this makes it far too slow. Any tips on how to reconceptualise this?
I had a really hard time understanding the rules, like several people in the comments, because you express the problem in terms of adding braces.
Could I suggest that you phrase the problem something like: "Varying the order of evaluation, how many distinct expression trees evaluate to true?"
This is unambiguous and answers questions like: "is t|t|t distinct from (t|t)|t and t|(t|t) ?" (answer: no, it's either one or the other)
Even once you understand the problem, I think it's a little hard for 3 kyu. I really enjoyed it anyway, so thanks.
Hi, i have problem with c++ random test. String with operators contain same number of characters as string of values:
basic_tests Log size var=3, size_op=2 var=tft, op= ^& size var=6, size_op=5 var=ttftff, op= |&^&& size var=8, size_op=7 var=ttftfftf, op= |&^&&|| size var=9, size_op=8 var=ttftfftft, op= |&^&&||^ size var=10, size_op=9 var=ttftfftftf, op= |&^&&||^& size var=25, size_op=24 var=ttftfftftffttfftftftfftft, op= |&^&&||^&&^^|&&||^&&||&^ Test Passed random_tests Log size var=20, size_op=20 var=ffttfttftfttfttttfff, op= ^^&^|&&||&&|||^&&&^&
My mistake, I'll fix this as soon as I can. Right now, it should be OK to ignore the last operator.
Thank you!
C-V, is this fixed?
It's fixed now. I'm really sorry it took so long
(the translation editor didn't work and being reluctant to use the kata editor I kept delaying)
Many thanks!
C++ translation published
Many thanks. Approved.
@C-V: did you fix the C++ translation? (see issue above)
According to the description above, solve("tft", "^&") should be 2. You have replied to a question that
"(t ^ f & t)"
is a valid expression. I'm confused, because all of these are true:"((t ^ f) & t)"
= True"(t ^ (f & t))"
= True"(t ^ f & t)"
= TrueWhat's wrong with my logic? It would help for me, if your description include the False cases as well.
Ignore the case that does not have inner parenthesis
Why?
What are the rules for how parenthesis are placed? I think many poeple will skip this Kata because the rules seem unclear, and solving will require guessing and checking against the test cases to understand the problem.
EDIT: "(t ^ f & t)" is the same as "((t ^ f) & t)" If each expression is re-written in a notation without parenthesis (using order of operations), the expressions are identical, and count as one.
KenKamau is intentionally being vague about the rules.
Agree, requirements are not 100% clear especially when reading some of the comments here. I actually just finished this kata and based on the test results, it's definitely the case that only order of operations is considered, not parentheses. For example:
Assuming
&
has equal or higher precedence than^
, the two expressions above have the same exact order of operations, and so should only be counted once.This comment has been hidden.
This comment has been hidden.
Many thanks. Both fixed.
This comment has been hidden.
This comment has been hidden.
Excellent kata. I learned a lot about how to optimize my code from it! Thanks:)
Thank you!
Scary kata at first sight but once you get the right approach, it is an enjoyable one! Do you have such other tasty katas? :-D
Nice solution too!
Java translation
Please review and approve.
Approved. Thanks.
Approved. It is not hard but i should admit that i was scared when i first saw it. I think it is 5kyu but considering how many skips there are, well, approved with 4kyu. Please let me know if the rank should be modified
Many thanks Zed. This is OK.
This comment has been hidden.
Dammit you can understand the code, i think itself would stop anyone from trying to read LOL No, but to me, any problem would be 'scary' if i can not come up with a solution within seconds o_O
Any chance we can raise this to 3Kyu? Just look at how users have been performing.
As you wish o_O
Many thanks. 113 skips out of 488 users is a bit much.
I'm not sure that "skips" are really meaningful, actually.
I just thought the Kata is more difficult than most 4 kyus. The skips issue is just an observation.
In random tests length of ops must be one less than of string. However ops and string have the same length.
Which language?
python, sorry.
Just an oversight on my part. It shouldn't affect the outcome. Will fix it shortly.
All fixed now. Please check.
Resolved. Let me know if you face more issues.
Just to clarify with a couple examples, the following sets for each example are all regarded as distinct?
and
Correct. Any of them that leads the whole expression to evaluate to true must be counted.
Are you suggesting that these three expressions count as 3, not 1? If so, why are these considered different arrangements?
The answer to this question contradicts the description of the kata. In the kata description it states:
For example, solve("tft","^&") = 2, as follows:
However, in Example 1 above, three possibilities are given, applying this to the example from the description gives:
So, there are three expressions that evaluate to true, not 2 as given in the kata description. This kata needs further clarification, another example would help.
For the tests,
Expected
andinstead got
are reversed.Fixed.
I feel that the instrutions are a bit vague: why do you need outer braces? What about the normal operator order (left to right)? Is there any difference between
((t ^ f) & t)
and(t ^ f) & t
and(t ^ f & t)
andt ^ f & t
?!I will clarify.
Hi anter, perhaps you can clarify your question. It seems obvious that
((t ^ f) & t)
,(t ^ f & t)
and(t ^ (f & t))
are different, because we have inner braces that need to be evaluated first, and this will affect the result. The outer braces are not necessary. I will change the description.Well, the inner braces are not necessary if they don't change the order of the operations:
1 + 2 + 3 = (1 + 2) + 3
, or4 * 5 + 6 = (4 * 5) + 6
I'm not aware any precedence order for bitwise operations (unlike in normal mathematics): so
(t ^ f) & t = t ^ f & t
, but correct me if I'm wrong.Cheers
The idea is to apply as many inner braces as possible and count every outcome that is
true
overall, just like it says in the description. Submit your solution, then see what you get.Can you delete this if it is not true? You answer another question about the exact same three statements and say:
"It seems obvious" to me that only two of the statements are unique.