I have a nxn grid of randomly generated numbers. I have a label that displays the element numbers for the X and Y axis:
It aligns up properly for single digit numbers, but when the grid size increases the labels become out of proportion and don't line up like so:
I'm wondering if there is any way to make the label line up with the numbers within the grid and scales depending on the size. Is this even possible?
You can use this to determine the number of digits in an integer:
int getDigits(int col)
{
int len = 1;
while ( col/= 10 )
{
len++;
}
return len;
}
With this you can determine the number of " " to print as you loop through.
std::string space(getDigits(column), ' ');
std::cout<<space<<num;
You could use two rows for the top numbering and work out a simple calculation to determine whether or not to display the digit for that row.
For instance, the top row could be for the 'tens' column and the second row for the units. You know you have 20 columns, so you could display a space character or zero if the current count of a loop is less than 10. If it's not less than 10, display a 1 character.
The bottom row could then just cycle through 0 - 9 twice.
Related
Here we have a box that is 4 * 7 and it can be filled with rectangles that are either 1 * 2 or 2 * 1. This depiction is from the book Competitive Programmer's Handbook.
To solve this problem most efficiently, the book mentions using the parts that can be in a particular row:
Since there are 4 things in this set, the maximum unique rows we can have is 4^m, where m is the number of columns. From each constructed row, we construct the next row such that it is valid. Valid means we cannot have vertical fragments out of order. Only if all vertical "caps" in the top row correspond to vertical "cups" in the bottom row and vice versa is the solution valid. (Obviously for the horizontal fragments, their construction is restricted in row creation itself, so it is not possible for there to be inter-row discrepancy here.)
The book then says this:
Since a row consists of m characters and there are four choices for
each character, the number of distinct rows is at most 4^m. Thus, the
time complexity of the solution is O(n4^{2m}) because we can go through
the O(4^m) possible states for each row, and for each state, there are
O(4^m) possible states for the previous row.
Everything is fine until the last phrase, "there are O(4^m) possible states for the previous row." Why do we only consider the previous row? There are more rows, and this time complexity should consider the entire problem, not just the previous row, right?
Here is my ad hoc C++ implementation for 2 by n matrix, which would not work in this case, but I was trying to abstract it:
int ways[251];
int f(int n){
if (ways[n] != 1) return ways[n];
return (ways[n] = f(n-1) + f(n-2));
}
int main(){
ways[0] = 1;
ways[1] = 1;
for (int i = 2; i <= 250; i++){
ways[i] = -1;
cout << f(250) << '\n';
}
}
Here we have a box that is 4 * 7 and it can be filled with rectangles that are either 1 * 2 or 2 * 1. This depiction is from the book Competitive Programmer's Handbook.
To solve this problem most efficiently, the book mentions using the parts that can be in a particular row:
Since there are 4 things in this set, the maximum unique rows we can have is 4^m, where m is the number of columns. From each constructed row, we construct the next row such that it is valid. Valid means we cannot have vertical fragments out of order. Only if all vertical "caps" in the top row correspond to vertical "cups" in the bottom row and vice versa is the solution valid. (Obviously for the horizontal fragments, their construction is restricted in row creation itself, so it is not possible for there to be inter-row discrepancy here.)
The book then mysteriously says this:
It is possible to make the solution more efficient by using a more
compact representation for the rows. It turns out that it is
sufficient to know which columns of the previous row contain the upper
square of a vertical tile. Thus, we can represent a row using only
characters upper square of a vertical tile and □, where □ is a combination of characters lower vertical square, left horizontal square and right horizontal square.
Using this representation, there are only 2^m distinct rows and the
time complexity is O(n2^(2m)).
Why does this simple square work? How would you know if there is a horizontal box underneath the top vertical fragment? How would you know left and right horizontal fragments are aligned? It breaks my mind why this is possible. Does anyone know?
Here is my ad hoc C++ implementation for 2 by n matrix, which would not work in this case, but I was trying to abstract it:
int ways[251];
int f(int n){
if (ways[n] != 1) return ways[n];
return (ways[n] = f(n-1) + f(n-2));
}
int main(){
ways[0] = 1;
ways[1] = 1;
for (int i = 2; i <= 250; i++){
ways[i] = -1;
cout << f(250) << '\n';
}
}
I want to layout X buttons.
At the start, Y items can be in a row.
After the first row is laid out, only Y - 1 items can appear in the next row and so on.
So say I have 13 buttons and the first row can have up to 6 buttons, I would need 3 rows. The first would have 6 buttons the second 5 buttons, and the 3ed 2 buttons.
Thanks
What algorithm could be to do:
int getRowCount(int startCols, int numItems);
I know how to do it with MOD if the number of columns is constant but how would you do it if the maximum number of columns decreases with each row?
In situations like this, I try to translate the english into code.
int getRowCount(int startCols, int numItems) {
int currentCols = startCols;
int numRows = 0;
while (numItems > 0) { // as long as items remain
numRows += 1; // add another row
numItems -= currentCols; // reduce the remaining items by the current number of columns
currentCols--; // reduce the number of columns by one
}
}
It's always best to run through the scenario with some edge cases. Ask yourself questions like:
What answer do I get if numItems is 0?
What answer do I get if startCols is 0?
What answer do I get if numItems == startCols?
How do you find number of items in a column inside a grid?
I have a grid (listview control to be specific), and have some items.
Some times a given row might not be full. ANd can have values in fewer than maximum columns. I need to find Number of items in a given Column.
If the grid is like
1 2 3
4 5 6
7
and if input column is 1, then we need to output 3, and 2 for input of 2 or 3.
I have variables to for ItemCount, CoulmnCount and RowCount which track number of items, rows and columns.
A very rudimentar way would be something like this:
int iItemCount=0,iItemInColumn=0;
for(int iCol=0;iCol<iColumnCount;iCol++)
for(int iRow=0;iRow<iRowCount;iRow++,iItemCount++)
if(iCol==iInputCol && iItemCount<iTotalItems)
iItemInColumn++;
Can you guys think of any sophesticated way, which does not need loops? possible utilizing just 3 variables which I already have for tracking?
Assuming 0-based indexes:
def itemsInColumn(itemCount, columnCount, inputColumn):
lastItemColumn = (itemCount - 1) % columnCount
if inputColumn <= lastItemColumn:
return (itemCount + columnCount - 1) / columnCount
else:
return itemCount / columnCount
It depends on the total number of items (itemCount) and the number of columns (columnCount). It just computes itemCount / columnCount, and rounds up or down depending on whether the input column is less than or equal to the last item's column.
The computation "(itemCount + columnCount - 1) / columnCount" is just a trick for rounding up using integer division. In general, given positive integers a and b: ceil(a / b) = (a + b - 1) div b, where div is integer division.
Is there a smart algorithm that takes a number of probabilities and generates the corresponding truth table inside a multi-dimensional array or container
Ex :
n = 3
N : [0 0 0
0 0 1
0 1 0
...
1 1 1]
I can do it with for loops and Ifs , but I know my way will be slow and time consuming . So , I am asking If there is an advanced feature that I can use to do that as efficient as possible ?
If we're allowed to fill the table with all zeroes to start, it should be possible to then perform exactly 2^n - 1 fills to set the 1 bits we desire. This may not be faster than writing a manual loop though, it's totally unprofiled.
EDIT:
The line std::vector<std::vector<int> > output(n, std::vector<int>(1 << n)); declares a vector of vectors. The outer vector is length n, and the inner one is 2^n (the number of truth results for n inputs) but I do the power calculation by using left shift so the compiler can insert a constant rather than a call to, say, pow. In the case where n=3 we wind up with a 3x8 vector. I organize it in this way (rather than the customary 8x3 with row as the first index) because we're going to take advantage of a column-based pattern in the output data. Using the vector constructors in this way also ensures that each element of the vector of vectors is initialized to 0. Thus we only have to worry about setting the values we want to 1 and leave the rest alone.
The second set of nested for loops is just used to print out the resulting data when it's done, nothing special there.
The first set of for loops implements the real algorithm. We're taking advantage of a column-based pattern in the output data here. For a given truth table, the left-most column will have two pieces: The first half is all 0 and the second half is all 1. Since we pre-filled zeroes, a single fill of half the column height starting halfway down will apply all the 1s we need. The second column will have rows 1/4th 0, 1/4th 1, 1/4th 0, 1/4th 1. Thus two fills will apply all the 1s we need. We repeat this until we get to the rightmost column in which case every other row is 0 or 1.
We start out saying "I need to fill half the rows at once" (unsigned num_to_fill = 1U << (n - 1);). Then we loop over each column. The first column starts at the position to fill, and fills that many rows with 1. Then we increment the row and reduce the fill size by half (now we're filling 1/4th of the rows at once, but we then skip blank rows and fill a second time) for the next column.
For example:
#include <iostream>
#include <vector>
int main()
{
const unsigned n = 3;
std::vector<std::vector<int> > output(n, std::vector<int>(1 << n));
unsigned num_to_fill = 1U << (n - 1);
for(unsigned col = 0; col < n; ++col, num_to_fill >>= 1U)
{
for(unsigned row = num_to_fill; row < (1U << n); row += (num_to_fill * 2))
{
std::fill_n(&output[col][row], num_to_fill, 1);
}
}
// These loops just print out the results, nothing more.
for(unsigned x = 0; x < (1 << n); ++x)
{
for(unsigned y = 0; y < n; ++y)
{
std::cout << output[y][x] << " ";
}
std::cout << std::endl;
}
return 0;
}
You can split his problem into two sections by noticing each of the rows in the matrix represents an n bit binary number where n is the number of probabilities[sic].
so you need to:
iterate over all n bit numbers
convert each number into a row of your 2d container
edit:
if you are only worried about runtime then for constant n you could always precompute the table, but it think you are stuck with O(2^n) complexity for when it is computed
You want to write the numbers from 0 to 2^N - 1 in binary numeral system. There is nothing smart in it. You just have to populate every cell of the two dimensional array. You cannot do it faster than that.
You can do it without iterating directly over the numbers. Notice that the rightmost column is repeating 0 1, then the next column is repeating 0 0 1 1, then the next one 0 0 0 0 1 1 1 1 and so on. Every column is repeating 2^columnIndex(starting from 0) zeros and then ones.
To elaborate on jk's answer...
If you have n boolean values ("probabilities"?), then you need to
create a truth table array that's n by 2^n
loop i from 0 to (2^n-1)
inside that loop, loop j from 0 to n-1
inside THAT loop, set truthTable[i][j] = jth bit of i (e.g. (i >> j) & 1)
Karnaugh map or Quine-McCluskey
http://en.wikipedia.org/wiki/Karnaugh_map
http://en.wikipedia.org/wiki/Quine%E2%80%93McCluskey_algorithm
That should head you in the right direction for minimizing the resulting truth table.