Skip to content

Instantly share code, notes, and snippets.

class BinaryTree:
def __init__(self, root):
self.root = Node(root)
def preorder(self, root):
if root is None:
return
print(root.data, end=" ,")
self.preorder(root.left)
self.preorder(root.right)
Bądź miły. Nie bądź arogancki. Prowadź ciekawą rozmowę, a nie przesłuchuj adwersarza. Proszę nie szydzić, także z reszty społeczności.
Posty powinny być coraz bardziej przemyślane i merytoryczne, a nie coraz mniej, w miarę jak temat staje się coraz bardziej szczegółowy.
Jeśli się z kimś nie zgadzasz, odpowiedz na argument, zamiast rzucać wyzwiskami, czy argumentami ad personam. "To jest idiotyczne; 1 + 1 to 2, a nie 3" można skrócić do "1 + 1 to 2, a nie 3".
Odpowiadaj na najmocniejszą wiarygodną interpretację tego, co ktoś mówi, a nie na słabszą, którą łatwiej skrytykować. Zakładaj dobrą wiarę
Unikaj flamu, niezwiązanych z tematem kontrowersji i ogólnych wątków
@lion137
lion137 / find_unpaired.py
Created December 15, 2019 17:51
xor to find unpaired
# python:
def find_unpaired(xs):
"""find unpaired element with array of 2n + 1 elements"""
for i in range(1, len(xs)):
xs[i] ^= xs[i - 1]
print(xs[len(xs) - 1])
# pseudocode:
fun findUnpaired(aList):
#N - length of input list
for i from 1 to N:
@lion137
lion137 / conversions.cs
Created October 27, 2019 22:31
Some lecture on C# basis
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp5
{
    class Program
    {
@lion137
lion137 / f_list.java
Created July 8, 2019 08:27
Functional list in Java - working proof of concept
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
interface List {
boolean is_empty();
int head();
List tail();
@lion137
lion137 / s_list.py
Created July 6, 2019 12:16
python list
class Nil:
def is_empty(self):
return True
def head(self):
raise Exception("Exception: Empty list")
def tail(self):
raise Exception("Exception: Empty list")
@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 {
@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 / 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 / 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))