I have been trying to program the 3x 7 trick. The full code is available here: https://codereview.stackexchange.com/questions/9419/programming-of-3-x-7-trick.
I am stuck at step 5.
I have this error when i type in the value for the row.
Not sure what the error is. Need some guidance.
step 6:
That error comes from the back_to_array function, where you have a typo in the condition of the inner loop. It should be j < numRows instead of i < numRows.
The main issue is the mistake with i < numRows instead of j < numRows.
As per requested, here are some other modifications you could and should implement:
Write printf("%8i", ... instead of printf("%i\t", ..., since the latter is likely to spread out the numbers unevenly.
Sanitize your input. Right now, you can make the program crash by inputting strange values. (Also, give the user a hint about whether to use the values 0, 1, 2 or 1, 2, 3.)
Right now, you're not shuffling row 0 and column 0. For instance, you start with column 6 and go through the columns one by one, but you stop as soon as you reach 0, before entering the loop again.
There's a problem where you quite often notice that the same numbers occur together on the same row. I believe, although I'm not completely sure, that the problem is that you're sorting the rows. The point of placing the selected row in the middle of the deck all the time, is to make the selected card move towards the center. If you sort the row, you allow the card to move away from the center. I commented out the sorting and couldn't notice the problem anymore. Is there a reason why you sort the rows?
Regarding design: Personally, I'd not display a shuffled array and wait for a keystroke before beginning. Instead, I'd write the instructions and immediately ask the player to enter a number. I always tried to enter a number already after the first array had been displayed. Very annoying. :)
Related
given the specific set of columns, I'm looking for a way to turn red cells into blank cells either within the COUNTIF process or afterward.
formula I use to get (partially) correct answer is:
=ARRAYFORMULA(IF(A2:A="",,IF(NOT(REGEXMATCH(TO_TEXT(B2:B), "\-")),
COUNTIFS(A2:A&D2:D, A2:A&D2:D, ROW(A2:A), "<="&ROW(A2:A)), )))
here is a copy of my sheet: - CC -
consider yellow cells as RESET/TERMINATION for ID
after each reset, ID gets upgraded (E column)
E column is just a helper for better visualization - it's not part of the dataset
A column is unsorted and it may be even ungrouped
When I make a copy of your sheet (the only option provided), the negative numbers in Col B are actually all text. So I didn't backtrack into the best way of doing this from the original data in Col A. Instead, I just worked with Cols A:D as you currently have them (though I suspect that you don't need the helpers) and with your original "close" formula as you wrote it.
That said, this should work to deliver your desired result as shown:
=ArrayFormula({"DESIRED OUTPUT";IF(A2:A="",,IF(VLOOKUP(A2:A&"~"&COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))-1,{SPLIT(UNIQUE(A2:A)&"~0|0","|");A2:A&"~"&COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A)),C2:C},2,FALSE)<>0,,IF(NOT(REGEXMATCH(TO_TEXT(B2:B), "\-")), COUNTIFS(A2:A&D2:D, A2:A&D2:D, ROW(A2:A), "<="&ROW(A2:A)), )))})
My contribution is this part:
IF(VLOOKUP(A2:A&"~"&COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))-1,{SPLIT(UNIQUE(A2:A)&"~0|0","|");A2:A&"~"&COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A)),C2:C},2,FALSE)<>0,, [most of your previous formula])
In plain English, this looks up the last occurrence of each ID. Only if that last occurrence had a 0 value in Col C will the results of your main formula be shown.
I stacked SPLIT(UNIQUE(A2:A)&"~0|0","|") on top of the actual ID~Row, Col C values so that the first occurrence of any ID will still find a 0 and will not result in an error. (Adding IFERROR would have unnecessarily lengthened the formula.)
NOTE 1: I assumed here that you original "close" formula works as you expect. I did not test it under close scrutiny. I just plugged it into my extended formula that determines where to place results.
NOTE 2: Normally, I don't get into complex formulas on these forums, for the sheer sake of time and the fact that I stay very busy. But you help out a ton on this forum, so I was happy to invest back into your own rare inquiry here.
I'm writing random numbers in an array using loops in Pure Data. For some reason in loop one Pure Data starts writing from index 1 instead of 0 and finishes on index 0 after the loop, this is not what I want. In loop two I found a solution using an extra [f] and [bang], Pure Data does the expected, starting from index 0. In both cases console prints the data in the same order (numbers are random, the order is the same), just the graph, reflecting the array, is different.
Does anyone know why the first case fails and the second works? My guess is that its related to the flow of data into the left and right inlets of [tabwrite].
loop one
Code
Console
Graph (1.151 is on index 1 instead of index 0)
loop two
Code
Console
Graph (1.742 is correct on index 0)
Message order.
You have a fanning output on the [f] object. Use a trigger [t b f] (short for [trigger bang float]) to make the order explicit. If you see an atom with multiple connections on one output, that's a red flag! Always use trigger objects, because without them the order in which you made the connections will be the order in which the messages will be sent. That's not visible in the patch, so it should be avoided!
Read more about message order in the documentation of Pd
There is another issue in your patch. Consider this case: If NotasPor0it is 8 and the counter is at 6, then NotasPor0it is changed to 4, the select object [sel] will never trigger the reset function of the counter idiom and continue to count... To avoid this issue, you could use a 'larger as' [>] symbol, or solve it with modulo (%) as seen in the screenshot.
I'm trying to read amplitude from a waveform and shine a green, yellow or red light depending on the amplitude of the signal. I'm fairly new to labVIEW and couldnt get my idea that wouldve worked with any other programming language I know to work. What I'm trying to do is take the value of the signal and for everytime it updates I'll store the value of the amplitude into an index of a large array. With each measurement being stored in the n+1 index of the array.
After a certain amount of data points I want to start over and replace values in the array (I use the formula node with the modulus for this). By keeping a finite amount of indexes to check for max value I restrict my amplitude check to a certain time period.
However my problem is that whenever I use the replace array subset to insert a new value into index n, all the other index points get erased. Rendering it pretty much useless. I was thinking its the Initialize array causing problems but I just cant seem to wrap my head around what to do here.
I tried creating just basic arrays in the front panel, but those either are control or indicator arrays and can't seem to be both written and read from, its either control (read but not write) or indicate(write but not read)?. Maybe its just not possible to do what I had in mind in an eloquent way in LabVIEW. If its not possible to do this with arrays in LabVIEW I will look for a different way to do it.
I'm pretty sure I got most of the rest of the code down except for an unfinished part here and there. Its just my issue with the arrays not working as I want them too.
I expected the array to retain its previously inputted data for index n-1 when index n is inputted. And only to be replaced once the index has come back to that specific point.
Instead its like a new array is initialized every time a new index is input.
download link for the VI
What you want to do:
Transport the content of the modified array into the next iteration of the WHILE loop.
What happens:
On each iteration, the content of the array is the same. It is the content of the initial array you created outside.
To solve this, right-click the orange square on the left border of the loop, and make it a "shift register". The symbol changes, and a similar symbol appears on the right border. Now, wire the modified array to the symbol on the right. What flows out into that symbol on the right, comes in from the left symbol on the next iteration.
Edit:
I have optimized your code a little. There is a modulo function, and an IF clause can handle ranges. ..3 means "values lower or equal 3". The next case is "Default", the next "7..". Unfortunately, this only works for integers. Otherwise, one would use nested IF clauses with the < comparator or similar.
I'm working on a math problem dealing with arrangements of Males and Females. There are 5 couples and we know that each couple sit adjacent to each other , every male is seated opposite to a female . If I am to represent male or female with either +1 or -1 and want to find the list of all possible outcomes and print them, how do I go about it?
Do I have to make use of 10 FOR loops? Is there a simpler way in which I could perhaps store the results in an array?
P.S. The math question isnt limited to what I have written here, I want to solve it by myself(sorry) but I would like to understand the issue I have highlighted above. If possible show me a small snippet to point me in the right way.
The most "efficient" or "simple" way to generate all ways to get 10 numbers containing 1 or -1 depends on the language. Python has a very simple and quick way. Here is an expression that creates a generator that does this. First execute the command
import itertools
to get what you need into your namespace, then the expression is
itertools.product((1, -1), repeat=10)
For example, to work on all those tuples of 10 numbers you could do:
import intertools
for mytuple in itertools.product((1, -1), repeat=10):
# Process mytuple
If you are not familiar with Python, each "tuple" that is created for a cycle of the loop is basically equivalent to an array.
Check out the Power Set to generate all possible subsets of a set.
There are many ways to generate this, but you stated that you want to figure it out.
This link from math.stackexchange.com seems relevant
If order doesn't matter, there are eleven solutions: one for each possible number of -1 in the output list. To list these, loop from 0 to 10 (inclusive) and then loop from 1 to 10 (inclusive), first printing a number of -1 equal to the value of the outer counter, then printing 1 until the end of the inner loop.
If order does matter, a simple recursive solution is to start with an empty buffer and position zero. Then, at each level, add -1 and 1, in turn, to the current position, recursively filling the remainer of the buffer by incrementing the position in each call. When the position is equal to the buffer length (hence the buffer is full) you can print the buffer's content and terminate the recursion. This is O(n2^n) time and O(n) space, where n is the buffer length. If stack overflow becomes a concern, consider rewriting this using iteration and a stack to explicitly manage the call stack yourself.
I'm iterating over a 3 dimensional array (which is an image with 3 values for each pixel) to apply a 3x3 filter to each pixel as follows:
//For each value on the image
for (i=0;i<3*width*height;i++){
//For each filter value
for (j=0;j<9;j++){
if (notOutsideEdgesCondition){
*(**(outArray)+i)+= *(**(pixelArray)+i-1+(j%3)) * (*(filter+j));
}
}
}
I'm using pointer arithmetic because if I used array notation I'd have 4 loops and I'm trying to have the least possible number of loops. My problem is my notOutsideEdgesCondition is getting quite out of hands because I have to consider 8 border cases. I have the following handled conditions
Left Column: ((i%width)==0) &&
(j%3==0)
Right Column: ((i-1)%width ==0) && (i>1) &&
(j%3==2)
Upper Row: (i<width) &&
(j<2)
Lower Row:
(i>(width*height-width)) && (j>5)
and still have to consider the 4 corner cases which will have longer expressions. At this point I've stopped and asked myself if this is the best way to go because If I have a 5 line long conditional evaluation it'll not only be truly painful to debug but will slow the inner loop. That's why I come to you to ask if there's a known algorithm to handle this cases or if there's a better approach for my problem. Thanks a lot.
Yes, there's a much better way. Write a fast loop to handle the cases where there is guaranteed to be no boundary problems. This will consist of the region from the second to the next-to-last columns and the second to next-to-last rows. Then you can write four routines to handle each of the sides (row 0, column 0, row N and column N) and you can hand code the last four points.
That said, there are also a heck of a lot faster ways of doing the addressing calculations you're doing.
A nice tip is to add an additional row at the top of the array, and another at the end (do the same for the columns).
These additionals rows/columns won't contain any information but they will ease the computation (no border cases). At the price of consuming more memory...
Just an idea :)