multiple instance if statement in xcode (iphone) - iphone

what is the proper way to do If statement with two variable in Xcode for Iphone
currently I have
if (minute >0) && (second == 0) {
minute = minute - 1;
second = 59;
}

The same was you would do it in any C/C++/Objective-C compiler, and most Algol derived languages, and extra set of parenthesis in order to turn to seperate boolean statements and an operator into a single compound statement:
if ((minute > 0) && (second == 0)) {
minute = minute - 1;
second = 59;
}

You'll need another set of parenthesis:
if ((minute >0) && (second == 0)) {
minute = minute - 1;
second = 59;
}

Or you could also write:
if (minute > 0 && second == 0)
Which is what you'll start doing eventually anyway, and I think (subjective) is easier to read. Operator precedence insures this works...

Related

sorting dates if timestamp is greater than zero

I'm trying to sort dates from oldest to most recent but I want timestamps that are zero to be at the bottom of the sorted list. Only dates that are greater than zero should be sorted and at the top of the list.
The issue I have is that timestamps that are zero show up at the top... how do I ignore zero timestamps and put them at the bottom and give the timestamps that are greater > 0 priority on the list ?
Here's what I have:
//DATE SORT
self.tasklist.sort { $0.onsiteThreshold < $1.onsiteThreshold }
self.tasklist.sort { $0.resolutionThreshold < $1.resolutionThreshold }
self.tasklist.sort { $0.scheduleDateTime < $1.scheduleDateTime }
self.tasklist.sort { (o1, o2) -> Bool in
if o1.onsiteThreshold == 0 || o2.onsiteThreshold == 0 || o1.resolutionThreshold == 0 || o2.resolutionThreshold == 0 || o1.scheduleDateTime == 0 || o2.scheduleDateTime == 0 {
return false
}
return true
}
Sorting an array four(!) times is pretty expensive.
A more efficient way is first to move all items which contain zero properties to the end of the list with partition(by:). This API is part of Standard Library but doesn't preserve the order of the left side, therefore the partition must take place first.
let partitionIndex = tasklist.partition(by: {$0.onsiteThreshold == 0 || $0.resolutionThreshold == 0 || $0.scheduleDateTime == 0})
Then sort the left side tasklist[0..<partitionIndex] by all three properties and append the right side.
let sorted = tasklist[0..<partitionIndex].sorted { (lhs, rhs) in
if lhs.scheduleDateTime == rhs.scheduleDateTime {
if lhs.resolutionThreshold == rhs.resolutionThreshold {
return lhs.onsiteThreshold < rhs.onsiteThreshold
}
return lhs.resolutionThreshold < rhs.resolutionThreshold
}
return lhs.scheduleDateTime < rhs.scheduleDateTime
}
taskList = sorted + tasklist[partitionIndex...]

Group typo3 condition

I need such a condition in ts:
([treeLevel = 0] && [globalVar = GP:R > 0]) || [PIDinRootline = {$pages.2018}]
I wanna show block if page has treelevel=0 and the get var R > 0, or if page id = $pages.2018
It looks like the similar code in php:
if(($treeLevel == 0 && $r > 0) || (pid == number))
The all expression in first brackets should be right, or in second.
Is it exist the method to group it like the previous record or I can only use userfunc?
There is no grouping in TS conditions, but if you need this particular condition from your post I think it is not needed because brackets around && are useless in this case.
(p && q) || r
is exactly the same as
p && q || r
Did you tested it?

speed up prime number generating

I have written a program that generates prime numbers . It works well but I want to speed it up as it takes quite a while for generating the all the prime numbers till 10000
var list = [2,3]
var limitation = 10000
var flag = true
var tmp = 0
for (var count = 4 ; count <= limitation ; count += 1 ){
while(flag && tmp <= list.count - 1){
if (count % list[tmp] == 0){
flag = false
}else if ( count % list[tmp] != 0 && tmp != list.count - 1 ){
tmp += 1
}else if ( count % list[tmp] != 0 && tmp == list.count - 1 ){
list.append(count)
}
}
flag = true
tmp = 0
}
print(list)
Two simple improvements that will make it fast up through 100,000 and maybe 1,000,000.
All primes except 2 are odd
Start the loop at 5 and increment by 2 each time. This isn't going to speed it up a lot because you are finding the counter example on the first try, but it's still a very typical improvement.
Only search through the square root of the value you are testing
The square root is the point at which a you half the factor space, i.e. any factor less than the square root is paired with a factor above the square root, so you only have to check above or below it. There are far fewer numbers below the square root, so you should check the only the values less than or equal to the square root.
Take 10,000 for example. The square root is 100. For this you only have to look at values less than the square root, which in terms of primes is roughly 25 values instead of over 1000 checks for all primes less than 10,000.
Doing it even faster
Try another method altogether, like a sieve. These methods are much faster but have a higher memory overhead.
In addition to what Nick already explained, you can also easily take advantage of the following property: all primes greater than 3 are congruent to 1 or -1 mod 6.
Because you've already included 2 and 3 in your initial list, you can therefore start with count = 6, test count - 1 and count + 1 and increment by 6 each time.
Below is my first attempt ever at Swift, so pardon the syntax which is probably far from optimal.
var list = [2,3]
var limitation = 10000
var flag = true
var tmp = 0
var max = 0
for(var count = 6 ; count <= limitation ; count += 6) {
for(var d = -1; d <= 1; d += 2) {
max = Int(floor(sqrt(Double(count + d))))
for(flag = true, tmp = 0; flag && list[tmp] <= max; tmp++) {
if((count + d) % list[tmp] == 0) {
flag = false
}
}
if(flag) {
list.append(count + d)
}
}
}
print(list)
I've tested the above code on iswift.org/playground with limitation = 10,000, 100,000 and 1,000,000.

Consistent random colour highlights

In a table I have columns with to and from dates, I highlight overlaps between rows taking into account the periods, this is done exhaustively in nested loops. This is not the issue.
I need the same colour for the rows that overlap.
sub highlight_overlaps {
my $date_from1;
my $date_to1;
my $date_from2;
my $date_to2;
my $i = 0;
my $j = 0;
for ($i; $i < $#DATE_HOLDER; $i++) {
$date_from1 = $DATE_HOLDER[$i][0];
$date_to1 = $DATE_HOLDER[$i][1];
my $red = int(rand(65)) + 190;
my $green = int(rand(290)) - 55;
my $blue = int(rand(290)) - 55;
for ($j=$i+1; $j<=$#DATE_HOLDER; $j++) {
$date_from2 = $DATE_HOLDER[$j][0];
$date_to2 = $DATE_HOLDER[$j][1];
if (($date_from1 le $date_to2 && $date_to1 ge $date_to2) ||
($date_from1 le $date_from2 && $date_to1 le $date_to2) ||
($date_from1 gt $date_from2 && $date_from1 lt $date_to2)) {
$tb->setCellStyle($i+2, 6, "background-color:rgb($red,$green,$blue);font-size:9pt");
$tb->setCellStyle($i+2, 7, "background-color:rgb($red,$green,$blue);font-size:9pt");
$tb->setCellStyle($j+2, 6, "background-color:rgb($red,$green,$blue);font-size:9pt");
$tb->setCellStyle($j+2, 7, "background-color:rgb($red,$green,$blue);font-size:9pt");
}
}
}
}
This works fine if it's just a pair of dates; say:
1) 25-06-2012 27-06-2012
2) 18-06-2012 29-06-2012
Will get the same colour
If though I have
0) 26-06-2012 28-06-2012
1) 25-06-2012 27-06-2012
2) 18-06-2012 29-06-2012
0 will get a different colour while 1 & 2 are paired as intended.
When and how to pick colours so that different colours are only applied to different overlaps?
Following up on the first answer; how may I represent overlaps in order to store them in a data structure, so that I can colour them after their detection?
You'll have to compare each interval against each other interval, and put them in 'buckets' when they are equal. Now when you compare an interval to a third interval, you put the third in the same bucket as the interval.
Then you print the buckets.
Perl's hash would make for fine buckets.
About your overlap detection
There is no overlap if
date1_to < date2_from OR
date2_to < date1_from
Or, in Perl:
if ($date_to1 lt $date_from2 || $date_to2 lt $date_from1) {
#overlap
}
Invert that either using Perl's unless, or using de Morgan:
if ($date_to1 ge $date_from2 && $date_to2 ge $date_from1) {
#overlap
}

How do I add a condition to an existing conditional expression?

I had a programmer write a Perl script for my site.
One of the functions is to update price/stock when a certain condition is met.
# update when price/stock conditions met
if ( ($force_price_updates == 1) ||
($data->{'price'} <= $product_price && $data->{'quantity'} > 0) ||
($product_quantity == 0 && $data->{'quantity'} > 0) ) {
What the above is not doing is not updating the price if the new price is higher. It updates the stock value, but if the new stock comes at a higher price, I lose out. Stock gets updated and but the price is not.
The script goes through a number of feeds and if the same product is found in any of the feeds, the script should amend price/stock change according to the rule above.
I can't find the programmer and my Perl knowledge is limited. I understand what the code is doing, but don't know what it should do if the price is higher and stock is greater than zero.
You can add the extra condition you're looking for to that statement.
The condition you're looking to match is:
$data->{'price'} > $product_price && $product_quantity > 0
So the final version would look like this:
if (($force_price_updates == 1) || ($data->{'price'} <= $product_price && $data->{'quantity'} > 0) || ($product_quantity == 0 && $data->{'quantity'} > 0) || ($data->{'price'} > $product_price && $product_quantity > 0)) {