Is there prettier way to get the full-date of the next 1st of August:
$year = date('Y') + 1;
if (date('j') >= 1 && date('j') <= 8) {
$year = date('Y');
}
$nextAugust = '01-08-' . $year;
$nextAugust = Carbon::createFromFormat('d-m-Y', $nextAugust);
Preferably with Carbon PHP
Following code find next august with carbon object.
$nextAugust = Carbon::parse('first day of August' . (date('n') > 8 ? ' next year' : ''));
I hope my answer can help for your problem.
This will do it:
$nextAugust = Carbon::createFromFormat('d-m-Y', '01-08-' . (date('Y') + intval(date('m') / 8)));
Clarification:
Instead of the if condition, you can divide the current month by 8 and convert the resulting value to an integer using intval(date('m') / 8). This will give you either 0 or 1, and will be added to the current year. For example, if you are in March (i.e., m is 3), you get 3/8 = 0. Hence, next August will be in the same year because 2018 + 0 will be 2018. On the other hand, if you are in October (i.e., m is 10), you get 10/8 = 1. Hence, next August will be in the next year because 2018 + 1 will be 2019. Here's how you simply implement it:
Hope it helps.
For me, I'll do:
$date = '01-08-';
$year_now = Carbon::now()->format('Y');
$date = $date.$year_now;
$carbon_date = Carbon::createFromFormat('d-m-Y', $date);
Let me know if it is not working.
Related
i am new to programming and starting up with kotlin . i've been stuck on this problem for a few days and i really need some assistance . i am trying to read a user input of Strings in a format like this 3 + 2 + 1, go through the String and wherever there is an operator , add up the numbers before and after the operator sign . so the above 3 + 2 + 1 should output 6.
Here's a snippet of my code
fun main() {
val userInput = readLine()!!.split(" ")
var sum = 0
for (i in 0 until userInput.size) {
if (userInput.get(i) == "+"){
sum += userInput.get(i-1).toInt() + userInput.get(i+1).toInt()
}
}
println(sum )
}
my code works until the point of adding up the numbers . it repeats the next number after the operator , so using the above example of 3 + 2 + 1 it outputs 8 thus 3 + 2 + 2 + 1. I'm so confused and don't know how to go about this .
Try not to increment the sum value each time, but rewrite the last number which was participated in sum. Just like that:
You have the case: 1 + 2 + 3 + 4
Split them
Now you have the array [1, +, 2, +, 3, +, 4]
Then you iterate this array, stuck with the first plus and sum the values.
Rewrite the second summed value with your sum.
Now you have new array [1, +, 3, +, 3, +, 4]
At the end of the loop, you will have this array [1, +, 3, +, 6, +, 10]
And your sum is the last element of the array
The logic of your code is that for each "+" encountered, it adds the sum of the numbers left and right of the "+" to the sum. For the example "1 + 2 + 3", here's what is happening:
Starting sum is 0.
At first "+", add 1 + 2 to sum, so now sum is 3.
At second "+", add 2 + 3 to sum, so now the sum is 3 + 5 = 8.
So you are adding all the middle numbers to the total twice, because they each appear next to two operators.
One way to do this is start with the first number as your sum. Then add only the number to the right of each "+", so numbers are only counted once.
fun main() {
val userInput = readLine()!!.split(" ")
var sum = userInput[0].toInt()
for (i in userInput.indices) {
if (userInput[i] == "+") {
sum += userInput[i + 1].toInt()
}
}
println(sum)
}
sum += userInput.get(i-1).toInt() + userInput.get(i+1).toInt()
This is only valid for the first iteration, so if the user puts 1 + 2 + 3.
So
userInput[0] is 1
userInput[1] is +...
the first time that line will be triggered sum will be 1 + 2, and that's quite fine, but the second time, in the second +, you will sum i-1 (which is 2 and was in the total sum already) and i+1, that will be 3, so you are doing 1+2+2+3.
You need to understand why this is happening and think of another way to implement it.
Check this is working for me
var sum = 0
readLine()?.split(" ")?.filter { it.toIntOrNull() != null }?.map { sum += it.toInt() }
println(sum)
Simple example:
days = range(1,10)
for d in days:
model.AddBoolXOr(a,b,c,d,e,f,g)
Above I can ensure only one of a...g is true every day. But it is not always possible to achieve this every day so I want to be able to maximize the number of times it is achieved. Something like...
array_bools = []
days = range(1,10)
for d in days:
day_bool = NewBoolVar('name')
model.Add(day_bool = XOr(a,b,c,d,e,f,g))
array_bools.append(day_bool)
model.Maximize(sum(array_bool[i] for i in range(len(array_bool))))
array_bools = []
days = range(1,10)
for d in days:
day_bool = NewBoolVar('name')
model.Add(sum([a,b,c,d,e,f,g]) == 1).OnlyEnforceIf(day_bool)
model.Add(sum([a,b,c,d,e,f,g]) != 1).OnlyEnforceIf(day_bool.Not())
array_bools.append(day_bool)
model.Maximize(sum(array_bool))
See this page of documentation
If I want to a calculate a day of the week in the future the result is pretty simple to get:
enum { SUNDAY = 0, MONDAY = 1/*....*/ SATURDAY = 6}
int getDayInFuture(int currentDay, int numDaysForward)
{
return (currentDay + numDaysForward) % 7;
}
But I have a function where you can enter a number of days either forward or backward, and I'm having trouble for when calculating a day in the past. The best I've done so far is:
inline int getDayInFutureOrPast(int currentDay, int numDaysForwardOrBack)
{
int result = (currentDay + numDaysForwardOrBack);
if (result >= 0) return result % 7; // JUST CALCULATE IT SIMPLY AS NORMAL
else // GOING BACKWARDS
{
int remainder = result % 7;
if (remainder == 0) return 0; // I HAVE TO ADD THIS SPECIAL CONDITION, IF I DON'T SUNDAY
// (enum 0) minus 7 days ends up as -7 + (-7 modulo 7) == 7
// SHOULD BE 0, SUNDAY(0) MINUS 7 DAYS SHOULD BE SUNDAY(0)
else return 7 + remainder;
}
}
I get the feeling there's a simpler way to do this but I can't think of it.
Summing the currentDay and numDaysForwardOrBack will give the desired day, but it's not in the 0 to 6 range. Using modulo on the sum will give us the remainder of the quotient of Sum/7, but this first modulo operation will still potentially give us a negative result. To eliminate negative results, I add 7 to the result and perform modulo again. This secondary addition operation does not change results that were positive to begin with because the modulo removes the excess, and negative values are shifted up to the positive range.
inline int getDayInFutureOrPast(int currentDay, int numDaysForwardOrBack)
{
int remainder = (currentDay + numDaysForwardOrBack) % 7; // value range is [-6 to 6]
return (7 + remainder) % 7; //shifts value up to bring value range to [0 to 6]
}
Can someone please calculate the the no. of steps it will take to execute the above code?
And verify the solution, with some input values of n.
(found some relevant question, but not helping)
int count=0;
for(int i=1; i<=n ;i=i*2)
{
for(int j=1; j<=i; j=j*2)
{
count++;
}
}
We can make a table:
i = 1: j = 1 --> 1 count
i = 2: j = 1,2 --> 2 counts
i = 4: j = 1,2,4 --> 3 counts
i = 8: j = 1,2,4,8 --> 4 counts
The pattern should be clear from here. We can reimagine the pattern such that i = 1, 2, 3, 4, ..., and instead of going from 1 to n, let's just say it goes from 1 to log n. This means that the total count should be the sum from i = 1 to log (base 2) n of i. The sum from i = 1 to x of i is simply x(x+1)/2, so if x = log_2(n), then this sum is simply (log_2(n) * log_2(n)+1)/2
EDIT: It seems like I made a mistake somewhere, and what I wrote is actually f(n/2) based on empirical tests. Thus, the correct answer is actually (log_2(2n) * log_2(2n)+1)/2. Nevertheless, this is the logic I would follow to solve a problem like this
EDIT 2: Caught my mistake. Instead of saying "let's just say it goes from 1 to log n", I should have said "let's just say it goes from 0 to log n" (i.e., I need to take the log of every number in the series)
inner-loop
i = 1 --> log(1) = 0
i = 2 --> log(2) = 1
i = 4 --> log(4) = 2
i = 8 --> log(8) = 3
i = 16 -> log(16) = 4
i = 32 -> log(32) = 5
i = 64 -> log(64) = 6
.
.
.
i = n -> log(n) = log(n)
That is the amount of work and it will stop after log(n) iterations as i hits n.
1 + 2 + 3 + 4 +...+ log(n) = [(1+log(n))*log(n)]/2 = O(log^2(n))
Since I am new to cuda .. I need your kind help
I have this long vector, for each group of 24 elements, I need to do the following:
for the first 12 elements, the even numbered elements are multiplied by -1,
for the second 12 elements, the odd numbered elements are multiplied by -1 then the following swap takes place:
Graph: because I don't yet have enough points, I couldn't post the image so here it is:
http://www.freeimagehosting.net/image.php?e4b88fb666.png
I have written this piece of code, and wonder if you could help me further optimize it to solve for divergence or bank conflicts ..
//subvector is a multiple of 24, Mds and Nds are shared memory
____shared____ double Mds[subVector];
____shared____ double Nds[subVector];
int tx = threadIdx.x;
int tx_mod = tx ^ 0x0001;
int basex = __umul24(blockDim.x, blockIdx.x);
Mds[tx] = M.elements[basex + tx];
__syncthreads();
// flip the signs
if (tx < (tx/24)*24 + 12)
{
//if < 12 and even
if ((tx & 0x0001)==0)
Mds[tx] = -Mds[tx];
}
else
if (tx < (tx/24)*24 + 24)
{
//if >12 and < 24 and odd
if ((tx & 0x0001)==1)
Mds[tx] = -Mds[tx];
}
__syncthreads();
if (tx < (tx/24)*24 + 6)
{
//for the first 6 elements .. swap with last six in the 24elements group (see graph)
Nds[tx] = Mds[tx_mod + 18];
Mds [tx_mod + 18] = Mds [tx];
Mds[tx] = Nds[tx];
}
else
if (tx < (tx/24)*24 + 12)
{
// for the second 6 elements .. swp with next adjacent group (see graph)
Nds[tx] = Mds[tx_mod + 6];
Mds [tx_mod + 6] = Mds [tx];
Mds[tx] = Nds[tx];
}
__syncthreads();
Thanks in advance ..
paul gave you pretty good starting points you previous questions.
couple things to watch out for: you are doing non-base 2 division which is expensive.
Instead try to utilize multidimensional nature of the thread block. For example, make the x-dimension of size 24, which will eliminate need for division.
in general, try to fit thread block dimensions to reflect your data dimensions.
simplify sign flipping: for example, if you do not want to flip sign, you can still multiplied by identity 1. Figure out how to map even/odd numbers to 1 and -1 using just arithmetic: for example sign = (even*2+1) - 2 where even is either 1 or 0.