My question is organizing a one-dimensional list of numbers into a two-dimensional table with a variable number of columns to the right.
The lengths of the column should be balanced so that all rows except the last are filled. The empty space in the last row should be at the right end of the row.
EXAMPLE:
List: 1,5,1,3,6,2,8,7,4,9,5,6,25,4,8,5,63
COLUMNS: 5
TABLE: 1 6 4 6 8
5 2 9 25 5
1 8 5 4 63
3 7
I have to write a program (using PHP) to read a collection of records (<100), each with a positive integer in columns 1-5. A zero value terminates the list. A second set of records should be read next, with various positive integers values for C, the number of columns, in columns 1-5. After each value of C is read, the sum of the elements in each row of the table should be computed and printed. Sums will be less than 9 digits. The two-dimensional table itself should not be printed.
Using the sample data shown above for various values of C, output similar to the following should be produced:
For example:
C ROW SUM
5 25 41 81 10
***************************
1 153
***************************
Either vertical or horizontal printing of row sums is acceptable, but a line of asterisks should separate output generated for different values of C. A zero value for C should cause the program to terminate.
Related
I'm implementing a sudoku solver using human way algorithm. Which have 3 constraint, different number ini row, cell and box.
I googled and I got http://www.emanueleferonato.com/2008/12/09/sudoku-creatorsolver-with-php/. But I cannot understand how this guy get floor($cell / 9) for return_row function or floor(return_row($cell) / 3) * 3 + floor(return_col($cell) / 3) for return_block.
I try to figure it out by write down the data in excel and I know there is some pattern like this :
[cell] [column]
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 0
But how did he figure it out that the formula was $cell % 9 ?
I want to know, if I don't know the answer for the formula, how can I calculate that ? How can I determine that formula ? What method should I use?
Thanks
This comes from the way the cells are counted, which we could call row-major.
You can see block and cell numbers with their respective row and column numbers on this image :
rows and columns
The first row (0) contains cells 0 to 8, the second row cells 9 to 17, and so on until row 8, which contains cells 72 to 80.
If you number rows 0 to 8 and columns 0 to 8 as well, we can see that the formula for a cell that corresponds to this numbering is then cell = 9 * row + col, which should explain the formulas for get_row and get_col.
When moving right from any cell by one column, you add 1 to the cell count, which means the formula for the cell number looks like something + col.
When moving down one row, you add to the cell number the amount of cells per row, which here is 9, so the formula also looks like 9 * row + something.
Putting those together, you get a formula that is 9 * row + col + offset : the "+ something"s dependencies is row and col are determined, but maybe they still contain a constant value.
In our case, the formula gives the numbering we want with offset=0, but if you started numbering from 1 you formula would be 9 * row + col + 1.
However you do not have to do this reasoning every time. Just now that when you have a rectangle where you count items row by row, the formula for an item's number is always row * row_size + col + number at (0,0). This is also how contiguous memory is allocated for double arrays in C, for example, a very common pattern. If you count column by column, then you have col * col_size + row + number at (0,0)
Blocks
Now blocks are numbered the same way, but there are only 3 rows and columns. You can replace one by one the elements in the get_block formula to understand it : floor(row / 3) * 3 + floor(col / 3)
Since there are 3 rows of blocks but 9 of cells, the (cell-)rows 0, 1 and 2 correspond to the first row of blocks, 3 to 5 to the second row of blocks and 6 to 8 to the final and third row of blocks. What we get out of this is that a row of blocks rb contains the rows of cells 3 * rb, 3 * rb +1 and 3 * rb + 2. The opposite operation is dividing by 3 and flooring, which gets you rb for any of the expressions above.
This works exactly the same for the columns.
Thus when replacing in the expression, we now have : block_row * 3 + block_col. This I'd exactly the same formula (with 3 instead of 9) than we had for the numbering of cells, and thus gets you the number of the block from its row and column.
I already searched but I always find LEAST and GREATEST as hints. I want to have the next ascending number in a row that's not used. Like the following:
entries
1
2
3
5
6
7
If every of the numbers is for one row in my table I want the number 4 as a result and in the following example:
1
2
3
4
5
6
I want the number 7 as a result. Is there any possiblity to accomplish this in an SQL statement?
Best,
Robin
This query assumes that the number 1 is in your table
select min(number) + 1 from entries e1
where not exists (
select 1 from entries e2
where e2.number = e1.number + 1
)
If you want all missing numbers (where gaps are no larger than 1) instead of the smallest one, then remove min()
It think the solution is to do a self-join with the next value, and extract the first lowest result. Example:
Table: values, with column value
SELECT v1.value
FROM values v1
LEFT JOIN values v2 ON v1.value = (v2.value + 1)
WHERE v2.value IS NULL
ORDER BY v1.value ASC
LIMIT 1
Given a table like the one below, what would be the best way to detect the two columns separately?
So what I would need the total colspans for the first column.
What is important to remember is that the nr of columns can change.
In the case of this example, the second column starts at "10 euro" (second row). The first section is equal to 2 colspans. The other section is 5 colspans.
Any (abstract) ideas on how to do this?
You must consider the gaps in between the table cells and mark their positions, like this::
0 1 2 3 4 7
0 2 3 4 5 6 7
0 1 2 4 5 7
...
0 2 7
Once you have built an array with above information, you iterate over them and mark the common gap locations:
0 2 7
Since 0 and 7 are both at the edges of your table, you can strip those off. Then you're left with position 2 as the common gap between your rows.
Done :)
I have a list of numbers between 1-20,000. I wish to insert all of these numbers randomly into a table and only once. How can I accomplish this?
For example with 1-10, they would be inserted in the below order, not 1, 2 , 3, 4
9
3
5
1
2
4
6
..etc
Use shuffle()
$arr = <numbers 1-20K>
shuffle($arr);
<code to insert into sql>
you can use Knuths or Floyds algorithm to achieve this and then store all the numbers in the database. Read this SO question for both the implementation:
Unique random numbers in an integer array in the C programming language
One tip, make sure you dont save into the database for every number generated, you should batch your inserts.
I have multiple tables/content types searched for a keyword and a fixed number of "result slots" for the autocomplete in the UI.
Let's assume there are 4 tables (persons,pages,articles,places) and 12 result slots. When a search returns 3 or more hits in each table, 3 results are displayed for each table.
I need an algorithm (preferably PHP) that increases the number of slots for a table when there are less than three results in the others. It should "fill up" the slots with results from the other tables as long as there are slots (and of course results) left
e.g.
person: 6
pages: 3
articles:2
places: 1
thanks!
Interesting question.
Lets say you have 4 categories A,B,C,D in the order of priority.
Fetch the number of rows of A,B,C,D
The function min(3,X) returns the smaller of 3 and X. Now do your initial allocation of slots by
Alloc_A=min(3,A)
Alloc_B=min(3,B)
Alloc_C=min(3,C)
Alloc_D=min(3,D)
The remaining slots are then:
Rem_A=A-Alloc_A
and so on.
The number of free slots are then:
free_slots=12-Alloc_A-Alloc_B-Alloc_C-Alloc_D
As for filling in the remaining slots, you can do it in proportion to the number of remainaing articles. We can allocate in proportion by
Alloc_A+=round(Rem_A/(Rem_A+Rem_B+Rem_C+Rem_D))
Alloc_B+=round(Rem_B/(Rem_A+Rem_B+Rem_C+Rem_D))
and so on. For example if there are 4 free slots and there are 9 in B and 3 in D,This will allocate 3/4 slots to B and 1 to D. But this can get unfair if, say b is 10 times as large as D. You can cap the others as, say 3x the smallest one.