Skip to content

Instantly share code, notes, and snippets.

View robbywalker's full-sized avatar

Robby Walker robbywalker

View GitHub Profile
@robbywalker
robbywalker / v1_level1_n_2.py
Created October 21, 2010 22:58
O(n^2) solution to GPCv1 Level 1
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]
@robbywalker
robbywalker / v1_level1_tsql.sql
Created October 21, 2010 23:01
Tim Lerner's T-SQL solution to GPCv1 Level 1
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)
@robbywalker
robbywalker / v1_level2.py
Created October 21, 2010 23:03
Rodrigo Farnham's readable Python solution to GPCv1 Level 2
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:
@robbywalker
robbywalker / v1_level3.py
Created October 21, 2010 23:05
David Koblas's one-liner for GPCv1 Level 3
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]])
@robbywalker
robbywalker / lucene-interval-fields-example.java
Created December 6, 2010 19:48
Example usage of the Lucene Interval Fields project.
// 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));
@robbywalker
robbywalker / hookshot-forwardInvocation.mm
Last active December 11, 2015 05:18
Simplified version of hookshot's forwardInvocation
- (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 {
+ (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));
}
NSString *newName = [NSString stringWithFormat:@"CCInstrumentedProxy_%s", class_getName(cls)];
Class subClass = objc_allocateClassPair([CCInstrumentedProxy class], [newName UTF8String], 0);
classes[cls] = subClass;
+ (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:));