Skip to content

Instantly share code, notes, and snippets.

View ackintosh's full-sized avatar
🎯
Focusing

Akihito Nakano ackintosh

🎯
Focusing
View GitHub Profile
@ackintosh
ackintosh / gist:5180548
Created March 17, 2013 07:59
Merge sort in PHP
<?php
function mergesort(&$array)
{
_mergesort($array, 0, count($array));
}
function _mergesort(&$array, $left, $right)
{
if ($right - $left <= 1) return;
@ackintosh
ackintosh / php.ini
Created February 24, 2013 06:31
Xdebug setting in MAMP
[xdebug]
zend_extension="/Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
xdebug.remote_enable=On
xdebug.remote_host=“localhost”
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_autostart=Off
xdebug.profiler_enable = On
xdebug.collect_vars=on
xdebug.collect_params=4
@ackintosh
ackintosh / gist:4995567
Created February 20, 2013 13:29
PHPでちょっとしたレジ機能を作る
<?php
$Cart = new Cart();
$Cart->setSetMenu(new SetMenu1());
$Cart->add(new Hamburger(), 2)->add(new Popato(), 2)->add(new Drink(), 2);
printf("合計は %d になります。", $Cart->total());
/**
@ackintosh
ackintosh / gist:4770795
Created February 12, 2013 15:45
Selection sort in PHP
<?php
$array = range(1, 20);
shuffle($array);
var_dump($array);
var_dump(selectionsort($array));
function selectionsort($array)
{
$size = count($array);
@ackintosh
ackintosh / gist:4770442
Last active December 12, 2015 12:09
Bubble sort in PHP
<?php
$array = range(1, 20);
shuffle($array);
var_dump($array);
var_dump(bubblesort($array));
function bubblesort($array)
{
$size = count($array);
@ackintosh
ackintosh / gist:4770220
Created February 12, 2013 14:29
Binary tree in PHP
<?php
class TreeNode
{
public $value;
public $left;
public $right;
public function __construct($value)
{
$this->value = $value;
@ackintosh
ackintosh / gist:4761915
Last active December 12, 2015 10:49
アルゴリズムを学ぼう p48 の問題をPHPで。
<?php
$exec_num = 1000;
$ar = new SplFixedArray(10);
$cnt = 0;
for ($i = 0; $i < $exec_num; $i++) {
if ($ar->valid() === false) {
// 配列作成 & コピー
$ar = getArray($ar);
}
@ackintosh
ackintosh / binary_search.php
Created February 12, 2013 10:35
Linear search and Binary search in PHP
<?php
function contains($v, Array $vs)
{
if (count($vs) === 0) return false;
$left = 0;
$right = count($vs) - 1;
while (($left + 1) < $right) {
$mid = $left + ($right - $left) / 2;
if ($v < $vs[$mid]) {
@ackintosh
ackintosh / gist:4760829
Last active December 12, 2015 10:39
a の k乗 を m で割った余りを求める関数 powmod(a, k, m) をPHPで実装
<?php
// 愚直なパターン
function powmod($a, $k, $m)
{
$t = 1;
foreach (range(1, $k) as $r) {
$t = $t * $a;
}
return $t % $m;
}
@ackintosh
ackintosh / gist:4701126
Created February 3, 2013 10:00
Observer pattern in Ruby
class Payroll
def update(changed_employee)
puts("I cut a check for #{changed_employee.name}")
puts("His salary is now #{changed_employee.salary}")
end
end
class TaxMan
def update(changed_employee)
puts("I will send a new invoice to #{changed_employee.name}")