TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Here's a puzzle game. I call it Reverse the List of Integers

171 pointsby selfabout 1 year ago

26 comments

two-starabout 1 year ago
Hi. I&#x27;m the post&#x27;s author. This was something I dashed off quickly, so I was a bit imprecise in the language. Clarifications:<p>Only positive integers are meant to be allowed. (Zero excluded.)<p>Combining is meant to work on adjacent pairs of integers.<p>If this is used in coding interviews, I deny any responsibility. Unless it&#x27;s used in interviewing me, in which case I will totally take credit.
评论 #40026155 未加载
评论 #40016546 未加载
评论 #40071342 未加载
评论 #40021115 未加载
评论 #40015784 未加载
ch33zerabout 1 year ago
I thought the game was cool so I built a little version of it here:<p><a href="https:&#x2F;&#x2F;blaise.gg&#x2F;number_game&#x2F;index.html" rel="nofollow">https:&#x2F;&#x2F;blaise.gg&#x2F;number_game&#x2F;index.html</a>
评论 #40012951 未加载
评论 #40011275 未加载
评论 #40011292 未加载
评论 #40011429 未加载
评论 #40016729 未加载
评论 #40015151 未加载
评论 #40011293 未加载
评论 #40014088 未加载
评论 #40013584 未加载
评论 #40014130 未加载
thih9about 1 year ago
This can be transformed into a problem with pegs and moving blocks.<p>Like tower of hanoi[1], but you can add or remove empty pegs, blocks are the same size and can be stacked in any order, you can move as many blocks as you want and you cannot have towers with the same amount of blocks.<p>Unless you want to deal with negative integers, then it would get more tricky.<p>[1]: <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Tower_of_Hanoi" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Tower_of_Hanoi</a>
评论 #40011780 未加载
评论 #40010923 未加载
评论 #40010987 未加载
orlpabout 1 year ago
It&#x27;s not possible in general, e.g. [3, 2, 1] can&#x27;t be reversed.
评论 #40010395 未加载
评论 #40027383 未加载
评论 #40011596 未加载
评论 #40010697 未加载
评论 #40010836 未加载
评论 #40011426 未加载
评论 #40010869 未加载
评论 #40010594 未加载
OskarSabout 1 year ago
Neat little problem! You could do SAT-solvers and stuff, but it&#x27;s also obviously a graph search problem: each state is a vertex, each edge is an operation that leads to another vertex. So, obviously: Dijkstra works (or really just BFS, since the edges are unweighted), but not the fastest in the world.<p>The fun one to play with would be A-star, but you&#x27;d have to find a suitable heuristic. One obvious candidate is that if you have target list that is X long and a current list that is Y long, then you need at least abs(Y - X) steps to get there (since each step either adds or removes a number). That&#x27;s admissable and would probably speed up the search quite a bit compared to regular BFS, but you could probably do a lot better. I&#x27;m thinking a heuristic based on the number of inversions or something.<p>I would suspect if you found a really good heuristic for A-star, that&#x27;s about as good as you&#x27;re going to get. Though maybe there&#x27;s something more clever I&#x27;m not spotting.
评论 #40050919 未加载
评论 #40020968 未加载
selfabout 1 year ago
Start with a list of positive integers, (e.g. [7, 5, 3]) and your goal is to make the same list, in reverse ([3, 5, 7]).<p>Operations:<p>1) Split an integer into two smaller integers. (e.g. [7, 5, 3] → [6, 1, 5, 3]) 2) Combine (add) two integers into a larger one. (e.g. reverse the last e.g.)<p>Restrictions:<p>1) You can never make an integer greater than the largest integer in the original list. 2) You can never make a move that results in the same integer appearing in the list more than once.
评论 #40010236 未加载
评论 #40017448 未加载
glitchcabout 1 year ago
The rules are too restrictive. Even a basic [1, 2] is not solvable.<p>For this game to be popular, the difficulty should grow (generally) with the size of the list and the relative differences between numbers.
deosjrabout 1 year ago
Here&#x27;s a gist in Prolog that I believe solves this puzzle: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;deosjr&#x2F;7314659509333ee2ad67dbf276e8d487" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;deosjr&#x2F;7314659509333ee2ad67dbf276e8d...</a>
评论 #40019816 未加载
commandlinefanabout 1 year ago
Oh boy, give it three months and we&#x27;re gonna start being asked this in coding interviews.
评论 #40016710 未加载
thrdbndndnabout 1 year ago
The rules for operation 2 didn&#x27;t mention that two integers need to be next to each other (vise versa for operation 1). Is it a requirement?
评论 #40010737 未加载
评论 #40012964 未加载
semi-extrinsicabout 1 year ago
I made a completely graphical representation of this game in P5.js - it turned out quite intuitive I think.<p>The integers are represented by the number of edges in each straight section.<p><a href="https:&#x2F;&#x2F;editor.p5js.org&#x2F;semi-extrinsic&#x2F;sketches&#x2F;IKOjwE7Vb" rel="nofollow">https:&#x2F;&#x2F;editor.p5js.org&#x2F;semi-extrinsic&#x2F;sketches&#x2F;IKOjwE7Vb</a>
ericydabout 1 year ago
I like it, but it strikes me that there are actually not that many valid moves you can make in the example problem. Maybe this isn&#x27;t true in other examples, but I found most of my time thinking about this was simply realizing that most of my desired moves were off limits. Perhaps that&#x27;s the point, it just struck me.
rokickiabout 1 year ago
Here are the maximum number of steps required through 10, and likely maximum number of steps required through 12:<p>6: 14 7: 26 8: 74 9: 86 10: 126 11: 106 (?) (full state space not explored) 12: 130 (?) (full state space not explored)
评论 #40051132 未加载
评论 #40032041 未加载
jpablomrabout 1 year ago
Coming soon to a coding interview near you!
评论 #40011670 未加载
评论 #40011541 未加载
评论 #40011551 未加载
paxysabout 1 year ago
I feel like those last two rules would make the puzzle impossible to solve for most sets of inputs. E.g. in [3, 2, 1] all possible splits are illegal.
spywaregorillaabout 1 year ago
Squinting at this... Feels like there is some relationship to asking if you can tile a triangle into integer-length non isoceles triangles.<p>It feels like it is typically going to be impossible. Most puzzles will seem to have only a few legal moves at a time, all of which will either loop back to wehre they are or lead to victory.Generally speaking though,odd numbers are useful.
bradserabout 1 year ago
My first reaction to this was, &quot;neat coding question for interviews&quot;. I started to try to solve it for a few seconds, then thought of when I&#x27;ve asked coding questions. Invariably starting by saying &quot;The goal is to see how you approach and work through the problem, and less so whether you come up with the optimal solution&quot;. Then I thought, what if the interviewer and interviewee tackled the problem together, both coming in cold? And that was stated as such, up front? It would be much closer to the real world of tacking novel problems as a team. Taking the pressure of &quot;I need to come up with the answer&quot; off the interviewee should result in real focus on &quot;working through the problem&quot;, and decrease interviewee stress; if the interviewer doesn&#x27;t know the answer yet, how can they expect the interviewee to come up with one?<p>There are a couple of obvious downsides like: What if it turns out the problem is too trivial? Move on to the next one. What if the interviewee has already encountered the problem before? No different than if the interviewer posed an already-solved problem.<p>It would be incumbent upon the interviewer to not reveal a solution if they come up with it first of course. And the evaluation of &quot;how you approach and work through the problem&quot; is less definite than whether an answer (brute-force or optimal) was achieved; but if approach is indeed the important thing, that needs to be evaluated regardless. I&#x27;m sure there are other downsides I&#x27;m not seeing off the top of my head.<p>I can&#x27;t be the first person to come up with this strategy, nor try it in real interviews. Has anybody attempted this? Was it successful or not, and why?<p>I can&#x27;t tell if this strategy that just popped into my head is of value :-).
评论 #40015796 未加载
评论 #40017092 未加载
HarHarVeryFunnyabout 1 year ago
Seems like there are going to be non-search algorithmic solutions that solve these non-optimally, then god&#x27;s solution where each split or combine works towards putting more than one number in place.
simonbarker87about 1 year ago
I hate little puzzles like this when presented in isolation, my brain just bounces off them.<p>If the EXACT same puzzle was presented as part of a real problem with context and reasons, my brain would be all over it and I&#x27;d work out a solution.<p>As another comment says, it will inevitably show up as a coding interview, one that I would likely fail!
评论 #40014359 未加载
评论 #40014296 未加载
评论 #40013774 未加载
评论 #40014713 未加载
amlutoabout 1 year ago
Assuming negative numbers are not allowed, the how about:<p>[3, 2]<p>Creating a zero is useless, you can’t split 3 or 2, and you can’t make a 5.<p>[2, 1] is similarly stuck.
selfabout 1 year ago
(not my post)
评论 #40010341 未加载
评论 #40012358 未加载
jojojafabout 1 year ago
So like, infinity, infinity-1, infinity-2, etc.?
j4cobgarbyabout 1 year ago
the best I can find for [7, 5, 3] is:<p>7 5 3<p>7 1 4 3<p>2 5 1 4 3<p>2 5 1 7<p>2 6 7<p>2 1 5 7<p>3 5 7
评论 #40010504 未加载
评论 #40010528 未加载
评论 #40010472 未加载
评论 #40015466 未加载
评论 #40010327 未加载
评论 #40013078 未加载
eschneiderabout 1 year ago
A dynamic programming approach would work. Basically, try everything.
评论 #40013160 未加载
selfieabout 1 year ago
Could a constraint solver just rip through this problem?
评论 #40014843 未加载
评论 #40014446 未加载
评论 #40014223 未加载
评论 #40011398 未加载
anthkabout 1 year ago
.... (reverse &#x27;list)