Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Honestly I couldn't figure this out on my own but when I attempted it - sometimes it would complete and sometimes it would fail.
I came up with a cheat by putting the for loop in a function and running it under a while loop.
If the function doesn't return the same string as before it will run again until it returns the same string as it did before.
Since the triplets are random you can get inaccurate results only parsing over the triplets once. Puayny is correct in their comment "This solution is incomplete though elegant, it will fail for some test cases currently not included. The 'for l in triplets' loop shouldn't run just once, it should run until there are no more swaps.". Their comment is the most efficient solution however since these are triples you could also just run the loop twice which I think is more than enough to get the correct sequence.
you have the wrong solution
if we check it on all possible triplets permutation for first test, then as a secrets we get
defaultdict(<class 'int'>, {'whatisup': 2565, 'tuwhapis': 120, 'uwhatpis': 76, 'twhapisu': 240, 'twhuapis': 6, 'twhapuis': 26, 'whatupis': 79, 'whatpisu': 660, 'whuatpis': 9, 'whatpuis': 16, 'twhaisup': 180, 'twhaupis': 28, 'whatipsu': 285, 'wuhatisp': 35, 'wuhiatsp': 88, 'whiuatsp': 71, 'whiatsup': 195, 'whuatisp': 18, 'whuiatsp': 10, 'uwhatisp': 7, 'uwhiatsp': 11, 'whatiusp': 30, 'whatuisp': 30, 'whiatusp': 60, 'twhiapsu': 60, 'whiatpsu': 75, 'twhiasup': 60})
I recommend using a set comprehension:
Besides being more terse, this doesn't build an extra list temporary.
is wrong
on python 3.8
its wrong
This comment is hidden because it contains spoiler information about the solution
I think a generalized version of this solution that runs multiple passes and stops only when 'r' remains unchanged for two passes would be a good answer. I made a rough implementation here https://www.codewars.com/kata/reviews/53fbcb9ecfc5f67703000032/groups/5ff218e51d40250001bac79a
This comment is hidden because it contains spoiler information about the solution
Can you please explain me what exactly changed in Python 3 which makes the solution break?
This solution passes the tests but to me is definitely not best practice. This solution works in python 2, and not in python 3. But if you just sort r it works the same in py3 as in py2. That's the difference. r comes out mostly sorted in py 2 from the creation of the set apparently.
BUT it still is not a great solution in py2 (or py3 with the sort). Yes it passes the tests, but it's not always going to get the right answer to different valid inputs.
For example - if you feed in the last test reversed (recreate alphabet backwards) it fails. But if the loop is run twice instead of once it does pass. So additional looping may be needed accurately reconstruct the secret with this approach. To see it fail trying to create alphabet backwards fork and add this to tests:
secret6 = secret5[::-1]
triplets6 = [x[::-1] for x in triplets5]
test.assert_equals(recoverSecret(triplets6), secret6)
Nice solution, two comments:
-the use of the variable 'l' has two distinct uses which detracts from readability. Suggest using a different variable name than 'l' within the fix function.
-swapping the two variables can be done with a,b = b,a in a single line
lol, this is a wrong solution. How did it passed these tests? Luck or just 'bruteforce' attempt? :D
Can someone please explain how the list variable r get changed on the fix function?
I thought the l is a local variable, how does it affect the r variable on the original function?
OK, i kept digging.. it turned out that list are mutalbe which means that l and r refer to the same address and changing one of them will change also the other one.
I have a similar solution, I'd say the exact same approach but somehow random test cases failed sometimes. I ran the loop in recoverSecret function twice to double check and it worked then. I'm quite surprised with the result and pretty sure I'm doing something wrong. I'm begginer so if someone could steer me in the right direction I'd really appriciate it.
Loading more items...