Find nearest time out of five [closed] - objective-c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I currently have an array with different times. These are user defined and in 30 minute intervals. So, an example could be:
NSMutableArray *times = [[NSMutableArray alloc] initWithObjects: #"9:30", #"12:00", #"13:30", #"17:00", nil];
Also, the number of different times can be vary from one to five.
Now I want to find the one that's the closest to the current time. So for instance, if it was 11:00 right now, out of the above examples, it would return 12:00.
What is the best way to do this?

Loop over the array of times, comparing each one to the current time. Store the closest one in a variable outside the loop, checking each time against that variable and updating its value only if the iterator value is closer. I'm not posting code on purpose, because this is the kind of thing you need to learn to figure out to be a developer.

Storing String cannot help you on comparing "time".
Try storing the "times" in NSTimeInterval.
Then you should be able to compare and select the most nearest time to current time.
EDIT:
The way on how to convert to NSTimeInterval and how to compare should leave back to you to implement.

Related

Query time for a specific entity is 10000 times higher [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
We run into a problem: select for a filter by a certain id takes a very long time. For all id about 5ms, for this - 10 seconds.
This is explain. Left - normal, right - long. This is absolutely the same sql query, where the difference is only in one digit 'where id = ...'
this
It is striking that a filter is used on the right, but for some reason it is not on the left, as well as some huge number of 'rows removed'. Such a number can only be obtained by multiplying the number of rows in the joined tables. Once again I repeat that the sql query is absolutely the same except for the entity id, the number of retrieved data for entities is comparable.
One of the tables also uses btre index. The only thing that this id has is special - it comes after the numbering break, 22,23,24,30 for example. But I was not able to reproduce the problem on this principle.
Unfortunately, I cannot show the code, but I hope that this information will be enough to advise something.
upd:
I found the reason. Postgres for some reason expects that one of the tables will return only 1 structure, when as a real return in 10k+ and therefore chooses the wrong algorithm. For other entity ids, it "thinks" correctly and chooses higher algorithms. Can you find how posgres counts plan lines? What could be the problem?
If I understand correctly, your problem is data histogram. We cannot support you because you cannot provide example code. Briefly, one of your table has a data whose id columns has heterogenous data in it. For example; your table has 1 billion records and in that table each id has 500 records. Yet, some of the id' s (virtually, let say) 20 or 200 millions records. So, if you search for these highly non-selective rows the database optimizer will not help you.
Check your data histogram!

What is the best way to hold multiple 3D arrays in SQL? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
A program I am working on currently builds multiple differently sized 3D arrays (50x50x200, 30x30x100 for example).
I need to store these arrays in SQL, so I can pull them out again and display the arrays. I have come across this thread where it describes the best way to store a 3D array. I will be using that data structure.
Another thing to mention would be that many of the same sized arrays will be created e.g. 10 50x50x200. My problem is how to go about storing these arrays. I have given this a lot of thought, and I have subsequently come to a few conclusions:
1 table per array indexed with an ID (x,y,z,ID)
1 table per array size e.g. 50x50 table, 30x30 table etc with a 4D array inside (w,x,y,z,ID)
1 enormous table with a 5D array inside (w,x,y,z,size,ID) - size is whether the array is for example 30x30
What would be the best way to go about storing these arrays?
Edit:
All elements within an array will be retrieved at once;
There is no need for updates within these arrays - once they are saved in SQL they will not change;
After the original insertion, similar to updates, the arrays will not change;
The best way depends on how you are going to manipulate them. If you simply need to store the array in SQL and do no manipulation, then you are storing an object.
You can store the entire object as a varbinary or varchar() using a single column. To the database, they would look like a bunch of bits, which is fine for storing and retrieving.
If you were going to do any actions on the array, then you would want a more native solution. That would probably involve two tables, an arrays table to define each array and an arrayElements table with one row per element in the array, so five columns:
Array id.
x, y, z coordinates.
element value.

How to make correct function in dynamic programming [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the following problem in dynamic programming.
A person has time machine and he can move in time either 1 year or 2. At the beginning he is at year 0 and he wants to reach year 100. Every step he does (1 or 2 years) he is paying some fixed fees. There is an array with 100 integers represents the fee he needs to pay if he went threw the specific year.
I need to find the minimum amount the person can pay to go from year 0 to year 100 using dynamic programming.
From what i have done so far i think that there should be something like
minCost(i) = min{A[i-1], A[i-2]}
and the base cases are years 1 and 2 which costs A[1], A[2] respectively. But i think this approach has more of greedy algorithm rather than dynamic programming.
I saw the bin packing algorithm of dynamic programming and i understood it and the matrix that represents it.
How should the matrix of the shown problem above look like?
And how should i build the function and the pseudo code for this problem?
You are almost there.
Think about how will you reach the i th year from i-1 th year and i-2 th year. There is a fee which you are forgetting to take into consideration.
MinCostToReachYear(i) = min( MinCostToReachYear(i-1) + fee(i-1), MinCostToReachYear(i-2) + fee(i-2) )
You already know the base cases year 1 and year 2. Can you think of extrapolating with the use of a for loop or more easily with a recursive function which you already know as mentioned above? I leave it as an exercise for you.

What are typical lengths of chat message and comment in database? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I need to create a column in SQL Server database. Entries for that column will contain messages from chat. Previously such messages has been stored as comments.
My main quetion is:
What is typical text length for chat message and comment?
By the way:
What would happen if I used varchar(max)? How would it impact database size and performance? Is better to use powers of 2 or powers of 10 (e.g. 128 instead of 100) while considering text lengths?
Using VARCHAR(MAX) has a disadvantage: you can not define an index over this column.
Generally, your application should impose a maximum length for a chat message. How big that limit is depends very much on what the application is used for. But anything more than 1000 byte is probably less a legitimate message but an attempt to disrupt your service.
If your maximum value is a power of 2, or a power of ten or any other value has no influence on the performance as long as the row fits in one (8KB) page.
Short answer - it doesn't matter.
From MSDN:
The storage size is the actual length of the data entered + 2 bytes.
So VARCHAR(10) and VARCHAR(10000) will consume the same amount of data if the values don't exceed 10 characters.
Definitely use N/VARCHAR(MAX), it can grow to be 2GB (if I remember correctly). It will grow as required though, so it is very efficient with regards to space unless you are only storing very small amounts of data.

Randomly Generating Enemy Stats Depending on Game State [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In my game, I want that, the more the player is playing a game, the harder are the enemies to kill by generating them randomly, like the game "Dungeon Raid" on iOS. Here's the stat that the enemies should have:
HP
Attack
Time: Seconds needed for the enemy before attacking (The higher the time is, the slower is the enemy.)
The time varies depending on the attack amount (If the enemy is really strong, it must be slow)
Score: The score given by the enemy when killed
What I thought could be right to do is to determine the minimum stats that an enemy could get and then increment it depending on the game state and by using an algorithm to calculate them accordingly, but I really don't know how to do it and even where to start.
You probably want to start by being able to generate random numbers over a specific range. The simplest case is to generate random numbers uniformly between a minimum and a maximum value. As the game progresses you can change the minimum and maximum values to increase along with the stats. of your player.
You could do this like so,
NSUInteger BoundedUniformRandomNumber( NSUInteger min, NSUInteger max)
{
NSUInteger range = max - min;
u_int32_t randomNumber = (u_int32_t)min + arc4random_uniform((u_int32_t)range);
return (NSUInteger)randomNumber;
}
Note that arc4random_uniform is from stdlib so please include #import <stdlib.h>, also the arc4random_uniform is half-open, which means that it will return the minimum value, but will never return the maximum value. Therefore the maximum value you will generate in the above function is actually max - 1.
Hopefully that gets you started.