Skip to content

Instantly share code, notes, and snippets.

@hungneox
hungneox / DI.py
Created August 12, 2021 15:01 — forked from elnygren/DI.py
Functional Dependency Injection in TypeScript
# dataclass saves the trouble of writing __init__ implementations
# frozen=True makes the class instances immutable (so no hidden mutable state)
@dataclass(frozen=True)
class UserConnector:
def get_user(id: str) -> User:
return User.objects.get(id=id)
@dataclass(frozen=True)
class OrgConnector:
def get_org(id: str) -> Org:
@hungneox
hungneox / solution.py
Last active July 18, 2021 13:30
Excel Sheet Column Title
# https://leetcode.com/problems/excel-sheet-column-title
class Solution:
def convertToTitle(self, columnNumber: int) -> str:
"""
Success
Details
Runtime: 28 ms, faster than 77.90% of Python3 online submissions for Excel Sheet Column Title.
Memory Usage: 14.1 MB, less than 70.63% of Python3 online submissions for Excel Sheet Column Title.
"""
if columnNumber <= 26:
@hungneox
hungneox / axiosInterceptor.js
Created March 30, 2021 08:32 — forked from VassilisPallas/axiosInterceptor.js
Axios interceptor that handled the error response and uses the cancel signal in every request
import axios from 'axios';
let signal = axios.CancelToken.source();
/**
* returns if error is being caused by axios cancel
* @function
* @returns {Boolean} - true if the error caused by canceling the request
*/
const areRequestsCanceled = err => {
@hungneox
hungneox / nodejs-tcp-example.js
Created March 19, 2021 22:09 — forked from tedmiston/nodejs-tcp-example.js
Node.js TCP client and server example
/*
In the node.js intro tutorial (http://nodejs.org/), they show a basic tcp
server, but for some reason omit a client connecting to it. I added an
example at the bottom.
Save the following server in example.js:
*/
var net = require('net');
@hungneox
hungneox / BinaryGap.py
Last active January 30, 2021 00:20
Codility BinaryGap - Python solution
def solution(N):
# write your code in Python 3.6
meet_one = False
count = 0
keep = []
while N:
if meet_one and N & 1 == 0:
count+=1
if N & 1:
@hungneox
hungneox / dijkstra.js
Created December 17, 2020 05:09 — forked from stella-yc/dijkstra.js
Dijkstra's Algorithm in Javascript using a weighted graph
const problem = {
start: {A: 5, B: 2},
A: {C: 4, D: 2},
B: {A: 8, D: 7},
C: {D: 6, finish: 3},
D: {finish: 1},
finish: {}
};
const lowestCostNode = (costs, processed) => {
@hungneox
hungneox / spacemacs-cheatsheet.txt
Created April 28, 2020 07:22 — forked from k3yavi/spacemacs-cheatsheet.txt
Spacemacs cheatsheet
//from http://www.saltycrane.com/blog/2015/12/switching-emacs-vim-actually-spacemacs/
Useful Spacemacs commands
SPC q q - quit
SPC w / - split window vertically
SPC w - - split window horizontally
SPC 1 - switch to window 1
SPC 2 - switch to window 2
SPC w c - delete current window
@hungneox
hungneox / mekong.md
Created April 15, 2020 20:10
Mekong is dying

Trong hình là đoạn sông Mekong khổng lồ bị cạn trơ đáy chỉ còn một dòng nước nhỏ ở chính giữa ở gần làng Sangkhom (thuộc tỉnh Nong Khai, Thái Lan) vào tháng 1/2020. Đây là đoạn sông Mekong vừa thoát ra khỏi Lào, cũng như bắt nguồn từ con sông Lan Thương (Lancang), chảy từ các khối băng thuộc dãy Himalayas, cao nguyên Tây Tạng (tỉnh Thanh Hải) và Thanh Tạng (tỉnh Vân Nam, Trung Quốc).

Ảnh này là của New York Times, được đăng trong bài viết nói về dữ kiện vệ tinh của Mỹ cho thấy dù nước về trên cao nguyên Tây Tạng và đang làm mùa mưa/tuyết tan, nhưng có một khối lượng nước khổng lồ được giữ lại trong lãnh thổ Trung Quốc khiến phần sông Mekong từ Lào đến Thái Lan hoàn toàn khô cạn. Xem:

https://www.nytimes.com/2020/04/13/world/asia/china-mekong-drought.html

https://558353b6-da87-4596-a181-b1f20782dd18.filesusr.com/ugd/81dff2_68504848510349d6a827c6a433122275.pdf?index=true

https://www.reuters.com/article/us-mekong-river/chinese-dams-held-back-mekong-waters-during-drought-study-finds-idUSKCN21V0U7

Creating your own Native Modules

Sometime when we need a specific way to interact with the phone or operator system which is various different between iOS and Android, that when we need to lean that responsibility to the OS it self.

In RN, most of the logic and UI, in the best way, must be consistent between platforms. So, In an advance level of bringing best user experience to end user, we need to create native module, that mean the leaning the logic to the Native Part. There are ton of article out there showing why and when we need to create a native module, I won't dive into explanation again. Instead, I will show you how can we create a native module that could be used in React native code. That mean, you write your Objective-C/Java code, and import and run it as a module in Javascript Code.

I will show you here the most basic way to create the most basic library, called: open https://google.com. It means, I want to open Google by using the default iOS/Android browser in a native way.

Creating re

@hungneox
hungneox / README.md
Created February 11, 2020 10:18 — forked from twolfson/README.md
Setting up SOPS

I'm learning about SOPS and setting it up as my preferred mechanism for storing secrets. Here are my notes.

PGP

It’s security mechanism is that we (i.e. client) use a PUBLIC key from the receiver (i.e. server) and encode it with a random key (I’m saying nonce but it could be reused)

This varies from RSA and SSH because the server uses a PUBLIC key to identify the client.

Web of trust

Web of trust operates by still using PGP (i.e. encoding with recipient’s public key) but additionally, we can encrypt/sign the data as our own by signing it with the client’s private key.

This means the recipient will initially decrypt via our (i.e. client’s) public key (verifying the source) and then decrypting via their (i.e. server’s) private key to get the data.