Skip to content

Instantly share code, notes, and snippets.

View mhtsai1010's full-sized avatar
🇹🇼
Make It Simple.

J.T. mhtsai1010

🇹🇼
Make It Simple.
View GitHub Profile
@mhtsai1010
mhtsai1010 / script-front-matter.sh
Last active August 22, 2016 04:06
Shell: script front-matter
#===============================================================================
#
# FILE: filename.sh
#
# USAGE: filename.sh [Options] [Arguments]
#
# DESCRIPTION: describe the purpose of this program.
#
# OPTIONS: see function ’usage’ below
# REQUIREMENTS: ---
@mhtsai1010
mhtsai1010 / function-front-matter.sh
Last active August 22, 2016 04:06
Shell: function front-matter
#--- FUNCTION ----------------------------------------------------------------
# NAME: usage
# DESCRIPTION: Display usage information for this script.
# PARAMETER 1: --
#-------------------------------------------------------------------------------
@mhtsai1010
mhtsai1010 / utf-8-magic-comment.py
Last active August 22, 2016 04:06
Python: utf-8 magic comment
# -*- coding: utf-8 -*-
@mhtsai1010
mhtsai1010 / tcp-client-sockets.py
Last active August 22, 2016 04:07
Python: tcp client sockets
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
target_host = "127.0.0.1"
target_port = 80
# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@mhtsai1010
mhtsai1010 / udp-client-sockets.py
Last active August 22, 2016 04:07
Python: udp client sockets
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
target_host = "127.0.0.1"
target_port = 80
# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@mhtsai1010
mhtsai1010 / binary2decimal.py
Last active August 22, 2016 04:05
Python: binary to decimal
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
def bin2dec(binary_string):
""" Convert binary string to decimal number """
decimal = 0
while binary_string:
decimal = decimal * 2 + (ord(binary_string[0]) - ord('0'))
binary_string = binary_string[1:]
@mhtsai1010
mhtsai1010 / ternary_conditional_operator.py
Last active August 22, 2016 04:04
Python: ternary conditional operator
A = Y if X else Z
@mhtsai1010
mhtsai1010 / list_comprehension.py
Last active August 22, 2016 04:04
Python: list comprehension syntax
[ expression for item in list if conditional ]
@mhtsai1010
mhtsai1010 / tokenized_input.py
Last active August 22, 2016 04:04
Python: tokenized input Raw
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
tokenizedInput = sys.stdin.read().split()
@mhtsai1010
mhtsai1010 / prime_list.py
Last active August 22, 2016 04:03
Python: generate a prime list < n
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
def primes(n):
""" Returns a list of primes < n """
sieve = [True] * n
for i in xrange(3,int(n**0.5)+1,2):
if sieve[i]:
sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)