Skip to content

Instantly share code, notes, and snippets.

@chancancode
chancancode / gist:1744276
Created February 5, 2012 09:07
Sequence Slicing
Sequence Slicing
https://www.facebook.com/hackercup/problems.php?pid=317343604974808&round=154897681286317
Let S be a sequence of N natural numbers. We can define an infinite sequence MS in the
following way: MS[k] = S[k mod N] + N * floor(k / N). Where k is a zero based index.
For example if the sequence S is {2, 1, 3} then MS would be {2, 1, 3, 5, 4, 6, 8, 7, 9,
11, 10, 12...}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define NUM_FNS 4
typedef int (*fn_ptr)(const char *);
int to_upper(const char *str)
@chancancode
chancancode / gist:2498906
Created April 26, 2012 11:19
Get real GCC
0. https://github.com/kennethreitz/osx-gcc-installer/downloads
1. Go to https://developer.apple.com/downloads/index.action
2. Login with a developer account, or create one
3. Download the latest “Command Line Tools for XCode”
4. Mount the dmg and run the installer
module MyModule
def clean_options
# Just an example helper method
# Don't want this to pollute the AR::Base namespace
end
def do_other_stuff
# Another helper method
end
key, type = key.split(/(?<!:):(?!:)/)
key.gsub('::',':')
@chancancode
chancancode / gist:2830878
Created May 29, 2012 21:33
Ruby time to .NET ticks
class Time
# See http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx
TICKS_SINCE_EPOCH = Time.utc(0001,01,01).to_i * 10000000
def ticks
to_i * 10000000 + nsec / 100 - TICKS_SINCE_EPOCH
end
end
@chancancode
chancancode / gist:3046772
Created July 4, 2012 11:05
JSON Matching
class MyTestCase < MiniTest::Unit::TestCase
# ...
WILDCARD_MATCHER = Object.new
def WILDCARD_MATCHER.==(other)
true
end
def WILDCARD_MATCHER.is_a?(klass)
@chancancode
chancancode / gist:3192023
Created July 28, 2012 06:04
ProTip(tm): SVN over github
mbp:godfrey t $ svn co https://github.com/chancancode/json_expressions.git
Error validating server certificate for 'https://github.com:443':
- The certificate is not issued by a trusted authority. Use the
fingerprint to validate the certificate manually!
Certificate information:
- Hostname: github.com
- Valid: from Fri, 27 May 2011 00:00:00 GMT until Mon, 29 Jul 2013 12:00:00 GMT
- Issuer: www.digicert.com, DigiCert Inc, US
- Fingerprint: ce:67:99:25:2c:ac:78:12:7d:94:b5:62:2c:31:c5:16:a6:34:73:53
(R)eject, accept (t)emporarily or accept (p)ermanently? p
@chancancode
chancancode / 0.rb
Created September 4, 2012 19:21
JsonExpression capturing
mbp:godfrey json_expressions [master] $ irb -Ilib
>> require 'json_expressions/matcher'
=> true
>> m = JsonExpressions::Matcher.new({a: :capture_me})
=> {:a=>:capture_me}
>> require 'json'
=> true
>> m =~ JSON.parse('{"a": 123}')
=> true
>> m.captures
@chancancode
chancancode / minitest_helpers.rb
Created September 7, 2012 22:35
Factory Test
class MiniTest::Spec
def ensure_valid(factory)
factory.valid? ? factory : skip
end
end