Skip to content

Instantly share code, notes, and snippets.

String::reverse = ->
size = @length + 1
result = ""
while size -= 1
result += @[size - 1]
result
String::reversable = ->
@reverse() == @toString()
# to memorize the collatz sequence length for a specific number
memory = []
Number::collatz = ->
if @ % 2 == 0
@ / 2
else
3 * @ + 1
Number::collatzSequenceLength = ->
#!/bin/env ruby
# -*- coding: utf-8 -*-
class MatrixCalc
def initialize(matrix)
@matrix = matrix
end
def input
@input ||= traverse
@nevill
nevill / astro.js
Created October 8, 2013 13:15
获得星座名称
// 输入 m: 月, d: 天
// 例如 getAstro(5, 1) 返回 `金牛`
function getAstro(m, d) {
return "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯".substr(m * 2 - (d < "102123444543".charAt(m - 1) - -19) * 2, 2);
}
@nevill
nevill / spread.js
Created October 16, 2013 06:47
Try to understand how to use `spread` in Q - a tool to compose asynchronous promises.
var Q = require('q');
var util = require('util');
function randomDelayedCall(func) {
var delay = Math.floor(Math.random() * (1200 - 100) + 100);
setTimeout(func, delay);
}
function findUser(id, cb) {
var err = null;
@nevill
nevill / creation.js
Created December 11, 2013 05:54
A sample to use swagger-jack
module.exports = {
resourcePath: '/orgs',
apis: [{
path: '/orgs',
operations: [{
httpMethod: 'POST',
responseClass: 'void',
nickname: 'returnParams',
parameters: [{
dataType: 'Org',
@nevill
nevill / app.js
Last active August 29, 2015 13:56
An example of usage on swagger-jack, to validate against uploaded files
// To test with it, run with `curl`
// curl -F "photo=@/path/to/your/photo" localhost:3000/api/uploads
var express = require('express');
var swagger = require('swagger-jack');
var app = express();
app.use(express.logger('dev'))
.use(express.bodyParser())
.use(express.methodOverride())
.use(swagger.generator(app, {
@nevill
nevill / filterDuplicated.js
Created May 19, 2014 03:48
Simpe test on how to strip out duplications from array in Javascript
var _ = require('underscore');
var ObjectID = require('mongodb').ObjectID;
var duplicatedIds = [];
var lengthOfDuplicatedIds = 10;
for (var i = 0; i < lengthOfDuplicatedIds; i++) {
duplicatedIds.push(new ObjectID());
}
var ArrToTest = [];
@nevill
nevill / Hijacked packets
Last active August 29, 2015 14:06
Baidu Hijack
11:29:35.387693 IP (tos 0x0, ttl 63, id 50801, offset 0, flags [DF], proto TCP (6), length 64)
116.2xx.x.x.53587 > 115.239.211.110.80: Flags [S], cksum 0x5f8f (correct), seq 895062881, win 65535, options [mss 1452,nop,wscale 4,nop,nop,TS val 783494939 ecr 0,sackOK,eol], length 0
0x0000: 4500 0040 c671 4000 3f06 95fc 74ea 2302 E..@.q@.?...t.#.
0x0010: 73ef d36e d153 0050 3559 9361 0000 0000 s..n.S.P5Y.a....
0x0020: b002 ffff 5f8f 0000 0204 05ac 0103 0304 ...._...........
0x0030: 0101 080a 2eb3 2f1b 0000 0000 0402 0000 ....../.........
11:29:35.394412 IP (tos 0x0, ttl 55, id 50801, offset 0, flags [DF], proto TCP (6), length 64)
115.239.211.110.80 > 116.2xx.x.x.53587: Flags [S.], cksum 0x2bbb (correct), seq 117149092, ack 895062882, win 65535, options [mss 1440,nop,wscale 7,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,sackOK,eol], length 0
0x0000: 4500 0040 c671 4000 3706 9dfc 73ef d36e E..@.q@.7...s..n
PS1='\[\e[1;34m\]\u@\h \W \[\e[0;32m\]\$\[\e[0;39m\] '
if [ -f `brew --prefix`/etc/bash_completion ]; then
. `brew --prefix`/etc/bash_completion
PS1='\[\e[1;34m\]\u@\h \W\[\e[0;36m\]$(__git_ps1 "(%s)") \[\e[0;32m\]\$\[\e[0;39m\] '
# PS1='\u@\h \W$(__git_ps1 " (%s)")\$ '
GIT_PS1_SHOWDIRTYSTATE=true
fi
# bash setting