Skip to content

Instantly share code, notes, and snippets.

@kevinlebrun
kevinlebrun / README.md
Last active December 26, 2015 16:29
Maximum Contiguous Sum in Python

Maximum Contiguous Sum

The problem was to find the maximum contiguous sum of series of numbers.

$ pip install pytest

$ py.test --doctest-modules

@kevinlebrun
kevinlebrun / talk-to-bc.sh
Created August 7, 2013 12:10
Bash corpoc
#!/bin/bash
{ coproc bc_proc { bc; read; } >&3; } 3>&1
echo "1 + 2" >&${bc_proc[1]}
echo "2 + 5" >&${bc_proc[1]}
@kevinlebrun
kevinlebrun / pf
Last active December 19, 2015 06:19
Prime Factors Kata in Bash
#!/bin/bash
TEST=${TEST:+'test'}
function prime_factors_of {
local number=$1
for (( divisor=2; number > 1; divisor++ )); do
for (( ; number % divisor == 0; number /= divisor )); do
prime_factors=("${prime_factors[@]}" $divisor)
@kevinlebrun
kevinlebrun / uchr.php
Created December 11, 2012 09:32
Some ways to transform unicode to char in PHP
<?php
function uchrA($str) {
return html_entity_decode(preg_replace('/\\\u([\da-fA-F]{4})/', '&#x\1;', $str));
}
function uchrB($str) {
return utf8_decode(json_decode(sprintf('"%s"', $str)));
}
@kevinlebrun
kevinlebrun / create_user_kata.php
Created December 1, 2012 11:10
Training with PDO and SQL
<?php
$db = new PDO('mysql:unix_socket=/tmp/mysql.sock;dbname=mybdd', 'root', 'root');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/**
CREATE SCHEMA `mybdd`;
CREATE TABLE `users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(255) NOT NULL,
@kevinlebrun
kevinlebrun / foundations_principles_development.mkd
Last active September 28, 2018 02:51
Principles of Object Oriented Design

Principles

  • DRY ou Don't Repeat Yourself
  • KISS ou Keep It Simple, Stupid!
  • YAGNI ou You Ain't Gonna Need It!
  • SOLID
  • STUPID
  • Law Of Demeter
  • Tell, don't ask!
@kevinlebrun
kevinlebrun / SimplexProbe.php
Created March 15, 2012 14:21
Service status checking with port scanning
#!/usr/bin/php -q
<?php
function check_socket($host, $port)
{
$conn = @fsockopen($host, $port, $errno, $errstr, 2);
if ($conn) {
fclose($conn);
return true;
}
@kevinlebrun
kevinlebrun / telnet.js
Created February 29, 2012 09:52
Node telnet and local REPL
#!/usr/bin/env node
var net = require('net'),
repl = require('repl');
var mood = function () {
var m = [ "^__^", "-___-;", ">.<", "<_>" ];
return m[Math.floor(Math.random() * m.length)];
};
@kevinlebrun
kevinlebrun / curl_post_foobar.php
Created September 9, 2011 11:49
CURL POST Json
#!/usr/bin/env php
<?php
$url = $argv[1];
$data = array('foo' => 'bar');
echo '>>>>> request : ' . $url . PHP_EOL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
@kevinlebrun
kevinlebrun / network_helpers.php
Created September 9, 2011 11:12
PHP Network Helpers
<?php
function is_in_subnet($subnet, $ip)
{
list($subnet_ip, $subnet_mask) = explode('/', $subnet);
$begin = (ip2long($subnet_ip) & ip2long($subnet_mask)) + 1;
$end = (ip2long($subnet_ip) | (~ip2long($subnet_mask))) + 1;
$ip = ip2long($ip);
return ($ip >= $begin && $ip <= $end);
}