Skip to content

Instantly share code, notes, and snippets.

View bmarcot's full-sized avatar

Benoit Marcot bmarcot

  • Trondheim
View GitHub Profile
@bmarcot
bmarcot / git-cheat-sheet.sh
Last active September 16, 2019 06:03
Git cheat sheet
## <...>: a mandatory parameter
## [...]: an optional parameter
# Compare two versions of a file
$ git diff HEAD^^ HEAD main.c
$ git diff HEAD~2 HEAD main.c
# Create a patch from a diff between two branches
$ git format-patch -n <master>..<develop>
const insertAt = (str, sub, pos) => {
return str.substring(0, pos) + sub + str.substring(pos);
}
console.log(insertAt("I want apple", " an", 6));
int spi_write_then_write(struct spi_device *spi,
const void *tx_buf_0, unsigned tx_len_0,
const void *tx_buf_1, unsigned tx_len_1)
{
struct spi_message message;
struct spi_transfer x[2];
spi_message_init(&message);
memset(x, 0, sizeof(x));
if (tx_len_0) {
# app/assets/javascripts/welcome.js.coffee
$ ->
$(document).on 'change', '#countries_select', (evt) ->
$.ajax 'update_cities',
type: 'GET'
dataType: 'script'
data: {
country_id: $("#countries_select option:selected").val()
}
@bmarcot
bmarcot / tsort.scala
Created July 30, 2013 11:04
Topological sort in Scala.
def tsort(gs: List[(Int, List[Int])]): List[Int] = {
gs match {
case g :: gs => {
if (g._2.isEmpty) g._1 :: tsort(gs.diff(List(g)).map(x => (x._1, x._2.diff(List(g._1)))))
else tsort(gs :+ g)
}
case _ => List()
}
}
@bmarcot
bmarcot / knapsack_problem.scala
Last active June 28, 2017 13:41
The Knapsack Problem, in Scala -- Keywords: dynamic programming, recursion, scala.
def knapsack_aux(x: (Int, Int), is: List[Int]): List[Int] = {
for {
w <- is.zip(is.take(x._1) ::: is.take(is.size - x._1).map(_ + x._2))
} yield math.max(w._1, w._2)
}
def knapsack_rec(xs: List[(Int, Int)], is: List[Int]): List[List[Int]] = {
xs match {
case x :: xs => knapsack_aux(x, is) :: knapsack_rec(xs, knapsack_aux(x, is))
case _ => Nil
trait Generator[+T] {
self =>
def generate: T
def map[S](f: T => S): Generator[S] = new Generator[S] {
def generate = f.apply(self.generate)
}
@bmarcot
bmarcot / atoiBase.scala
Last active December 21, 2015 11:18
atoi() for any base.
def hexToInt(s: String): Int = {
s.toList.map("0123456789abcdef".indexOf(_)).reduceLeft(_ * 16 + _)
}
def baseToInt(s: String, base: String): Int = {
s.toList.map(base.indexOf(_)).reduceLeft(_ * base.length + _)
}
def split[A](chars: List[A], n: Int): List[List[A]] = {
if (chars.isEmpty) List()
else chars.take(n) :: split[A](chars.drop(n), n)
}
def encrypt_rec(chars: List[List[Char]]): List[List[Char]] = {
if (chars.isEmpty) Nil
else {
val cs = chars.filter(_.isEmpty == false)
cs.map(_.head) :: encrypt_rec(cs.map(_.tail))
@bmarcot
bmarcot / problem_solving.scala
Last active December 20, 2015 07:08
Work in progress...
def tsort(gs: List[(Int, List[Int])]): List[Any] = {
if (gs.isEmpty) List()
else for {
g <- gs
if (g._2.isEmpty)
} yield g._1 :: tsort(gs.diff(List(g)).map(x => (x._1, x._2.diff(List(g._1)))))
} //> tsort: (gs: List[(Int, List[Int])])List[Any]
def tsort_aux(xs: List[Any]): List[List[Int]] = {
if (xs.isEmpty) List(List())