5 kyu
By the Power Set of Castle Grayskull
1,375 of 2,728xcthulhu
Loading description...
Mathematics
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.
Only 1 random test in CS
New framework should be used
Nice one, well done, many compliments for the kata.
OCaml translation (author inactive)
Approved
Im getting an array.sort error when testing
TypeError: array.sort is not a function at /workspace/node/test.js:37:37 at Array.map () at normalize (test.js:37:18) at doTest (test.js:39:17) at Context. (test.js:47:3) at process.processImmediate (node:internal/timers:471:21)
Seems like an issue in with the testing suite at first glance but i could be wrong, any ideas?
You are supposed to return a 2D array. If you return a 1D array somewhere, this happens: the test suite stumbles over your wrong answer instead of gracefully failing you. ( This cannot happen in a strongly typed language, but in JS, it can. )
It could be argued that the test suite should catch this, but you're really just getting a
TypeError
instead of anAssertionError
, for which the error message is not as nice. The test still correctly fails.I think this kata is broken..it is randomly giving me that "int object is not iterable" at line 5 even though there is no code written on line 5🙃
That's because your code isn't always returning a list of lists. Not a kata issue.
Useless for me, simply because requirements are incomplete.
The test expects a list with a specific order and the order has nothing to do with (sub)sets. And there is no specific requirement defining precisely any specific order.
seeing that this is one of your first katas on the site, you could try solving easier katas first (7/8kyu) and then move to more complex ones like these.
p.s., this is not a kata issue
EDIT: the tests are framed in a way such that you can return the powerset in any order. the test suite will sort it for you
sidd114, could you please add this information about 'any order' in the description of the kata. Because that was the first question I've got after reading the kata.
D translation
Rust translation
Description should be language-agnostic
done
Lua translation!
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/130.
Please join the discussion to help us identify duplicate kata and retire them.
This kata was decided to stay.
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/118.
Please join the discussion to help us identify duplicate kata and retire them.
Wrong link, closing :)
no sample tests in CoffeeScript
( JS )
Needs updating to
Node 14.x
. Needs updating tochai
. Needs Example tests. Needs more fixed Submit tests. Needs more random Submit tests. Needs to ask for aSet
instead of anArray
. ( OK, that last one is debatable. )all done except the last, i dont see what it adds since
(new Set([[], []])).size === 2
The issue is that a power set is unordered and can not contain duplicates.
But JS semantics mean it can still contain values that should be duplicates, but are not. Hence debatable.
JS
Set
is not an accurate implementation of a mathematical Set; JSArray
is worse.So, even by invalidating all existing solution, the problem can't be solved ( JS does not have the appropriate datatype ); it can only be made less bad. Which arguably is not worth doing; I support that decision.
Some libraries are not allowed by Haskell's tests, the description does not say a word about that:
Moreover other languages with similar built-in libraries (Python and Ruby) don't have such limitations, which make this kata disporportionately difficult in Haskell.
For a short one-liner that's ranked
5kyu
, should the restrictions be lifted all over, or rather enforced all over?I have no idea (and I can't see the solution in your link, I have not solved this kata in Haskell).
Well, this kata has never had the pretention to enforce restrictions, so there is no reason to state them now.
This comment has been hidden.
Getting this error:
ModuleNotFoundError: No module named 'codewars_test'
Seems the backend needs to be updated, from my understanding?
Language?
Duplicate issue of
no sample tests
belowNo sample tests.
Hi everyone, I've translated this kata to Swift and would love to get it approved :) authors, power users - check it out ;-)
https://www.codewars.com/kumite/5b70a4721db5ce253b00009c?sel=5b70a4721db5ce253b00009c
it's been a few weeks since I posted it..
Thanks !
Approved some time ago
it doesnt look approved
he pasted the link for another of his translations which has been approved, but he also made an unapproved translation for this kata
I have some problem with this test-case,("Expected: 8, instead got: 8")
This comment has been hidden.
Not an issue
I'm running into an problem when I submit the kata. I'm passing the first test, but then I'm getting the following error:
ReferenceError: i is not defined
I've tested my code in the node cli and it seems to work fine. Is there a way to see the tests that get run on submit?
This comment has been hidden.
My solution first failed on the random non-numerical test. From the error message it was obvious that if the random characters given in the test are not uniq (there are multiples of some character), then my script fails. So I modified the script (rocket science, applied #uniq! on the method argument) and the test was successful. Then I could look at other people's solution, and I saw tons of solutions that would fail on this test.
Hello,
Why is my solution marked as possible cheat?
Hi
I've implemented a solution for this problem in Haskell, but when I submit it, it fails for tests with the input consisting of lists of zeros, eg, [0,0,0,0] or [0,0,0,0,0,0,0,0]. What is the expected solution for this? If this was a set, then this would be an invalid input since there can't be duplicates, correct? I'm removing any duplicates, which results in the set [0], which should have the power set [[], [0]], but this is not being accepted. Any suggestions?
Cheers Gautham
It isn't a set, but a sequence/list. The title "power set" is a little bit misleading. You need to return the set of all subsequences, not the set of all subsets, e.g.
Cheers, I thought I'd tried that too, but there was a bug in the code. It's working now.
Then how is [1,3] a subsequence of [1,2,3] as stated in the example?
This comment has been hidden.
Greetings xchtulhu and first of all thanks for this kata :)
I took the liberty of translating it into Ruby and adding a twist (not numeral randomized lists) to the tests. I hope you will like enough to add it to your work and/or give me some valuable feed to improve my skills :)
Cool kata, neat puzzle!
This comment has been hidden.
I assume you are using Python 3 (not yet supported here on CodeWars), as you use the print() (notice the brackets) function.
Also, it seems you forgot to RETURN a value: your function just prints it on screen.
Finally, you are returning a list of tuples, while you should return a list of lists.
I hope this helps (and that CodeWars will notify me if you reply to this: I am getting very few messages notified, sigh...).
Sublists or subsets? Presumably you mean the latter, since that's what the example and title both indicate, but the instructions mention sublists.
Perhaps specify ordered sublists (to accomodate languages without a readily built-in set data structure)?
In python you can use whatever iterable you want.
I'm getting a so-called "Unknown error." I can't give to code or it will be a "spoiler" though. Note that it works on my local machine.
Here, I've marked your post as a spoiler, feel free to put your code down.
Okay, I'm marking this as resolved.
please any one tell me how write this problem test case ?
[Again, hoping notifications will work on my profile again]
You can just submit it to get some testing, otherwise if it is js, python or (I hope soon) ruby, I can give you a couple of tests.
This comment has been hidden.
I can't reproduce your bug; are you still struggling?
Ok, I got it. Your second test does not like when the array passed as argument is modified in place... I had to make a copy to get it passed. (Maybe you should make a copy in the test, or specify in the description that the input array must not be modified)
I will specify that the input array must not be modified. Thanks for your help!
Testing code may lack
Since the problem deals with computing the power set, it'd be natural to expect the result to be given as in the set type, rather than list type.
This comment has been hidden.
Please close this issue.
@Sebbe The author wrote this elsewhere, which I think answers your question:
"In Javascript/Coffeescript, you will need to use a nested Array.
In Python, you can use nested lists, nested sets, generators of generators, I wrote it to be pretty flexible."
Okay, I'm closing this issue now.
Does the solution have to accept a nested list/array, or is it sufficient to deal with a flat list/array?
A flat list would just be a list of numbers. You need to produce all possible combinations. Those combinations are grouped in their own nested array.
In Javascript/Coffeescript, you will need to use a nested Array.
In Python, you can use nested lists, nested sets, generators of generators, I wrote it to be pretty flexible.
By 'accept', I mean 'accept as input'. You show flat input in your example
but should I design my function/method to also find the power set of
?
Sure, but
power([1, [2, 3, [4, 5]] 6]) != power([1,2,3,4,5,6])
So I'm not sure what you have in mind.
From what you say, I think the answer is no. I guess I had in mind that maybe it was recursive, something like power([1, [2, 3, [4, 5]] 6]) ==
I was overthinking it!
The description still could use some work. You give an example, but never explicitly say what the function should do, or what a "sublist" means. I've heard it somewhere before, so I had a head-start, but unless explained, it's a pretty hard thing to get your head around.
Maybe something simple like:
...
Each combination of values should be included in a new array. A combination can be of length 0, up to the number of items in the original array, and any number between.
So the "power set" of
[1,2,3]
should include all combinations of length 0 (there is just one:[]
), and any combinations involving all items of the original length 3 (there is also just one:[1,2,3]
). Also, each combination of length 1 ([1]
,[2]
,[3]
), and 2 ([1,2]
,[2,3]
,[1,3]
) should be included.Note that the order within each combination doesn't matter, and nor does the order of the combinations themselves within the power set.
So in the example above, the resulting set (array) will look something like this:
[[], [1], [2], [3], [1,2], [2,3], [1,3], [1,2,3]]
....
This should make it super-clear what is expected.
I'm partial to the Hemmingway philosophy on describing these things. Do you think a link to the mathematical concept on wikipedia will suffice?
Ah. That Hemingway..! I subscribe to the "get out of the coderreader's way and let them understand what's going on" philosophy, myself. ;P
Unless you know math to begin with (and probably understand "power set" already), math diagrams and notation is pretty darn confusing.
Sticking with Hemmingway...
Your first test seems incorrect, unless I've misunderstood (which is quite possible)... It's got
[1,2,3]
in the expected value set twice. Shouldn't a combination only show up once?I checked this again and I can't reproduce this. I don't have
[1,2,3]
twice. Can you print your error here?Expected: [[],[1],[1,2],[1,2,3],[1,2,3],[1,3],[2],[2,3],[3]], instead got: [[],[1],[1,2],[1,2,3],[1,3],[2],[2,3],[3]]
I got the order wrong in the fixture... thanks for helping me debug this.
No worries ;P
Has it been fixed, now? Still getting the same error; maybe it's taking time to go through?
Just fixed.
Still not seeing it...
Aaaaand it's through. Kind of amazed at how long that took, but thanks for your help!
Really? When I put in:
I get:
Don't worry; just took a few minutes to make it through. All sorted now, and I passed the kata. Nice work; it's an interesting task to go through.
Sorry if it's a dumb question, but... what on earth am I meant to be doing, here? I know there's some in-joke in the title, probably some sort of mathy thing, but beyond that, there's not much to go on.
It looks like there is currently no example code in the kata description for JavaScript or CoffeeScript. Only the Python version gives this code example:
The kata is looking for the power set of the elements in the given list/array. The description could definitely stand to be improved on this one, especially since in many programming languages
[1 3]
would not be considered a sublist of[1 2 3]
.So a "power set" is basically all combinations of length 0 to the number of items in the original set?
So sorry, I'll port the example to the other languages.
Okay, the code listings should be fixed now. Hopefully it's clear what's going on.
Thanks. And thanks for the help, @laoris!
A "power set" of a set E is P(E), that is the set of all subsets (part) of E. Why is it called "power set" ? Because you can see it as the set of all functions from E to the boolean set (if True is associated to an element, it is in the subset) which would be noted B^E (if B is the boolean set) and thus has 2^(Card E) elements.
Of course the interpretation with lists/arrays is to handle those as sets, so you have to forget the ordering of the elements.