How do I check if a string contains another string in Objective-C?

NSString *string = @”hello bla bla”; if ([string rangeOfString:@”bla”].location == NSNotFound) { NSLog(@”string does not contain bla”); } else { NSLog(@”string contains bla!”); } The key is noticing that rangeOfString: returns an NSRange struct, and the documentation says that it returns the struct {NSNotFound, 0} if the “haystack” does not contain the “needle”. And if … Read more

Why does range(start, end) not include end?

Because it’s more common to call range(0, 10) which returns [0,1,2,3,4,5,6,7,8,9] which contains 10 elements which equals len(range(0, 10)). Remember that programmers prefer 0-based indexing. Also, consider the following common code snippet: for i in range(len(li)): pass Could you see that if range() went up to exactly len(li) that this would be problematic? The programmer … Read more

How to populate a table with a range of dates?

Try this: DROP PROCEDURE IF EXISTS filldates; DELIMITER | CREATE PROCEDURE filldates(dateStart DATE, dateEnd DATE) BEGIN WHILE dateStart <= dateEnd DO INSERT INTO tablename (_date) VALUES (dateStart); SET dateStart = date_add(dateStart, INTERVAL 1 DAY); END WHILE; END; | DELIMITER ; CALL filldates(‘2011-01-01′,’2011-12-31′); Here’s the SQL Fiddle to play with it: http://sqlfiddle.com/#!2/65d13/1 EDIT (to check if … Read more

Identify groups of continuous numbers in a list

EDIT 2: To answer the OP new requirement ranges = [] for key, group in groupby(enumerate(data), lambda (index, item): index – item): group = map(itemgetter(1), group) if len(group) > 1: ranges.append(xrange(group[0], group[-1])) else: ranges.append(group[0]) Output: [xrange(2, 5), xrange(12, 17), 20] You can replace xrange with range or any other custom class. Python docs have a … Read more

Why does Range work, but not Cells?

The problem is that Cells is unqualified, which means that the sheet to which those cells refer is different depending on where your code is. Any time you call Range or Cells or Rows or UsedRange or anything that returns a Range object, and you don’t specify which sheet it’s on, the sheet gets assigned … Read more