4 kyu
Solving a puzzle by matching four corners
138 of 215tonylicoding
Loading description...
Games
Performance
Puzzles
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.
Thanks for the kata Tony!
funny!
This comment has been hidden.
This comment has been hidden.
This was a great Kata. Similar to other solver feedback, named Tuples would have been a nice to have (but I think your approach seems very common on code wars). Another minor point of feedback is that I totally missed for an attempt or two the fact that puzzles can be rectangular. In hindsight, it's very obvious, since you have width and height variables, but with the square example I got a bit lost in the sauce. Finally, a very very minor grammatical point: In notes, I would change "Be careful about performances" to "Be careful about performance".
A lot of the solutions I've seen were huge and/or O(n2), I believe this is solvable in O(n).
Thanks for your work!
I think O(n^2) is the lowest possible time as you need to have a nested loop. Take your solution for example, it has a nested loop, so its O(n^2).
This comment has been hidden.
first things first: what is
n
exactly?@tony:
here, n being the number of pieces, the best solution is O(n) (for the precise reasons Ryanman is explaning)
If n is the number of pieces, yes as i was talking about like two nested for loops, it is either O(n) or O(n * m) where n, m is height and length.
I also don't believe any solutions are O(n^2) if n is the number of pieces. By doing a simple nested loop with
for p in pieces:
twice, it times out..
Great Kata!
Very nice and challenging kata! Took me a lot of thinking to solve this.
Thank you so much for this brainstorming kata. Through pain and suffering finally got it. It was a good experience for me.
Glad you enjoyed ;-)
@tonylicoding, Nice kata; do you plan to translate it to other languages?
Nope, all translations welcome;-)
Does each number appers only once (e.g. up to 4 times in one place in the puzzle)? I've just read the kata now so I will figure it out soon, but it should be mentioned... (You wrote thtat pieces are uniqe, but this is not the same)
OK, I've solve it and I got my answer :)
I think an "advanced" version of this kata may include -
In addition, there is h and w aren't neccasary...
The answer to this question is crucial to solving this kata. It's not apparent from the description, but after reading this comment, I realise I have waisted some time with a way too complex back-tracking strategy.
:whistling:
:whistling:
;)
Well this made me solve it in 2.5 seconds :p
Is it guaranteed that each side of the puzzle has a border of Nones? Since all edges are unique, I don't think the border is really required for all puzzles. Will it be always there?
yes it will. It makes easier the generation of the inputs. ;) (edit: mmmmh.... I see what you mean clearer... Tho, you need to clearly identify the edges/corner, to keep the original idea and its inspiration. So border need to be different from other pieeces. Then, to keep things simple, having them at the same size that the other pieces makes it clearly more friendly.)
Good kata! I'm still working on it, but I wanted to point out an issue with the sample data given in the description: puzzle piece with id 8 does not match the output.
What do you mean that it doesn't match the output? For the description? Ok, see it now.
Yep good catch. Will resolve this one, then edit.
Fixed. Click reset.
Looks like you've completed this. Good Job to get to 6kyu and to complete this kata (with the coding experience).
May I ask for some little hint, as a beginner? What advanced topic (data structure or something) do I need to know to complete this?
no advance topic and (no recursion)
just understand complete problem and solve it step by step
just to get you boost
Thx :)
Is it possible to implement this without using a for loop in other for loop.
Depends what you actually do with this...
Every one will use 2 loops at some point, one way or another, since you have to travel through the puzzle. Your goal is actually to try to not have any other loop nested/hidden in those two loops. The targeted time complexity here is O(MxN). If you use more, there are close to no chances your solution can pass, considering the performances tests are run on puzzles in
[50-100]*[50-100]
'hope this helps...?
All I do is just sequentially look for pieces. But still my code is too slow. Can you inspirate me by any ideas?
Your code is similar to bubble sort. Bubble sort is one of the slowest sorting algorithms, so no surprise that your code is very slow. Try using less for loops and ecspecially not switching each one.
Oh, thanks. I will try to optimize code. And... I'm sorry. I am new to the codewars. Just would like to know. You can see my unpublished code for this task, right?
Yes, if you click attempt, I can see your current solution so far.
My code runs for just over a second (1.2) in sublime with one of the larger tests. It gets the right answer but it wont pass the Kata tests. I have no idea how to optimize it. I probably need another approach.
Yes. A for loop inside a for loop inside of another for loop is way too slow. You have to get rid of that. One for loop with no for loops inside will be fine, if you know what you're doing. You can even have multiple. You may have a for loop in another for loop if its really simple, and everything is simplified enough.
Any tips on improving time? I already have a solution but it's too slow.
Don't use too many loops, and make sure they are as simple as possible. Also, a change of approach maybe? You would've solved a 5kyu without performance measures, just know that.
kata is nice :)
small suggestion though; replace length with height (more suitable but still a matter of test)
edit: about perf., you should add perf. (as B$B said) to make it 4
Changed length to height. Also, B4B gave me a choice. To resolve this suggestion, I made this performant. Resolved.
should we enforce performances requirements...?
I tried using this:
my solution solves the tests in roughly 6-8s, every other ones are timing out, so far (like... really every other ones...)
For performance, I think we should use just a few perhaps 10? This isn't a performace kata after all :D
(possibly, yes. But that woudl make it 4 kyu for sure. Currently I feel it more like 5...)
I really want to author a 4kyu (honor yes), but for 4kyu then my average kata rank will be exactly 6kyu. But there are questions surrounding it. Do I use your solution or do I make my own? I'm making another kata that's 4-3 kyu. So, I don't know. You have more experiece. It's all up to you.
?? -> not following (edit: avg of authored katas, you mean? oh... x) )
Making another version of this one doesn't seem like a good idea here. Let's wait some more feedback, maybe.
Yes
ok, so:
There are pros and cons to both. Keeping it easy would allow users to solve it with more classical approaches then see more efficient solutions. Making it harder will make the problem more valuable (on the honor/progress side), but will possibly make it a bit more painfull for the users...
Your kata => your call... ;)
For 5 kyu, there will definetely be more users who solve it, but for performance there will be more honor and my average rank for authoreed katas will be 6. I'll go with 4kyu and make it performant. So my call is 4. Then I could also mark another suggestion as resolved. ;-)
done
Thanks.
how to solve puzzle? do i have to arrange puzzle such that 2X2 sub puzzle match all numbers?
You solve the puzzle by rearranging it, so all the corners match. Look at the example input puzzle and the example output puzzle. After you rearrange the puzzle, you look at each piece number. Then, return a list of tuples, each with an amount of puzzle piece numbers. Each tuple contains a
width
amount of numbers. The list contains alength
amount of tuples.Just look at the examples and find the pattern. Find the obvious things, for instance the borders. Then, you may do it any way you wish. Different people solve it differently.
Hopes the new description helps :P
hi,
I didn't actually try yet but you need to clarify the description. All the needed information is in there, but the order is clearly suboptimal, especially considering the length of it. The main problem is that you lose the reader by adding details before giving the main parts. I cannot be more precise right now (need to go to sleep), but this is mainly about splitting the description differently.
another problem: the representation if the pieces seems suboptimal too. am object would be cool, but you'll have mutation troubles. A namedtuple would be better, on the other hand:
namedtuple('Piece','val idx')
, for example.note: don't close this too quickly, plz. I won't be around for a of time to develop about all of this.
@Blind4Basics
For the first one, do I change the order of the description: if so, in what way would you suggest. Any tips?
(I might need some days to properly answer to this. Poke at me in 3-4 if still no news)
No need to hurry. After this is solved, it will be ready for appoval. The issues mentioned by you guys, when resolved, will make this kata beter. Then, that's better quality will go to the people who only do approved katas.
Hey, it's been 3 days. You can honestly first finish the kata, then edit it by yourself if you would like.
hi,
Don't worry, I didn't forget. Just not enough time for now. ;)
It looks like you've solved the kata congrats. You may edit it as you like.
yup, just minutes ago. It's a pretty cool thing you did there. Really interesting things to build. I'll see about the description and post a fork of your solution so that we can discuss about some changes.
For now, one question: is that intended that one never find to possibly identical connections? I mean, every piece can currently be linked to a single other one. So no "trial/error" process. That makes it easier, but at the same time, that makes it more interesting because it allowes some interesting sideways (as you can see, I didn't even used the dimensions of the puzzle in my solution... x) )
Well, yes because in a puzzle, either the connections are always different, or you can see the difference between the pictures. I think my katas are more intresting, but it can also make the kata easier. Thanks for the feedback.
NOTE: the average rank would've been 4kyu if 2 people didin't rate it 8kyu.
ok, sounds consistent. I'll dig into that description (I actually never read it completely for now... XD ) and your test suite, then post that fork.
About the rank: yeah, I know... x/ (edit: tho, without that backtracking stuff and with a clear enough description... I guess it could be 5)
Well, this is the first time I've actually made a kata without bringing in a refrence solution. Looks like it's pretty good ;-). My random puzzle generator generates puzzles that are solved, then I random.shuffle it. See if there could be any improvements(ecspecially in the description), and that would be great.
Thanks again for your time and hardwork regarding this kata.
Ok, now about that
nammedtuple
.I'd like some feedback of new solvers before resolving this one.
(I've someone in mind... Shouldn't take too long ;o )
edit: right, the namedtuple.... Actually, I'm not sure it's really needed/a good idea. At first, I didn't see the reason for the 2 tuples. That's the main reason I had that idea, I guess. After, I get the reason (somehow)
monadius?
EDIT: Ok, yes
(na, someone else but that would do too, sure x) )
Well, no issues after G_kuldeep solved it so resolved.