Skip to content

Instantly share code, notes, and snippets.

View zvonicek's full-sized avatar
:octocat:

Petr Zvoníček zvonicek

:octocat:
View GitHub Profile
@zvonicek
zvonicek / gist:a91561169a99edf244a3
Last active August 29, 2015 14:01
isShuffle dynamic programming
def isShuffle2(x,y,z):
m = len(x)
n = len(y)
r = len(z)
S = {}
#inicializace pole
for i in range(-1,r+1):
for j in range(-1,r+1):
@zvonicek
zvonicek / truty_filter.rb
Last active August 29, 2015 14:12
Truty filter for Liquid
require 'truty'
module Jekyll
module TrutyFilter
def truty(input)
Truty.fix(input, :czech)
end
end
end
def is_surprising(seq, d):
subseq = set()
collisions = 0
for i in range(0, len(seq)-d-1):
word = (seq[i], seq[i+d+1])
if word in subseq:
collisions += 1
else:
subseq.add(word)
@zvonicek
zvonicek / transition.m
Last active September 29, 2015 12:32
UINavigationBar styling between UINavigationController transitions
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_navbar"] forBarMetrics:UIBarMetricsDefault];
} completion:nil];
}
@zvonicek
zvonicek / ServiceHolder.swift
Last active November 23, 2016 14:27
ServiceHolder
import Foundation
protocol Service {
init()
}
class ServiceHolder {
private var servicesDictionary: [String: Service] = [:]
init(services: [Service.Type]? = nil) {
import UIKit
protocol Coordinator {
/// Triggers navigation to the corresponding controller
func start()
/// Stops corresponding controller and returns back to previous one
func stop()
/// Called when segue navigation form corresponding controller to different controller is about to start and should handle this navigation
@zvonicek
zvonicek / Debounce.swift
Last active October 28, 2017 09:47
Will only execute the wrapped function if `delay` has passed without this function being called.
func debounce<A>(delay:Int, queue:DispatchQueue, action: @escaping ((A)->())) -> (A)->() {
var lastFireTime = DispatchTime.now()
let dispatchDelay = DispatchTimeInterval.milliseconds(delay)
return { state in
lastFireTime = DispatchTime.now()
let dispatchTime: DispatchTime = DispatchTime.now() + dispatchDelay
queue.asyncAfter(deadline: dispatchTime, execute: {
let when: DispatchTime = lastFireTime + dispatchDelay
let now = DispatchTime.now()
@zvonicek
zvonicek / Throttle.swift
Last active October 28, 2017 09:49
Will throttle the execution to once in every `delay` seconds.
func throttle<A>(delay:Int, queue:DispatchQueue, action: @escaping ((A)->())) -> (A)->() {
var lastFireTime = DispatchTime.now()
let dispatchDelay = DispatchTimeInterval.milliseconds(delay)
return { state in
let dispatchTime: DispatchTime = DispatchTime.now() + dispatchDelay
queue.asyncAfter(deadline: dispatchTime, execute: {
let when: DispatchTime = lastFireTime + dispatchDelay
let now = DispatchTime.now()
if now.rawValue >= when.rawValue {
extension UIViewController {
public func dch_checkDeallocation(afterDelay delay: TimeInterval = 2.0) {
let rootParentViewController = dch_rootParentViewController
// We don’t check `isBeingDismissed` simply on this view controller because it’s common
// to wrap a view controller in another view controller (e.g. in UINavigationController)
// and present the wrapping view controller instead.
if isMovingFromParentViewController || rootParentViewController.isBeingDismissed {
let type = type(of: self)
let disappearanceSource: String = isMovingFromParentViewController ? "removed from its parent" : "dismissed"
@zvonicek
zvonicek / TriangleBoxIntersect.py
Last active February 27, 2024 14:08
Triangle/AABB (Triangle-Box) Intersection Test using the "Separating Axis Theorem" in Python
import numpy as np
# based on http://www.gamedev.net/topic/534655-aabb-triangleplane-intersection--distance-to-plane-is-incorrect-i-have-solved-it/
def intersects_box(triangle, box_center, box_extents):
X, Y, Z = 0, 1, 2
# Translate triangle as conceptually moving AABB to origin
v0 = triangle[0] - box_center
v1 = triangle[1] - box_center