Skip to content

Instantly share code, notes, and snippets.

View sword-jin's full-sized avatar

Sword sword-jin

View GitHub Profile
@sword-jin
sword-jin / main.exs
Last active June 16, 2016 06:06
elixir exercise
# function 2
func_2 = fn
0, 0, _ -> "FizzBuzz"
0, _, _ -> "Fizz"
_, 0, _ -> "Buzz"
_, _, n -> n
end
IO.puts func_2.(1,1,1)
# Your init script
#
# Atom will evaluate this file each time a new window is opened. It is run
# after packages are loaded/activated and after the previous editor state
# has been restored.
#
# An example hack to log to the console when each text editor is saved.
#
# atom.workspace.observeTextEditors (editor) ->
# editor.onDidSave ->
@sword-jin
sword-jin / index.php
Last active June 10, 2016 10:01
Generator
<?php
// basic
function _genetator () {
yield "one";
yield "two";
yield "three";
}
foreach (_genetator() as $value) {
@sword-jin
sword-jin / createStore.js
Created May 1, 2016 15:18
redux createStore code
const createStore = (reducer) => {
let state;
let listeners = [];
const getState = () => state;
const dispatch = action => {
state = reducer(state, action);
listeners.forEach(l => l());
}
@sword-jin
sword-jin / .babelrc
Created April 29, 2016 16:40
React webpack.config.js
{
"presets": [
"es2015",
"react"
]
}
@sword-jin
sword-jin / main.cpp
Last active April 26, 2016 14:48
c++ basic templates
// Enumerations
enum Day { SUN, MON, TUE, WED, THU, FRI, SAT };
enum Mood { HAPPY = 3, SAD = 1, ANXIOUS = 4, SLEEPY = 2 };
Day today = THU; // today may be any of MON . . . SAT
Mood myMood = SLEEPY; // myMood may be HAPPY, . . ., SLEEPY
// pointers
char ch = ’Q’;
char* p = &ch; // p holds the address of ch
cout << *p; // outputs the character ’Q’
@sword-jin
sword-jin / index.js
Created April 26, 2016 07:27
Using php make a tiny is a joke.
<?php
// php make compiler is a joke.
class Compiler
{
// `tokens` 数组用于存所有的 token,待会我们会把 token 一个个 push 进去
protected $tokens = [];
// `current` 变量用于存当前处理到哪里了, 就像手指指着纸上我们处理到哪个位置了
protected $current;
@sword-jin
sword-jin / readme.md
Created April 20, 2016 14:14
x1,y1,x2,y2....最大距离

You are given a set of points on the Cartesian plane. Consider the distance between two points as the maximum difference of their coordinates. For example, the distance between points (1, 2) and (4, 6) is equal to max(|4 - 1|, |6 - 2|) = 4.

Given a set of points, find the pair with the largest distance and return the value of their distance.

Example

For A = [7, 6, 6, 8, 1, 2, 8, 6], the output should be

largestDistance(A) = 7.

@sword-jin
sword-jin / readme.md
Last active June 26, 2016 23:39
红路灯撞车

Consider an Uber-City where the only passenger vehicles on the roads are self-driving Uber cars. Since all the cars are self-driving, you could synchronize the car speeds in such a way that traffic lights would be obsolete.

As a first step you need to implement an algorithm that would check whether removing traffic lights at a given intersection of two one-way roads will lead to a car crash.

For each road you know when a car will approach the crossroads. You also know how long it takes to cross the crossroads.

Example

@sword-jin
sword-jin / perfectCity.php
Created April 20, 2016 10:15
Uber prefect city
<?php
function perfectCity($departure, $destination) {
$length = 0;
if ((int) $destination[1] == (int) $departure[1]) {
$length += min([1 - $departure[1] - (int) $departure[1] + 1 - $destination[1] - (int) $destination[1], $destination[1] - (int) $destination[1] + $departure[1] - (int) $departure[1]]);
} else {
$length += $destination[1] - $departure[1];
}