Skip to content

Instantly share code, notes, and snippets.

@am-
am- / docker-compose-start.php
Created March 29, 2018 07:04
Script to update the hosts file according to the container names in a docker-compose setup
<?php
define('HOST_SUFFIX', '.local');
/**
* @return string[]
*/
function determineIpAddresses()
{
$lines = array_slice(explode(PHP_EOL, shell_exec('docker-compose ps')), 2, -1);
@am-
am- / set-hostname-for-docker-container.sh
Created April 7, 2017 08:08
Dynamically determine the IP address of a Docker container and change the /etc/hosts accordingly
#!/bin/sh
# Assumption 1: $CONTAINER and $HOST are properly set
# Assumption 2: The host is already set in /etc/hosts.
# Assumption 3: In /etc/hosts, the IP address and host are separated by a tab
IP=`docker exec -t $CONTAINER ip addr | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | grep 172`
sudo sed -i "/$HOST/s/[^\t]*/$IP/" /etc/hosts
<?php
$data = [
'name' => ['a', 'b', 'c'],
'email' => ['a@bc.de', 'x@yz.de', 'barde@bar.de'],
];
$output = [];
for($i = 0; $i < count($data['name']); $i++) {
$output[] = array_combine(array_keys($data), array_column($data, $i));
}
<?php
class AnswerAdder
{
private function addTheAnswer($x)
{
return 42 + $x;
}
}
<?php
function computationWithAssumedKeys(array $array, array $necessaryKeys) {
$missingKeys = array_diff($necessaryKeys, array_keys($array));
if (!empty($missingKeys)) {
$message = 'Computation failed due to missing key(s): ' . implode(', ', $missingKeys);
throw new \RuntimeException($message);
}
// compute...
}
@am-
am- / lottery-maximization.hs
Last active October 28, 2016 07:15
Searches all combinations of "6 in 49" for the smallest payout given lottery tickets.
import Control.Applicative ((<$>))
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.Function (on)
import Data.List (foldl', minimumBy, tails)
import Data.Maybe (fromMaybe)
import Data.Text (lines, splitOn, pack, unpack)
import Data.Text.IO (readFile)
import Prelude hiding (lines, readFile)
@am-
am- / many-search-trie.php
Last active October 22, 2017 08:13
Searching for many strings in one string using Tries
<?php
function mkTrie($elements, $all = true) {
$trie = array('' => false);
if(!$all) {
$tree = array('' => array('parent' => null, 'successors' => array()));
}
foreach($elements as $element) {
$key = '';