PHP Updating Array of Appointments Slots [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a list of appointments for a day. There are 10 available appointments 15 minutes apart (I'm referring to each 15 minute appointment as a slot) and the list might look like this:
1 Unavailable
2 Unavailable
3 Available
4 Available
5 Available
6 Available
7 Available
8 Available
9 Unavailable
10 Unavailable
There are certain types of appointments that require a minimum number of slots. For example if the appointment type is "long" it needs 3 consecutive slots. So in the above example the possible combinations could be
3 Available
4 Available
5 Available
or
4 Available
5 Available
6 Available
or
5 Available
6 Available
7 Available
or
6 Available
7 Available
8 Available
I then need to update the list to make 7 and 8 unavailable:
7 Unavailable
8 Unavailable
as the appointment cannot start at these times as there are not 3 consecutive available slots starting at these times.
I'm completely stumped as to the logic/rule I can use to implement here? I'm hoping there is something abstract that I can use and just feed in the number of slots required.

This is a simple textbook exercise of a function within a function.
Your first function needs to scan for the first available slot and then passes on the context to the second function that will look ahead from the current position and make sure the next 1/2/3/4/etc slots are available.
If available, the second function returns true, the first function just takes into account that it needs 1/2/3/4/5/etc slots so one possible choice is to use:
3/4/5
4/5/6
5/6/7
6/7/8
Note: You can use 1 single function with two imbricated loops and an $i, $k index. I prefer two functions for the sake of simplify and readability!

Related

How to speed up these R computations? [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 5 years ago.
Improve this question
I have to data frames with X, Y, and Z coordinates. I want to find the distance between all of the points in the two data frames. (Like the distance between entry A1 and every entry in B, A2 and every entry in B, and so on, and vice versa). I basically did this:
1.) Wrote a function that calculates the distance between two points.
2.) Used the distanceFinder function to create a function that finds the distance between one point in a group, and every other point in the opposite group.
3.) Created a function called bigDistance() that calls filter() on every entry in one group, and appends the results to an empty data frame through a for loop until it's completed.
This code takes about 2 minutes to run on the file I'm experimenting with, and I just found out that I have to translate this algorithm to PHP... so I guess this is kind of an optimization question, because I feel like PHP would be way slower at making these computations than R? Sorry if people find this "off-topic" but yeah, super new to programming and Big O notation and stuff, so any tips would be amazing! Thanks!
The dist function does exactly what you are looking for.
myDf <- data.frame(
x = rnorm(8),
y = rnorm(8),
z = rnorm(8)
)
dist(myDf)
# 1 2 3 4 5 6 7
# 2 3.0457054
# 3 1.7260658 3.2107845
# 4 1.2839101 3.4596211 2.9451175
# 5 1.5656231 4.0154389 2.3421445 2.3612348
# 6 1.9294650 1.6655718 1.7977887 2.8726174 2.5815296
# 7 2.1842743 3.5274692 3.8552701 1.0984651 2.9951244 3.3220919
# 8 1.4795857 3.5364663 0.5567753 2.7033371 1.9226225 2.0631788 3.6624082
It seems to be pretty fast as well (73ms on average)
library(microbenchmark)
mb <- microbenchmark(dist(myDf))
mb
# Unit: microseconds
# expr min lq mean median uq max neval
# dist(myDf) 70.436 71.453 77.4083 72.978 82.133 172.911 100
autoplot(mb)

Randomly update columns with mysql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm building a browser game which you can shoot missiles on factories.
This is example of a similar SQL table:
user_ID iron steel metal gold
1 21 30 39 25
2 7 10 25 50
I need each missile to destroy 1 factory.
For example I shoot 10 missile, so 10 factories are destroyed. But I need it to have completely randomly and even.
The problem is if according to this SQL table, if I shoot for example 40 missile on user_ID '2'.
In best case I destroy 10 factories of each kind. But how can I do it when I don't know if there is enough of each kind?
First, divide the number of missiles by the number of factory types that have at least 1 factory. If all the types have at least this many factories, subtract this from each column.
If any of the types have less than this, they'll be totally wiped out. Reduce them to 0 and subtract their original totals from the number of missiles. Then start again, using just the remaining missiles.
Repeat this until you use up all the missiles or all factories are wiped out.

How do I get average rating and round up to nearest 1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a voting table called product_reviews, with a field that is ENUM from 1 to 5 called rating.
I want to get average rating from all rows with product_id 1665. I am using a star rating system so the average cannot be with decimals. Must be a number from 1 to 5.
Thank you very much.
SELECT round(avg(rating)) as average_review,
count(rating) as number_of_reviews
FROM product_reviews
WHERE product_id = 1665
You need to use round here since avg will return a value between 1.0 and 5.0, and if you use floor or ceil you're effectively eliminating 1 or 5 from the possible results unless all reviews on the product have that score.
Also, you'll alienate your users if they can openly see a product got 50 reviews with 1 star, and 1 review with 2 stars, and you're showing an average score of 2 stars next to that. It'll make your site seem unreliable which is usually not a good thing for a site that contains reviews. For this reason most star rating systems are also capable of showing partial stars to make it more precise.

nursery application - how to structure my database [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 9 years ago.
Improve this question
I'm having trouble getting my head around exactly what is the best way to build an application which is supposed to manage a nursery, particularly, who is in what room when.
Nurseries have several rooms and each room has an age range allowed in it, and a maximum number of children it can accommodate at any one time.
Children get booked in in a rota type style for mornings and afternoons, so a kid might be booked to be in on the following times.
monday am pm
tuesday pm
wed pm
thurs am pm
fri am pm
sat
sun
So I collect this rota in a table like so:
id
child_id
mon_am <-- if booked in then 1, if not then 0
mon_pm
tues_am
etc...
And then allocate a child to a room.
Now when adding a new child I need to be able to check to see what rooms there are spaces available in, so that I can know whether the appropriate room has the spare capacity. So with the info above and assuming the room in question has a capacity (for example) of 1 child, if someone wants to sign up for that room on tues and wed am, then they can because the first kid isn't booked in at that time.
I'm a bit new to all this and I'm having trouble figuring out what I need to do to check if a room has spare capacity. Can anyone help me out at all?
thanks
SELECT COUNT(1) AS `numChildrenBooked` FROM `your_table` WHERE `mon_am` = 1;
will return the number of rows in which mon_am is == 1 in your table. Compare that to the maximum number of children per room to check if there's space available.
What's bad in your db-structure probably is (I think) that you can only plan for one week at once...

Calculate stars from grades(5-1 and 1-5) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have grades from 1 to 5 where 1 is the best and 5 is the worst(But not just integral numbers like 1, also 1.14 and so on).
I want to calculate how many stars each grade would be(5 stars would be the best, 1 star the worst - 3 would stay 3 of course - but the rest?)
I googled to get the result but I'm not finding anything helpful. Maybe I'm missing the forest for the trees, I don't know. Basically it would be the reverse number I guess?
I want to do that in PHP.
Pretty straightforward I would think:
$new_grade = 6 - $old_grade;
Or for a more generic solution:
$new_grade = $grade_max + $grade_min - $old_grade;
Star = 6 - Grade
So:
5 → 1
4 → 2
3 → 3
2 → 4
1 → 5

Categories