Skip to content

Instantly share code, notes, and snippets.

View kanglicheng's full-sized avatar
🦜
LLMs ftw

Stephen Cheng kanglicheng

🦜
LLMs ftw
View GitHub Profile
@jasonsperske
jasonsperske / FencePainter.js
Last active February 3, 2018 03:43
Another Google technical interview question that left me stumped. "Write an algorithm that counts the number of ways you can paint a fence with N posts using K colors such that no more than 2 adjacent fence posts are painted with the same color". Here is a recursive approach:
var paint = function (posts, colors, display) {
var painter = function (posts, colors, fence, count, display) {
var color, total = count;
if (posts === 0) {
//Would you like to see them? Pass a display function
if(display) {
display(fence);
}
return 1;
} else {
@Wilfred
Wilfred / flatten.py
Last active December 20, 2020 15:06
flatten an arbitrarily nested list in Python
from copy import deepcopy
def flatten_list(nested_list):
"""Flatten an arbitrarily nested list, without recursion (to avoid
stack overflows). Returns a new list, the original list is unchanged.
>> list(flatten_list([1, 2, 3, [4], [], [[[[[[[[[5]]]]]]]]]]))
[1, 2, 3, 4, 5]
>> list(flatten_list([[1, 2], 3]))
[1, 2, 3]
@chrisdarroch
chrisdarroch / idea
Created October 17, 2013 03:40
Open a project in IntelliJ IDEA from your command line!
#!/bin/sh
# check for where the latest version of IDEA is installed
IDEA=`ls -1d /Applications/IntelliJ\ * | tail -n1`
wd=`pwd`
# were we given a directory?
if [ -d "$1" ]; then
# echo "checking for things in the working dir given"
wd=`ls -1d "$1" | head -n1`
@elgalu
elgalu / a-drip-of-javascript-quiz.js
Created September 4, 2013 23:10
A Drip of JavaScript - book notes / quizzes
// Explain what the Function.prototype.bind method allows.
// Use properly to fix this TypeError: Illegal invocation
var myLog = console.log;
myLog("hola");
=>
// The bind method allows us to create a new function which is permanently bound to a given value of `this`
var myLog = console.log.bind(console);
myLog("hola"); //==> "hola"
// As a bonus, you can't override its `this` value using call or apply.
myLog.call(window, "hello"); // will be ignored and will keep bound to console.log()
@jboulhous
jboulhous / push.md
Created July 16, 2013 11:37
Push to github from cloud9

Add a git remote in the Cloud9 console. Should look like this (replace the git url with your repo url):

git remote add origin [email protected]:C9Support/testPush.git 

Add files and commit them:

git add . 
git commit -m "First commit"

Push to github:

@tommeagher
tommeagher / edgelist.py
Last active February 11, 2018 19:21
Create an edge list in Python.
mylist = ['john', 'paul', 'george', 'ringo', 'matthew', 'mark', 'luke']
resultlist = []
for person in mylist:
myindex = mylist.index(person)
newlist = mylist[:myindex]+mylist[myindex+1:] #make a new temp list without the person in it
for item in newlist:
mytuple = (person, item)
backtuple = (item, person)
@reinholdsson
reinholdsson / readme.md
Last active February 12, 2018 08:28
Programming Links
@jameskbride
jameskbride / babysitter_kata
Created April 29, 2013 16:23
Babysitter Kata
Babysitter Kata
Background
----------
This kata simulates a babysitter working and getting paid for one night. The rules are pretty straight forward:
The babysitter
- starts no earlier than 5:00PM
- leaves no later than 4:00AM
- gets paid $12/hour from start-time to bedtime
@jarek-przygodzki
jarek-przygodzki / cycle-finder.groovy
Created April 15, 2013 18:58
Finding cycles in a directed graph using DFS and back edges
interface DfsVisitor {
void preorder(v)
void postorder(v)
void beforeChild(v)
void afterChild(v)
void skipChild(v)
@1st
1st / tests_for_toptal_on_codility.py
Last active May 19, 2022 19:58
My answers for tests on http://codility.com that I passed for company http://toptal.com I use Python language to solve problems.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Test that I passed on codility.com for TopTal company
#
# Task #1
def binary_gap(N):