Arranging cards: 2 cards on top. 1 card on the bottom - screen-positioning

I have these three cards in a line across the bottom:
and I want them to be a triangle: 2 cards on top, 1 card on bottom. like this:
c
I tried "flex-wrap:wrap" already

Related

auto layout messing up my buttons. What I am doing wrong? one button is taking up more space than the other despite all constraints being equal?

I have two buttons that have the same width and height. One Button is on the top left of the screen, while the other is on the top right of the screen. Here are the constraints:
left Button(colored Black):
Height 105, width 225,
align top to safe Area with 30 points,
align leading to safe area with 0 points.
Right Button(colored Blue):
height 105, width 225,
align top safe are with 30 points,
align trailing to safe area with 0 points.
shouldn't they be the same exact size? why is the blue button taking up more space in smaller screens?blue button takes up more space in smaller screens
Devices have different view widths...
A few examples:
iPhone 14 Plus 428
iPhone 14 Pro Max. 430
iPhone 14 Pro 393
iPhone 14 390
iPhone SE 3rd gen. 375
iPhone 13 390
iPhone 13 mini 375
You are setting each button width to 225 ... 225 + 225 = 450
So, the buttons are the same size, but they are overlapping.
If you want two equal-width buttons, constrain them like this:
blackButton.top to safeArea.top + 30
blueButton.top to safeArea.top + 30
blackButton.leading to safeArea.leading
blueButton.trailing to safeArea.trailing
and, the key constraint:
blueButton.width equalTo blackButton.width

Convert diamond matrix 2d coordinates to 1d index and back

I have a 2d game board that expands as tiles are added to the board. Tiles can only be adjacent to existing tiles in the up, down, left and right positions.
So I thought a diamond spiral matrix would be the most efficient way to store the board, but I cannot find a way to convert the x,y coordinates to a 1d array index or the reverse operation.
like this layout
X -3 -2 -1 0 1 2 3
Y 3 13
2 24 5 14
1 23 12 1 6 15
0 22 11 4 0 2 7 16
-1 21 10 3 8 17
-2 20 9 18
-3 19
Tile 1 will always be at position 0, tile 2 will be at 1,2,3 or 4, tile 3 somewhere from 1 to 12 etc.
So I need an algorithm that goes from X,Y to an index and from an index back to the original X and Y.
Anyone know how to do this, or recommend another space filling algorithm that suits my needs. I'm probably going to use Java but would prefer something language neutral.
Thanks
As I can understand form the problem statement, there is no guarantee that the tiles will be filled evenly on the sides. for example:
X -3 -2 -1 0 1 2 3
Y 3 6
2 3 4 5
1 1
0 0 2
-1
So, I think a diamond matrix won't be the best choice.
I would suggest storing them in a hash-map, like implementing a dictionary for 2 letter words.
Also, You need to be more specific to what your requirements are. Like, do you prioritize space complexity over time? Or do you want a fast access time and don't care about memory usage that much.
IMPORTANT :
Also, what is the
Max number of tiles that we have to hold
Max width and height of the board.

How can I distribute weights knowing what's inside the box

I have this problem and I think that I'm gonna need a mathematical solution.
I have some boxes. Of these, I only know their total weight and what is inside each one. I have to calculate each one's weight.
For example I have:
Total weight: 100
Number of boxes: 5
Number of items: 14
Stock:
Type1: 2 items
Type2: 1 items
Type3: 7 items
Type4: 4 items
Box #1:
Type1: 2 items
Type2: 1 items
Box #2:
Type4: 3 items
Box #3:
Type3: 3 items
Box #4:
Type3: 2 items
Type4: 1 items
Box #5:
Type3: 2 items
Each box can potentially have n types of items, so how can I distribute the total weight?
I cannot divide the total weight by the number of boxes because the result would be equal for all boxes and this is not a real case.
You have:
Four variables - the weight of each item type
One linear equation 2A + B + 7C + 4D = 100 - what you know about the total weight.
Some linear inequalities - you know that A, B, C and D are all positive.
There's an infinite number of possible solutions. For example A=B=C=2,D=20 or A=B=C=4,D=15 and everything in between.

Sort a deck of cards [duplicate]

This question already has answers here:
Sorting a deque using limited operations?
(3 answers)
Closed 6 years ago.
Given a deck of N cards. You have to sort them using following permissible operations:
You can see top 2 cards.
You can swap them.
You can insert top at the bottom.
Any ideas?
This seems to be a simple case of bubblesort, a very inefficient sorting algorithm that is described by smaller elements bubbling to the top (assuming that you are sorting in ascending order). The modified algorithm I'm about to present is very similar to the original bubblesort algorithm, so I am going to quickly explain the original first. Bubblesort (for ascending order) works as follows,
The first element in a list is marked.
If the element to the right of the marked element is less than the marked element, then the two elements are swapped.
The marker moves to the right one spot regardless of the outcome of step two
Steps two and three are repeated until the marked element becomes the last element in the list. Just for clarification, this means that when step 3 causes the last element to be marked, the iteration is over and a new iteration starts.
The four steps above are repeated until an iteration occurs where the marker goes through each element in the list without a single swap occurring. Here's an example from wikipedia, https://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example
So let's modify bubblesort so that a couple things change to fit the deck of cards scenario. Let's not think of the deck of cards as a deck, but more as a list. Yes, the first card in the deck will constantly be changing with each iteration of our modified bubblesort but can we make it so that cards are shifting while still maintaining the position of the first card in the deck? This question is to key to solving the problem. What I am saying is that moving the cards to the bottom of the deck will not change in the initial order of the cards, only swapping will. For example, consider this deck of cards where leftmost is the top and rightmost is the bottom:
NOTE: (*) signifies marked card
*5 3 1 2 6
In the algorithm that will later be explained, moving 5 to the bottom of the deck will make the deck look like this
3 1 2 6 *5
Notice how 5 is now at the bottom of the deck, but the order is still preserved. The * symbol signifies the first card in the list/deck, so if read from left to right, starting from 5 and looping back to 3, the order is preserved.
Now to the algorithm, how do we use what I just said to make this modified version of bubblesort as similar to the original? Simple, use this marking mechanism to make it so that we're not really sorting a deck, but rather a list of numbers. Before starting the algorithm, mark the top card in the deck. Here are the rest of the steps for each iteration of bubblesort:
Compare the top card with the card below it. If the top card is greater, then swap with the card below. If the marked card was swapped, then unmark the previously marked card and mark the new card at the top of the deck.
Place the top card of the deck to the bottom.
Steps 1 and 2 are repeated until the marked card resurfaces to being the second card in the deck (card below the top card)
Put the top card to the bottom of the deck, making the marked card the top card in the deck.
These steps are repeated for each iteration of the algorithm until an iteration occurs where no swaps were made. Here is an example to showcase the modified bubblesort:
NOTE: (*) signifies marked card
Iteration One:
5 3 1 2 6 //Mark the first card in the deck before starting
*5 3 1 2 6 //Compare 5(top card) with 3(card below it)
3 *5 1 2 6 //5 > 3 so swap
*3 5 1 2 6 //Since the marked card (5) was swapped, 3 becomes the new marked
//card to preserve original order of the deck
5 1 2 6 *3 //Top card is placed at the bottom
1 5 2 6 *3 //5 > 1 so swap
5 2 6 *3 1 //Put 1 at the bottom
2 5 6 *3 1 //5 > 2 so swap
5 6 *3 1 2 //Put 2 at the bottom
5 6 *3 1 2 //5 < 6 so no swap
6 *3 1 2 5 //Put 5 at the bottom
*3 1 2 5 6 //Marked card is second to top card, so put 6 at the bottom
//Marked card is now at the top, so new iteration begins
Before going into iteration two, I would like to point out that if you ran the original bubblesort, the resulting sequence from one iteration would be the same as the resulting sequence from one iteration of our modified algorithm.
Iteration Two:
*3 1 2 5 6 //3 > 1, so swap
*1 3 2 5 6 //Remark accordingly since the former marked card was swapped
3 2 5 6 *1 //Place 1 at the bottom
2 3 5 6 *1 //3 > 2, so swap
3 5 6 *1 2 //Place 2 at the bottom
3 5 6 *1 2 //3 < 5 so no swap
5 6 *1 2 3 //Place 3 at the bottom
5 6 *1 2 3 //5 < 6 so no swap
6 *1 2 3 5 //Place 5 at the bottom.
*1 2 3 5 6 //Since marked card is second to top card, place 6 at the bottom and end iteration
Iteration Three:
*1 2 3 5 6 //1 < 2 so no swap
2 3 5 6 *1 //Place 1 at the bottom
3 5 6 *1 2 //2 < 3 so no swap and place 2 at the bottom
5 6 *1 2 3 //3 < 5 so no swap and place 3 at the bottom
6 *1 2 3 5 //5 < 6 so no swap and place 5 at the bottom
*1 2 3 5 6 //Since marked card is second to top card, place 6 at the bottom and end iteration.
We now know to end the algorithm because an entire iteration has occurred without any swaps occurring, so the deck is now sorted. As for runtime, the worst case occurs, in terms of swaps, when the max number of iterations occurs which is n (size of the deck) times. And for each iteration, the worst case number of swaps occurs, which is also n times. So the big O is n*n or O(n^2).

Optimal Movement in grid

There is a maze of size N*M, consisting of unit blocks. At the start Alice has K percentage of energy.
Now Alice start from 1 st row and move towards N th row. From the present block she can move to a block in the next row, which is either on right or on left side of the present block. On moving to a block in i th row j th column, her energy will reduce by C(i,j) percent if C(i,j) is greater then 0, else it will be recharged by C(i,j) percent.
For Example if she has 50 percent of energy, on moving to block with C(i,j) = 15, she will have 35 ( 50 -15 ) percent of the it remaining.
Now the task is to find out the status of the Alice energy in the end, if she moves optimally to save maximum energy.
Note : Her energy will not exceed more than 100 percent, and she will not move further if her energy goes down to 0 percent .
EXAMPLE : Let us suppose a grid of 4*4 as follow :
2 -2 2 -2
-2 2 -2 2
1 -1 1 -1
-1 1 -1 1
And if K= 10 meaning she has 10 percent energy at start. then after reaching 4th row she will be having 16 percent energy.One of the optimal move will be <1,2> -> <2,1> -> <3,2> -> <4,1>
So here answer is 16.
I originally said you could solve this from the bottom up, but doing the example by hand I am going to work from the top down, because of the constraint that you cannot proceed once your energy goes down to zero - it doesn't come into play here, but it looks much easier to deal with if you are working from the top down. The principal is much the same - work row by row and at each stage write down the best score possible travelling through each cell, using the answers from the previous row to work out the answers for the current row.
We start out with 10, and I assume you can start out anywhere you want, so just subtract the top row from 10 to work out the best you can get to in the top row, including the energy difference from the cell you are now on. The top row becomes 8, 12, 8, 12.
The edge cells on the next row can only be reached in one way. The inner cells can be reached by two ways. In either case we work out the total there by taking account of the energy difference from that cell and the energy you could have from the cell you came from, taking the most promising choice when there are two. So we get 14, 10, 14, 10, where for example the second 14 is one of the 12ths above and to its left or above and two its right (if the scores were different we could take the best) combined with the -2 for that cell.
Similarly, we have 9, 15, 9, 15 for the next row and 16, 8, 16, 8 for the bottom, where for example the only way to get to the bottom left is from the 15 above and to its right, so adjusted by -1 we turn 15 into 16. There are two ways to get to the cell just to its right, but they both start from, 9, so adjusted by 1 we get 8.
If you need to whole path you can keep track of how you entered each cell as you work out the best cost to it, and then track your way up from the best answer when you are finished.

Resources