Skip to content

Instantly share code, notes, and snippets.

View sword-jin's full-sized avatar
:atom:

Sword sword-jin

:atom:
View GitHub Profile
@sword-jin
sword-jin / transpose.php
Created April 11, 2016 10:33
Laravel collection transpose
// key will be 0,1,2,...
Collection::macro('transpose', function() {
$items = array_map(function(...$items) {
return $items;
}, ...$this->values());
return new self($items);
});
// transpose(['name', 'email'])
@sword-jin
sword-jin / permutations.php
Last active March 31, 2016 03:36
Get all permutations.
<?php
function permutations(array $array) {
if (count($array) == 0) {
return [];
} elseif (count($array) == 1) {
return $array;
} else {
$results = [];
for ($i = 0; $i < count($array); $i ++) {
@sword-jin
sword-jin / subpalindrome.py
Last active March 30, 2016 08:46
Subpalindrome
# --------------
# User Instructions
#
# Write a function, longest_subpalindrome_slice(text) that takes
# a string as input and returns the i and j indices that
# correspond to the beginning and end indices of the longest
# palindrome in the string.
#
# Grading Notes:
#
@sword-jin
sword-jin / floor_puzzle.py
Last active March 30, 2016 08:33
Floor Puzzle
#------------------
# User Instructions
#
# Hopper, Kay, Liskov, Perlis, and Ritchie live on
# different floors of a five-floor apartment building.
#
# Hopper does not live on the top floor.
# Kay does not live on the bottom floor.
# Liskov does not live on either the top or the bottom floor.
# Perlis lives on a higher floor than does Kay.
@sword-jin
sword-jin / formula.php
Last active March 30, 2016 08:21
Formula Calculate python php
<?php
@sword-jin
sword-jin / SomeRequest.php
Created March 11, 2016 01:11
One request for save and update.
public function rules()
{
$user = User::find($this->users);
switch($this->method())
{
case 'GET':
case 'DELETE':
{
return [];
@sword-jin
sword-jin / index.cpp
Last active March 8, 2016 11:31
Linked list.
#include <cstdio>
#include <stdlib.h>
using namespace std;
struct Node
{
int data;
Node* next;
};
@sword-jin
sword-jin / index.js
Created March 3, 2016 11:26
求解 next permutation 代码
Array.prototype.nextPermutation = function() {
var i = this.length - 1;
while (i > 0 && this[i - 1] >= this[i]) {
i --;
}
if (i <= 0) return false;
var j = this.length - 1;
while (this[j] <= this[i - 1]) {
j --;
@sword-jin
sword-jin / index.php
Created January 16, 2016 06:49
Slim3.1 基本使用
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
require __DIR__ . '/vendor/autoload.php';
$container = new Slim\Container;
$container['filesystem'] = function($container) {
@sword-jin
sword-jin / observer.js
Created January 7, 2016 13:38
JavaScript Observer pattern -- es5
define(function() {
var subscribers = {};
function subscribe (type, fn) {
if (! subscribers[type]) {
subscribers[type] = [];
}
if (subscribers[type].indexOf(fn) == -1) {
subscribers[type].push(fn);
}