So I was asked to solve this problem which I don't fully understand that's why I have difficulties starting with it since I'm still a beginner. here it is :
There are approximately 2.5 billion people on the Internet as of
January 2013. Facebook reached one billion users in October of 2012. In this exercise, you’ll
write a program to determine when Facebook will reach 2.5 billion people if it were to grow at
fixed monthly percentage rates of 2%, 3%, 4% or 5%.
As u see I should compute 4 times with different rate and I should each time using a for loop , get month and year when it reach 2.5 billion user . I was given this hint from another exercise to use inside the for loop but I didnt understand how to use it .
amount = pow ( 1.0 * rate , year )
now let say u fixed the border of loop for ( int i = 1000000000 ; i <= 2500000000 ; here if I put ++i its not logical ! )
then inside the loop its easier I think I'm going to count month let say we start at 10 ( which is October 2012 ) if we reach 25 that mean 10 + 15 which mean the date will be January 2014 but the prob is on how to make a cout statement to the user of those information u see? can u help me get the right structure ? thanks
This can be solved via using a while loop (instead of a for loop), like so:
#include <iostream>
double GrowthRatio = 1.05d; // Using 5% growth in this example.
double nUsers = 1000000000;
int MonthsElapsed = 0;
while((nUsers *= GrowthRatio) < 2500000000)
MonthsElapsed++;
std::cout << MonthsElapsed;
Output:
18
Which means it took Facebook 18 months (from October 2012) to reach 2.5 billion users.
Related
I have tried "If, and, or" combining with simple script, and I am really new at the Apps script in Google Sheets.
What I need is: if any condition is true, I want the formula/function to subtract the corresponding value only for that condition from a total.
Background: a client is expected to serve 2-3 hours a day M-F (it can vary). If M=3, T=2, W=1, TH = 3, Fri = 3, then weekly total hours (WorkHours) = 12.
But the client may not always be scheduled on any day and these nonscheduled days can vary by client location. When this occurs, I need to reduce WorkHours by the number of hours for a nonscheduled day.
For Example:
When Cell C4 = Monday and Cell C6 = x are true, that equals a nonscheduled day on Monday then the formula would only subtract Monday hours from WorkHours total = 9. But there can be multiple days, M, W and F. This can vary.
I tried the first half of this formula:
=IF(OR(AND(C4="Monday",C6="x"),AND(D4="Tuesday",D6="x"),AND(E4="Wednesday",E6="x"),AND(F4="Thursday",F6="x"),AND(G4="Friday",G6="x"))
But I can’t get the “then” half of this if-statement to work in a formula.
I tried combining it with Apps Script and I can share that, but it’s nothing to be proud of.
Thank you so much
Marby
Taking a wild guess from your data, try a SUMIFS or SUMIFformula
=SUMIFS(A4:E4,A6:E6,"")
OR
=SUMIFS(A4:E4,A6:E6,"<>x")
OR
=SUMIF(A6:E6,"",A4:E4)
(If still in need do share a test sheet or more info)
perhaps the solution to your problem is simpler than it looks, and there is no need to use scripts to make this calculation.
try to do the following way
A
B
C
1
DayWeek
isValid
TimeWorked
2
M
X
3
3
T
X
2
4
W
X
1
5
TH
3
6
F
X
3
FORMULA TO CALCULATE
=SUMIF(B2:B6;"X";C2:C6) // result 9 as example
if your spreadsheet is fed sequentially, or has multiple collaborators, I suggest you use the rows as columns.
A
B
C
D
E
F
G
H
I
J
K
1
M
3
T
2
W
1
TH
x
F
3
FORMULA
FORMULA
=sum(sumif(B1;">0";B1);sumif(D1;">0";D1); sumif(F1;">0";F1); sumif(H1;">0";H1);sumif(J1;">0";J1))
if you are sure that you will not have other numbers in the columns, the formula can be reduced to
=sum(SUMIF(A1:J1;">0";A1:J1))
Initially, none of your answers would have helped with the sheets as I had structured them. But your answers sparked a restructuring of my sheets. It made them simpler and more useful to my users.
I really appreciate all your help. It helped me improve my product. Now that my sheets have been adapted, the formula I used =SUMIFS(A4:E4,A6:E6,"<>x") - working beautifully.
I have to really thank you - you helped me improve my whole approach.
This is great, really really helpful
Marby
I am trying to overcome a machine allocation problem with time horizon of 5 day. Production plan is hard to catch up, so my objective is to minimize total machines working time spent. Machines uses molds to produce and there are molds for each type of product. If a product is produced at the end of the day and if there will be production later day, total setup needed for that machine should be decreased by one. For this reason,
sets
i: mold type
j:jobs
k: days
parameters
x(i,k) ith mold production needed at day k
y(i,j) 1 if ith mold is compatible with jth machine
Decision variable
m(i,j,k) : 1 if ith mold processed in jth machine in day k 0 o/w
b(j,k) setup number of jth machine in day k
While computing the setup number for day 1, b(j,’1’), is simply equal to the sum of m(i,j,k).
For computing other days setup number I tried these but these made problem nonlinear and it takes months to solve.
b(j,'2')=e=sum(i,m(i,j,'2')) - sum(i,m(i,j,'2')*m(i,j,'1'))
By this way, if mold i is produced in both days, there will not be any setup made at second day. In order to restrain multiple setup reduction I put: sum(i,m(i,j,'2')*m(i,j,'1')) =l= 1
So, how can I decrease the setup number for a machine if it has used a mold a day before without making the problem nonlinear.
It is possible to linearize m(i,j,'2')*m(i,j,'1'):
Both(i,j) <= m(i,j,'2')
Both(i,j) <= m(i,j,'1')
Both(i,j) >= m(i,j,'2')+m(i,j,'1')-1
Both(i,j) is a binary variable
This transformation is done automatically by some solvers.
Note that there are alternative ways to model the start of a run, and often there are things to exploit (depending on the details).
So i want to solve problem ,that looks like nurse scheduling Problem, the different is that there is 2 type of Workers, who should work minimum 40 hours per week, the other type should work minimum 10 hours per week.
In order to solve this problem with simplex , i have to define the shifts(shift types). the shifts in a day should be minimum 3 hours and maximum 9 hours.
this photo is what i have tried so farShifts
And total of shift types would be 55. and if i multiply that to the number of workers and days. that would be 15000 variables. is it possible to solve a problem like this, or should i try to do it other way?
Has anyone suggestion?
You have a nurse example at CPLEX_Studio129\opl\examples\opl\nurses.
To do what you need you need to change
//global max worked time
forall(n in Nurses)
ctNurseMaxTimeConstraints:
NurseWorkTime[n] <= MaxWorkTime;
into
forall(n in Nurses)
ctNurseMaxTimeConstraints:
NurseWorkTime[n] <= n.MaxWorkTime;
where maxWorkTime is not global but per nurse. 15000 binary decision variables should be fine.
Currently I am dealing with a massive amount of Data in the original form of a list through combination. I am running conditions on each set of list through a for loop. Problem is this small for loop is taking hours with the data. I'm looking to optimize the speed by changing some functions or vectorizing it.
I know one of the biggest NO NOs is don't do Pandas or Dataframe operations in for loops but I need to sum up the columns and organize it a little to get what I want. It seems unavoidable.
So you have a better understanding, each list looks something like this when its thrown into a dataframe:
0
Name Role Cost Value
0 Johnny Tsunami Driver 1000 39
1 Michael B. Jackson Pistol 2500 46
2 Bobby Zuko Pistol 3000 50
3 Greg Ritcher Lookout 200 25
Name Role Cost Value
4 Johnny Tsunami Driver 1000 39
5 Michael B. Jackson Pistol 2500 46
6 Bobby Zuko Pistol 3000 50
7 Appa Derren Lookout 250 30
This is the current loop, any ideas?
for element in itertools.product(*combine_list):
combo = list(element)
df = pd.DataFrame(np.array(combo).reshape(-1,11))
df[[2,3]] = df[[2,3]].apply(pd.to_numeric)
if (df[2].sum()) <= 5000 and (df[3].sum()) > 190:
df2 = pd.concat([df2, df], ignore_index=True)
Couple things I've done that have sliced off some time but not enough.
*df[2].sum() to df[2].values.sum----its faster
*where the concat is in the if statement I've tried using append and also adding the dataframe together as a list...concat is actually 2 secs faster normally or it will end up being about the same speed.
*by the .apply(pd.to_numeric) changed it to .astype(np.int64) its faster as well.
I'm currently looking at PYPY and Cython as well but I want to start here first before I go through the headache.
So we have a timeline of T days in which some tasks have to be performed.
Every task has a penalty score. If the task is not performed in the given timeline , it's score adds up in the final penalty score. Every task can be performed only after it's given starting time.
The input will be given in the format:
T
Score Quantity_of_task Starting_time
For eg :
T = 10
140 5 4
This means that 5 tasks with penalty score 140 have to be performed from 4th day onwards.
You can perform at most 1 task on a particular day.
The goal is to minimize the final penalty score.
What I tried to do:
Example -
T = 10
Input size = 5
150 4 1
120 4 3
200 2 7
100 10 5
50 5 1
I sorted the list according to the penalty score , and greedily assigned the tasks with high penalty score to their corresponding days,i.e
2 tasks with highest score 200 are assigned to days 7 and 8
4 tasks with next highest score 150 are assigned to 1,2,3,4 days
4 tasks with next highest score 120 are assigned to 5,6,9,10 days
which gives the schedule as
150 150 150 150 120 120 200 200 120 120
Left out tasks:
10 tasks with 100 score = 1000 penalty
5 tasks with 50 score = 250 penalty
Final penalty = 1250.
This requires O(T * input_size). Is there a more elegant and optimized way of doing it?
Both input size and T have a constraint of 10^5.
Thanks.
If you store the available days in an ordered set, then you can perform your algorithm much faster.
For example, C++ provides an ordered set with a lower_bound method that will find in O(logn) time the first available day after the starting time.
Overall this should give an O(nlogn) algorithm where n = T+input_size.
For example, I suspect that when you have your 4 tasks of penalty 120 to assign from day 3 onwards, your current code will loop over days 3,4,5,etc. until you find a day that has not been assigned. You can now replace this O(n) loop with a single O(logn) call to lower_bound to find the first unassigned day. When you greedily assign the days, you should also remove them from the set so they won't be assigned twice.
Note that there are only T days so there will be at most T day assignments. For example, suppose all tasks have starting time 1, and quantity T. Then the first task will take O(Tlogn) time to assign, but all subsequent tasks will only need a single call to lower_bound (because there are no days left to assign), so will take O(logn) each.