Skip to content

Instantly share code, notes, and snippets.

View samidarko's full-sized avatar

Sami Darko samidarko

View GitHub Profile
@samidarko
samidarko / react-no-scroll-up.js
Last active March 7, 2016 08:32
how to preserve your window of scrolling up after rendering/updating a list in react
componentWillUpdate() {
this.scrollY = window.scrollY;
}
componentDidUpdate() {
if (this.scrollY) {
window.scroll(0, this.scrollY);
}
}
@samidarko
samidarko / countOccurrences.scala
Last active August 14, 2016 08:31
Count the number of occurrences in a string over consumption
def countOccurrences (s: String): String = s span (x => x == s.head) match {
case ("", "") => ""
case (fw, rest) => fw.length.toString + fw.head ++ countOccurrences(rest)
}
countOccurrences("") // ""
countOccurrences("aaa") // "3a"
countOccurrences("aaabbb") // "3a3b"
countOccurrences("aaabbbccchhhhhhddddhhhheee") // "3a3b3c6h4d4h3e"
@samidarko
samidarko / countOccurrences.hs
Created August 14, 2016 08:33
Count the number of occurrences in a string over consumption
import qualified Data.Char as Char
countOccurrences :: String -> String
countOccurrences "" = ""
countOccurrences s = let (fw, rest) = span (\x -> x == head s) s in (Char.intToDigit $ length $ fw) : (head s) : (countOccurrences rest)
countOccurrences "aaaaabbbbccccccaaaaaaa"
-- "5a4b6c7a"
@samidarko
samidarko / countOccurrences.py
Last active August 14, 2016 08:39
Count the number of occurrences in a string over consumption
# python 3
def span(string, char):
i = 0
while i < len(string) and string[i] == char:
i += 1
return string[0:i], string[i:]
def count_occurrences(string):
def solution(arr):
"""
Take a list of days and return optimal solution for transport fees where
Tickets are: day (2), weekly (7) and monthly (25) tickets
And month is bounded to 30 days, first day is 1
:param arr: list of days
:type arr: list
:return: transport month fees
:type: int
module Main exposing (..)
import Html exposing (Html)
import Html.App
import Widget
-- MODEL
from math import log
from matplotlib import pyplot as plt
import numpy as np
def fn_a(n):
return 64*n*log(n)
def fn_b(n):
return 8*n*n
def cities_by_distance(arr):
result = [None for _ in arr]
temp = [0 for _ in range(len(arr) - 1)]
for i, _ in enumerate(arr):
origin = i
pos = i
hop = 0
@samidarko
samidarko / InsertionSort.scala
Created August 30, 2016 15:27
Insertion sort imperative and functional
/**
* Created by vincentdupont on 26/8/16.
*/
import util.Random
object InsertionSort extends App {
def sortImp(a: Array[Int]) : Array[Int] = {
var key, i : Int = 0
for (j <- 1 until a.length) {
@samidarko
samidarko / countChange.hs
Created September 9, 2016 13:58
give all the ways of giving the change of an amount of money based on a list of coins
-- give all the ways of giving the change of an amount of money based on a list of coins
countChange :: Int -> [Int] -> Int
countChange money coins
| money < 0 = 0
| money == 0 = 1
| coins == [] = 0
| otherwise = countChange (money - (head coins)) coins + countChange money (tail coins)
-- countChange 10 [1, 5] == 3