Skip to content

Instantly share code, notes, and snippets.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lion137
lion137 / bit_hacks.go
Last active February 11, 2017 23:20
Golang tests
//Golang optimizatoin tricks
// copyleft by lion137
// https://lion137.blogspot.co.uk/2017/02/bit-hacks-in-go.html
func average(x uint32, y uint32) uint32 {
return (x & y) + ((x ^ y) >> 1)
}
func iexp(x uint32, n uint32) uint32 {
if (n == 0) {
@lion137
lion137 / ret_test.java
Last active June 5, 2017 08:55
return statement problem java
public BigInteger evaluateParseTreeBigInt(BinaryTree tree){
BinaryTree<T> leftT = tree.left;
BinaryTree<T> righT = tree.right;
boolean p = (leftT != null) && (righT != null);
if (p){
//cos tam, return jakis bigint
}
else return //jakis biginteger
}//IntelliJ IDEA sie pluje, ze brakuje return statemnt, sugeruje po else, ale to rozwala cala logike
@lion137
lion137 / afs.py
Last active August 18, 2017 00:06
Blog
def afs(g, s):
bag = data_structure()
bag.add(s)
while bag is not empty:
tile = bag.pop()
if tile is not marked:
mark(tile)
for x in adj_list(tile):
bag.add(x)
public static void cond_changer(long time_pause){
long start = System.currentTimeMillis();
System.out.println("start in cond_change");
while (System.currentTimeMillis() - start < time_pause * 1000) {
}
Main.condition = () -> true;
}
public static Callable<Boolean> cond_change() {
# Infinite primes generator and quantum article check:
# https://www.quantamagazine.org/mathematicians-discover-prime-conspiracy-20160313/
from tests import miller_rabin
def primes_generator():
i = 2
while True:
if miller_rabin(i):
yield i
@lion137
lion137 / find_par.py
Last active October 21, 2018 08:36
some parameter finder
def find_median(xs):
ys = sorted(xs)
if not len(xs) % 2 == 0:
return ys[len(ys) // 2]
else:
return (ys[(len(ys) // 2)] + ys[(len(ys) // 2) - 1]) / 2
def make_sum(xs, a):
return sum(map(lambda x: abs(x - a), xs))
@lion137
lion137 / newton_method.c
Created November 5, 2018 18:46
C, finding root, Newton Method
// implementation of Newton Method (Simple Iteration)
// 2017, Copyleft lion137
// Znajduje pierwiastek wielomianu Metodą Newtona:
// https://en.wikipedia.org/wiki/Newton%27s_method czyli:
// x0 = wartość poczatkowa (zgadywana - nie powinna za daleko odbiegać od pierwiastka - tu jest problem tej metody,
// ale wszystkie mają podobne problemy, patrz Wikipedia: https://en.wikipedia.org/wiki/Root-finding_algorithm , chociaz
// 100 dalej nie jest za daleko od pierwiastka -0.169... patrz przykład w main)
// i iteracja:
// x1 = x0 - f(x0) / f'(x0)
// x0 = x1 -> podstawienie
@lion137
lion137 / optional.cpp
Last active October 30, 2019 08:54
Optional in C++ as Kleisli Category
#include <iostream>
#include <cmath>
#include <vector>
/* More info and explanation here: https://lion137.blogspot.com/2019/01/kleisli-category-by-example.html */
template<class A> class optional {
bool _isValid;
A _value;
public:
optional(): _isValid(false) {}
@lion137
lion137 / simple_list.scala
Created July 6, 2019 11:37
functional list
sealed trait MyList[+A]
case object Nil extends MyList[Nothing]
case class Cons[+A] (head: A, tail: MyList[A]) extends MyList[A]
object HelloWorld {