This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_longest_palindrome(text): | |
best_palindrome = '' | |
length = len(text) | |
for i in range(length * 2 - 1): | |
start = i / 2 - i % 2 | |
end = i / 2 + 1 | |
while start > 0 and end < length and text[start] == text[end]: | |
start -= 1 | |
end += 1 | |
candidate = text[start+1:end] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare @String varchar(8000) = 'Fourscoreand...' | |
select l.N as TestLen | |
, c.N as TestChar | |
, substring(s.String, c.N, l.N) as Candidate | |
from (select @String as String) s | |
cross join master.dbo.Tally l | |
cross join master.dbo.Tally c | |
where 1 = 1 | |
and l.N > 1 | |
and l.N <= len(s.String) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getPrimes(n = 1000000): | |
#Generate list of primes using the sieve of Eratosthenes | |
#the list was arbitrarily chosen to include positive integers | |
#up to 10**6 and it turned out to be enough | |
primes = range(n) | |
primes[1] = 0 | |
for p in primes: | |
if p: | |
x = 2*p | |
while x < n: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import combinations, chain | |
nums = tuple([int(v) for v in "3 4 9 14 15 19 28 37 47 50 54 56 59 61 70 73 78 81 92 95 97 99".split(' ') if v.strip()]) | |
print sum([1 for v in chain(*[combinations(nums, r) for r in range(3, len(nums))]) if sum(v[0:-1]) == v[-1]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add some documents. | |
Document doc = new Document(); | |
doc.add(new Field("name", "George Washington", Field.Store.YES, Field.Index.NO)); | |
doc.add(new NumericIntervalField("term", true, 1789, 1793)); | |
doc.add(new NumericIntervalField("term", true, 1793, 1797)); | |
indexWriter.addDocument(doc); | |
doc = new Document(); | |
doc.add(new Field("name", "John Adams", Field.Store.YES, Field.Index.NO)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)forwardInvocation:(NSInvocation *)inv { | |
Class targetClass = [self class]; | |
SEL selector = inv.selector; | |
runCallbacks(beforeBlocks, self, targetClass, selector); | |
@try { | |
IMP imp = method_getImplementation(class_getInstanceMethod(targetClass, selector)); | |
// This is a private method and must not make it's way to production code!! | |
[inv invokeUsingIMP:imp]; | |
} @finally { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (void)preventInstrumentation:(Class)cls selector:(SEL)selector { | |
Method method = class_getInstanceMethod(cls, selector); | |
class_addMethod(classes[cls], method_getName(method), method_getImplementation(method), | |
method_getTypeEncoding(method)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *newName = [NSString stringWithFormat:@"CCInstrumentedProxy_%s", class_getName(cls)]; | |
Class subClass = objc_allocateClassPair([CCInstrumentedProxy class], [newName UTF8String], 0); | |
classes[cls] = subClass; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (id)instrumentInstanceMessagesSwizzledAllocWithZone:(NSZone *)zone { | |
id result = [self instrumentInstanceMessagesSwizzledAllocWithZone:zone]; | |
liveObjectClass[result] = self; | |
object_setClass(result, classes[self]); | |
return result; | |
} | |
+ (id)instrumentInstanceMessagesAddedAllocWithZone:(NSZone *)zone { | |
Method method = GetSuperMethod(self, @selector(allocWithZone:)); |