10
votes
Fortnightly Programming Q&A Thread
General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.
Don't forget to format your code using the triple backticks or tildes:
Here is my schema:
```sql
CREATE TABLE article_to_warehouse (
article_id INTEGER
, warehouse_id INTEGER
)
;
```
How do I add a `UNIQUE` constraint?
Let's say I have a world where I can put rectangles in any arbitrary location. The rules are the following:
Here are some examples of what I mean. The gray rectangles show the initial state. The transparent green rectangle shows the rectangle being inserted. The blue rectangles shows the newly inserted rectangles after running the algorithm.
Example 01:
Example 02:
Example 03:
Example 04:
Example 05:
Example 06:
Example 07:
Example 08:
Originally, I was using a brute force approach that stored every rectangle. Every time a new rectangle was added I'd discard these "vertical" rectangles and recompute the whole world. This time though I'm looking for a way to only update locally the rectangles that should change around where the new rectangle is added. Visually, the solution seems obvious, but I can't come up with the right algorithm that's as simple to implement as it looks.
(Eventually I'd also like to do rectangle removal or updating, but for now adding is enough.)
Edit: Fixed an issue with example 02. I had missed the rule 3 where I should merge vertically similar rectangles next to each others. snip
Look into forms of space partitioning like bsp or quadtrees. Those let you quickly identify which nodes your newly added rectangle overlaps with, and only look at rectangles within those nodes.
Indeed, I can do a quick query with that, but I was thinking more about the merging itself. But I think I was overthinking it. I just came up with 8 rules that I think will solve this problem. Not sure if there are mistakes, but I'll try to code this tomorrow:
To add to verticals:
Made up a bunch of words. Should be fun to implement that.
In this end this is what I got which is much simpler than the other thing I wrote in my other reply:
Let's say I want this, what I do is for each rectangle, I dissect them into two vertical range, one on the left of the rectangle and one on the right. The ranges on the same X axis are considered part of the same slab. I merge overlapping ranges. I sweep through those slabs one by one from left to right. Each time a previous slab doesn't have a matching range, a new vertical rectangle is created.
Another example: this becomes this.
I got a first implementation done. It's not yet super optimized, but it seems to work really well.
I'm working on a simple guess-style game in which I generate an array of letters, guess a sequence, test the sequence against the generated sequence, and display a message showing what is right/wrong.
I'm stuck at passing the array from one object to another, and am also unsure how to do it without regenerating the array each run.
I've got my code below.
When doing something like this, I like to print the output so I can actually determine that the data is what I expect it to be. Would the best way for me to get the data in GameArray.genARray from GameArray to ArrayInput be to design it so I call something like ArrayInput.(GameArray.genArray)? My comparison, AFAIK, will be done via
for
loops against the arrays (sort of fizzbuzz style lazy test), but I'm clueless on how to get the data where I need it to start doing testing like that.To me, using separate objects for everything seems like total overkill. Your objects are wrapping Lists, but you can just use normal functions on Lists without involving objects. I'd suggest the following solution:
You can add a loop around the last four lines if you want the player to be able to guess multiple times. Since the code is saved in its own variable, it won't change after a guess.
If you still need to pass something from object to object, you can define a method that takes the thing you want to pass in as an argument:
Spoiler
Definitely, but I wanted to do something of an intro to Python OOP with this project, and it felt like a good way to wrap my head around it.
Looking at what you wrote, I did things a little different because I had to define the inputs on the Game class. I had to create a new class, add the inputs, then use a method to add the inputs to the new class:
Putting it all together, I was able to determine everything is going to the objects it needs to:
and got the right outputs, the first of each pair being generated, and the second of each pair being inputted:
Thanks for the heads up about looping, I was going to do it that way anyway, and wanted to pass the data properly first.
I'm glad you found something that works. :)
I'm just slightly confused by the design. You're making Game a sub-class of both GameArray and ArrayInput, and that means that it can do both
genArray()
andgetArray()
-- you don't need a separateuser
ArrayInput anymore, you can just dogame.{get,use}Array()
.But it seems... messy to my sensibilities, because Game is now doing multiple things and things get shoved into it from the outside via the
playGame
method (which is a bit confusing itself, because it does not play the game, it just prepares it).If you're interested in further advice: I think it would be better to separate things cleanly into their concerns (without sub-classing), namely (a) making/breaking the puzzle and (b) reading player input.
I would make a
class Puzzle
that generates a random code to break in__init__
and does not concern itself with player input. Then you can add methods to that class that only pertain to checking the puzzle's code. For example, one method could calculate the number of black pegs (given a guess as input), one the number of white pegs, and one whether or not the code was cracked (i.e. 4 black pegs). This encapsulates the knowledge about the code(-breaking) in one place and would allow for a relatively simple game loop that does not know anything about the puzzle internals and only concerns itself with gathering user input and forwarding it.