Skip to content

Instantly share code, notes, and snippets.

@majk-p
majk-p / dynamic_tsp.py
Created November 17, 2015 16:14 — forked from mlalevic/dynamic_tsp.py
Simple Python implementation of dynamic programming algorithm for the Traveling salesman problem
def solve_tsp_dynamic(points):
#calc all lengths
all_distances = [[length(x,y) for y in points] for x in points]
#initial value - just distance from 0 to every other point + keep the track of edges
A = {(frozenset([0, idx+1]), idx+1): (dist, [0,idx+1]) for idx,dist in enumerate(all_distances[0][1:])}
cnt = len(points)
for m in range(2, cnt):
B = {}
for S in [frozenset(C) | {0} for C in itertools.combinations(range(1, cnt), m)]:
for j in S - {0}:
/*
* robotNav.js
*
* The green key is located in a slightly more
* complicated room. You'll need to get the robot
* past these obstacles.
*/
function startLevel(map) {
// Hint: you can press R or 5 to "rest" and not move the
/*
* robotMaze.js
*
* The blue key is inside a labyrinth, and extracting
* it will not be easy.
*
* It's a good thing that you're a AI expert, or
* we would have to leave empty-handed.
*/
/**************************
* exceptionalCrossing.js *
**************************
*
* Sorry, old friend, but I'm afraid I can't share
* co-authorship on this paper. You've done a very
* good job getting this Algorithm for me. The bit
* with the keys was especially clever! I wouldn't
* have thought of it myself. But then, of course,
* that's why you were here in the first place.
@majk-p
majk-p / prolog.pl
Created May 16, 2016 20:51
Prolog 1
% Contains
cont([], _):- write(false).
cont([E|_], E) :- write(true).
cont([_|T], E) :- cont(T, E).
:- cont([1,2,3], 2).
:- nl.
% Appearance counter
@majk-p
majk-p / prolog.pl
Created May 16, 2016 21:48
Prolog 2
% Contains
cont([], _, Result):- Result is 0.
cont([E|_], E, Result) :- Result is 1.
cont([_|T], E, Result) :- cont(T, E, Result).
:- cont([1,2,3], 3, R), write(R), nl.
% Appearance counter
cnt(_, [], Sum, Sum).
@majk-p
majk-p / prolog.pl
Created May 16, 2016 23:04
Prolog 3
sasiad(a, b).
sasiad(b, c).
sasiad(b, i).
sasiad(c, d).
sasiad(d, c).
sasiad(d, e).
sasiad(e, f).
sasiad(f, g).
sasiad(g, i).
sasiad(g, h).
### Keybase proof
I hereby claim:
* I am majk-p on github.
* I am majkp (https://keybase.io/majkp) on keybase.
* I have a public key whose fingerprint is 62A0 98DA 2951 259A 39D7 3357 69BF E94A 74B8 A92C
To claim this, I am signing this object:
@majk-p
majk-p / unsafeorderline.sc
Created July 14, 2022 19:09
unsafeorderline.sc
//> using scala "2"
case class UnsafeOrderLine(product: String, quantity: Int)
object UnsafeOrderLine {
def safeApply(product: String, quantity: Int): UnsafeOrderLine = {
if (product.isEmpty())
throw new RuntimeException("Product is empty")
else if (quantity <= 0)
throw new RuntimeException("Quantity lower than 1")
else
//> using scala "2"
//> using lib "org.typelevel::cats-core:2.8.0"
//> using lib "eu.timepit::refined:0.10.1"
//> using lib "io.circe::circe-core:0.14.2"
//> using lib "io.circe::circe-generic:0.14.2"
//> using lib "io.circe::circe-refined:0.14.2"
import cats.data.NonEmptyList
import eu.timepit.refined.auto._