Skip to content

Instantly share code, notes, and snippets.

@xeonqq
xeonqq / fsm_one_hot.v
Created June 12, 2023 07:46
fsm 4 locations, move left and right, one hot encoding
/*
* Do not change Module name
*/
module FSM(input clk, input [1:0] w, output [3:0] z);
localparam A=4'b0001, B=4'b0010, C=4'b0100, D=4'b1000;
reg [3:0] state, next_state;
initial state = A;
always @(posedge clk)

enter in a Docker container already running with a new TTY

docker exec -it [container-id] bash

Docker: Copying files from Docker container to host

docker cp :/file/path/within/container /host/path/target

How to stop a docker container which started with --restart=always

sudo docker update --restart=no <container_id> to update --restart flag of the container.

@xeonqq
xeonqq / bicycle_model.py
Created November 11, 2020 16:40
Simple demo of bicycle model
import matplotlib.pyplot as plt
from math import *
import numpy as np
import matplotlib.patches as patches
# reference https://dingyan89.medium.com/simple-understanding-of-kinematic-bicycle-model-81cac6420357
class Bicycle(object):
def __init__(self):
self._length =0.1 # m
self._width = 0.01
@xeonqq
xeonqq / MinPerimeterRectangle.py
Created July 7, 2017 14:26
MinPerimeterRectangle. Find the minimal perimeter of any rectangle whose area equals N.
from math import sqrt
def solution(N):
# write your code in Python 2.7
minperimeter = 2*(N + 1)
if N == 1:
return minperimeter
denominator = 2
@xeonqq
xeonqq / try_bluetooth_function.c
Created December 3, 2015 14:24
try to find the baudrate of the bluetooth module (not tested)
void tryBT()
{
delay(20000);
led_green_flash();
long baudrates[5] = {9600, 19200, 38400, 57600, 115200};
String inputString="";
for (int i =0; i< 5; i++)
{
DebugSerial.begin(baudrates[i]); // debug output
@xeonqq
xeonqq / Equi.py
Created September 8, 2015 13:34
Find an index in an array such that its prefix sum equals its suffix sum.
def solution(A):
# write your code in Python 2.7
s = []
tmp = 0
n = len(A)
for a in A:
tmp = a + tmp
s.append(tmp)
s.append(0)
@xeonqq
xeonqq / MaxDoubleSliceSum.py
Created September 6, 2015 22:34
Find the maximal sum of any double slice.
def solution(A):
# write your code in Python 2.7
n = len(A)
if n <=3:
return 0
leftMax = [0]*n
rightMax = [0]*n
for i in xrange(2,n-1):
leftMax[i] = max(0,leftMax[i-1]+A[i-1])
for i in xrange(n-3,0,-1):
@xeonqq
xeonqq / NumberOfDiscIntersections.py
Created September 4, 2015 16:13
Compute the number of intersections in a sequence of discs. follows the solution described here: http://stackoverflow.com/questions/4801242/algorithm-to-calculate-number-of-intersecting-discs
def solution(A):
# write your code in Python 2.7
B = []
for i in xrange(len(A)):
B.append((i-A[i],i+A[i]))
B = sorted(B, key=lambda x:x[0])
cnt = 0
for i in xrange(len(A)-1):
right = B[i][1]
start = i+1
@xeonqq
xeonqq / Triangle.py
Created September 4, 2015 11:57
Determine whether a triangle can be built from a given set of edges.
def solution(A):
# write your code in Python 2.7
A.sort()
for i in xrange(len(A)-2):
if (((A[i] + A[i+1]) > A[i+2]) and ((A[i+2] + A[i+1]) > A[i]) and ((A[i+2] + A[i]) > A[i+1])):
return 1
return 0
@xeonqq
xeonqq / MaxNonoverlappingSegments.py
Created September 1, 2015 13:52
Codility MaxNonoverlappingSegments: Find a maximal set of non-overlapping segments.
def solution(A, B):
# write your code in Python 2.7
pre = -1
N = len(A)
ans=0
for i in xrange(N):
if A[i] > pre:
ans+=1
pre = B[i]
return ans