I'm getting some behavior with a container object's children I just don't understand.
I'm making four display objects children of an mx:Canvas object. When I call getChildren(), I see them all in order, right where I think they should be:
1
2
3
4
The fun begins when I call swapChildrenAt(0,1); that's supposed to swap the positions of 1 and 2, but instead I wind up with:
MYSTERY_OBJECT_OF_MYSTERY
2
3
4
So, where did 1 go? Why, it's at position -1, of course.
getChildAt(-1): 1
getChildAt(0): MYSTERY_OBJECT_OF_MYSTERY
getChildAt(1): 2
getChildAt(2): 3
getChildAt(3): 4
FWIW, MYSTERY_OBJECT_OF_MYSTERY is a 'border'. Don't know how it got there.
Regardless, I find it baffling that getChildAt() and swapChildrenAt() are apparently using different starting indexes. Can anybody shed some light on this behavior?
You appear to be swapping the index of the display Objects instead of passing the display Objects at those location themselves.
The official documentation says
"swapChildren(child1:DisplayObject, child2:DisplayObject):void" therefore the index of the Display Objects themselves cant be used.
I hope this solves your problem.
Related
I am building a work-logging app which starts by showing a list of projects that I can select, and then when one is selected you get a collection of other buttons, to log data related to that selected project.
I decided to have a selected_project : Maybe Int in my model (projects are keyed off an integer id), which gets filled with Just 2 if you select project 2, for example.
The buttons that appear when a project is selected send messages like AddMinutes 10 (i.e. log 10 minutes of work to the selected project).
Obviously the update function will receive one of these types of messages only if a project has been selected but I still have to keep checking that selected_project is a Just p.
Is there any way to avoid this?
One idea I had was to have the buttons send a message which contains the project id, such as AddMinutes 2 10 (i.e. log 10 minutes of work to project 2). To some extent this works, but I now get a duplication -- the Just 2 in the model.selected_project and the AddMinutes 2 ... message that the button emits.
Update
As Simon notes, the repeated check that model.selected_project is a Just p has its upside: the model stays relatively more decoupled from the UI. For example, there might be other UI ways to update the projects and you might not need to have first selected a project.
To avoid having to check the Maybe each time you need a function which puts you into a context wherein the value "wrapped" by the Maybe is available. That function is Maybe.map.
In your case, to handle the AddMinutes Int message you can simply call: Maybe.map (functionWhichAddsMinutes minutes) model.selected_project.
Clearly, there's a little bit more to it since you have to produce a model, but the point is you can use Maybe.map to perform an operation if the value is available in the Maybe. And to handle the Maybe.Nothing case, you can use Maybe.withDefault.
At the end of the day is this any better than using a case expression? Maybe, maybe not (pun intended).
Personally, I have used the technique of providing the ID along with the message and I was satisfied with the result.
I was trying to solve Google Code Jam problems and there is one of them that I don't understand. Here is the question (World Finals 2013 - problem C): https://code.google.com/codejam/contest/2437491/dashboard#s=p2&a=2
And here follows the problem analysis: https://code.google.com/codejam/contest/2437491/dashboard#s=a&a=2
I don't understand why we can use binary search. In order to use binary search the elements have to be sorted. In order words: for a given element e, we can't have any element less than e at its right side. But that is not the case in this problem. Let me give you an example:
Suppose we do what the analysis tells us to do: we start with a left bound angle of 90° and a right bound angle of 0°. Our first search will be at angle of 45°. Suppose we find that, for this angle, X < N. In this case, the analysis tells us to make our left bound 45°. At this point, we can have discarded a viable solution (at, let's say, 75°) and at the same time there can be no more solutions between 0° and 45°, leading us to say that there's no solution (wrongly).
I don't think Google's solution is wrong =P. But I can't figure out why we can use a binary search in this case. Anyone knows?
I don't understand why we can use binary search. In order to use
binary search the elements have to be sorted. In order words: for a
given element e, we can't have any element less than e at its right
side. But that is not the case in this problem.
A binary search works in this case because:
the values vary by at most 1
we only need to find one solution, not all of them
the first and last value straddle the desired value (X .. N .. 2N-X)
I don't quite follow your counter-example, but here's an example of a binary search on a sequence with the above constraints. Looking for 3:
1 2 1 1 2 3 2 3 4 5 4 4 3 3 4 5 4 4
[ ]
[ ]
[ ]
[ ]
*
I have read the problem and in the meantime thought about the solution. When I read the solution I have seen that they have mostly done the same as I would have, however, I did not thought about some minor optimizations they were using, as I was still digesting the task.
Solution:
Step1: They choose a median so that each of the line splits the set into half, therefore there will be two provinces having x mines, while the other two provinces will have N - x mines, respectively, because the two lines each split the set into half and
2 * x + 2 * (2 * N - x) = 2 * x + 4 * N - 2 * x = 4 * N.
If x = N, then we were lucky and accidentally found a solution.
Step2: They are taking advantage of the "fact" that no three lines are collinear. I believe they are wrong, as the task did not tell us this is the case and they have taken advantage of this "fact", because they assumed that the task is solvable, however, in the task they were clearly asking us to tell them if the task is impossible with the current input. I believe this part is smelly. However, the task is not necessarily solvable, not to mention the fact that there might be a solution even for the case when three mines are collinear.
Thus, somewhere in between X had to be exactly equal to N!
Not true either, as they have stated in the task that
You should output IMPOSSIBLE instead if there is no good placement of
borders.
Step 3: They are still using the "fact" described as un-true in the previous step.
So let us close the book and think ourselves. Their solution is not bad, but they assume something which is not necessarily true. I believe them that all their inputs contained mines corresponding to their assumption, but this is not necessarily the case, as the task did not clearly state this and I can easily create a solvable input having three collinear mines.
Their idea for median choice is correct, so we must follow this procedure, the problem gets more complicated if we do not do this step. Now, we could search for a solution by modifying the angle until we find a solution or reach the border of the period (this was my idea initially). However, we know which provinces have too much mines and which provinces do not have enough mines. Also, we know that the period is pi/2 or, in other terms 90 degrees, because if we move alpha by pi/2 into either positive (counter-clockwise) or negative (clockwise) direction, then we have the same problem, but each child gets a different province, which is irrelevant from our point of view, they will still be rivals, I guess, but this does not concern us.
Now, we try and see what happens if we rotate the lines by pi/4. We will see that some mines might have changed borders. We have either not reached a solution yet, or have gone too far and poor provinces became rich and rich provinces became poor. In either case we know in which half the solution should be, so we rotate back/forward by pi/8. Then, with the same logic, by pi/16, until we have found a solution or there is no solution.
Back to the question, we cannot arrive into the situation described by you, because if there was a valid solution at 75 degrees, then we would see that we have not rotated the lines enough by rotating only 45 degrees, because then based on the number of mines which have changed borders we would be able to determine the right angle-interval. Remember, that we have two rich provinces and two poor provinces. Each rich provinces have two poor bordering provinces and vice-versa. So, the poor provinces should gain mines and the rich provinces should lose mines. If, when rotating by 45 degrees we see that the poor provinces did not get enough mines, then we will choose to rotate more until we see they have gained enough mines. If they have gained too many mines, then we change direction.
I wish to use Senseval-2 Coarse Sense Dataset but there is description available for the same (about the format of the dataset).
It is supposed to have the decision data i.e. whether two senses should be merged or not. Is the middle value a confidence measure? Also, they used a prerelease of Wordnet 1.7. Can I use Wordnet 1.7 for the same?
A sample from the file looks like :
material%5:00:00:physical:00 3 material%5:00:00:worldly:00
material%3:00:03:: 3 material%5:00:00:worldly:00
material%3:00:04:: 2 material%3:00:01::
material%3:00:02::
post%5:00:00:succeeding(a):00
present%3:00:01::
present%3:00:02::
present%3:01:00::
stone%3:01:00::
stone%5:00:00:chromatic:00
air%1:15:00:: 4 air%1:27:00::
air%1:19:00:: 4 air%1:27:00::
air%1:27:01:: 4 air%1:27:00::
air%1:04:00::
air%1:10:02::
air%1:07:00::
air%1:10:01::
appeal%1:04:00:: 3 appeal%1:10:00::
appeal%1:10:02:: 3 appeal%1:10:00::
Through inspection, the middle number actually describes how many senses are in the same merged sense. For example:
matrial%5:00:00:physical:00 3 material%5:00:00:worldly:00
material%3:00:03:: 3 material%5:00:00:worldly:00
basically says that there are 3 senses which is considered the same as material%5:00:00:worldly:00, which are the two senses provided in the two lines, and the sense itself.
You can see also that there are no number for senses that do not get merged, such as air%1:04:00, and for the sense material%3:00:04:: 2 material$2:00:01:: you can see that there are two senses. So you can do the merging by mapping the senses in the first position into the sense in the second position.
I'm working on assignment 2 of CS193P - Stanford's IOS programming course. One thing I was wondering about is how the calculatorBrain is supposed to be able to accept and run a stored program, a program being an array or stack of operands and operations.
So let's say we want to perform the following calculation: 2, 3, 4, +, *
If you typed this into the calulator, the following would happen:
2 3 4 get pushed onto the stack one at a time, and runProgram called for each one, which simply pops the number off the stack and returns it's value which gets pushed onto the stack.
You press +, and runProgram pops this and sees it has to add the top 2 items which it does and pushes the result onto the stack which now contains 2, 7. You press * and the stack now contains 14.
But I can't see how you can pass an array containing (2, 3, 4, +, *) to the brain (the instructor says later you can just pass a program to the runProgram class method and get the result, without having to instantiate a brain object), as runProgram would first try to execute the top operand i.e. * and to do this it would take the next two objects off the stack and try to multiply them and to push the result back onto the stack. These 2 objects are "+" and "4" which won't work.
Now the instructor has been doing this a lot longer than I have, as I assume that I'm missing somethings, but I'm not sure what.
Any ideas?
If you look at this as passing "2,3,4,+,*" to the "brain", you need to be thinking in the context of the stack processor.
The arguments are evaluated in the order in which they are encountered in the array. But, don't confuse the array for the stack, they are different objects. The stack is internal to the calculator routine and the array of input is external to the routine. Since I'm not taking that particular class at whatever school you're in, I can't speak to the particulars of the language in use, but basically, think of the array "2,3,4,+,*" as input to the keyboard of the calculator. However, the calculator is a very simple machine and only processes one key press at a time.
Thus, when you process the array, you're basically passing each element of the array to the calculator for processing and the calculator is then deciding whether to push to the stack or execute the operator. These elements are passed in order, so the calculator receives: 2 followed by 3, followed by 4, followed by '+', followed by '*'.
It looks like you're trying to think of the problem in terms of the array being passed in to the calculator as the stack, and that's not what you want to do here.
I hope this is clear.
OK - the answer is that the way runProgram gets the next item off the stack is by recursively calling a popOperandOffStack (pOOS) method.
So when it gets passed a program consisting of 2 3 4 + *, it starts by popping * off the stack. It then has to pop the next two operands off the stack. So it calls pOOS, which firstly returns a '+', so it then calls pOOS again (twice) which gets '4' & '3' respectively, which are added to get 7 which is pushed back onto the stack (which now contains 2 7) and also returned as the result of calling pOOS. So when it called pOOS for the first operand of the '*' operation it didn't get '+', it actually got '7'. The second call to pOOS (for the second operand of the *) gets '2' which it then happily multiplies to get 14.
I did try looking up recursion in my IT dictionary, but it just said 'See recursion'.
#steve Ives, I think you nailed the answer with this last comment. Having gone through this assignment a few weeks back (also on my own), I found this site to be helpful understanding the reverse polish calculator, it basically emulates one. But as you are thinking recursion, your brain can go into overload mode. Hope this helps validating some of your scenario testing...Good luck.
HP12C Emulator
I am writing a game, which need a map, and I want to store the map. The first thing I can think of, is using a 2D-array. But the problem is what data should I store in the 2D-array. The player can tap different place to have different reaction. So, I am thinking store a 2D-array with objects, when player click some position, and I find it in the array, and use the object in that array to execute a cmd. But I have a concern that storing lots of object may use lots of memory. So, I am think storing char/int only. But it seems that not enough for me. I want to store the data like that:
{
Type:1
Color:Green
}
No matter what color is, if they are all type 1, the have same reactions in logic, but the visual effect is based on the color. So, it is not easy to store using a prue char/int data, unless I make something like this:
1-5 --> all type 1. 1=color green ,
2=color red, 3 = color yellow.... ...
6-10 --> all type 2. 2 = color green,
2 = color red ... ...
So, do you have any ideas on how to minimize the ram use, but also easy for me to read... ...thx
Go ahead and store a bunch of objects in the array, but with these two refinements:
Store a pointer to the object, not the object itself. (Objective C may handle this for you automatically; I don't know.)
Remember that a pointer to a single object can appear in more than one position in the array. All squares that share the same color and behavior can share an object.
It would also help if you did the math on the size of the array and the number of distinct squares so we can know exactly how much RAM you are talking about.