Skip to content

Instantly share code, notes, and snippets.

View JackInTaiwan's full-sized avatar

Jack Cheng JackInTaiwan

View GitHub Profile
@JackInTaiwan
JackInTaiwan / python_hint_for_leetcode.py
Last active December 25, 2022 07:21
Some Syntax Hints of Python3 for LeetCode practice
# List
x = ["A", "B"]
x.insert(0, "C") # ["C", "A", "B"]
x = [(1, 2), (2, 2), (2, 1)]
x.sort() # [(1, 2), (2, 1), (2, 2)]
from copy import deepcopy
y = x.copy() # shallow copy
y = deepcopy(x) # deep copy
def print_func_name(func):
def warp():
print("Now use function '{}'".format(func.__name__))
func()
return warp
@print_func_name
def dog_bark():
print("Bark !!!")
@JackInTaiwan
JackInTaiwan / python_iteration_2.py
Last active December 15, 2021 08:58
python_iteration_2.py
class MyIterator:
def __init__(self, max_num):
self.max_num = max_num
self.index = 0
def __iter__(self):
return self
def __next__(self):
self.index += 1
import time
def print_func_name(time):
def decorator(func):
def wrap():
print("Now use function '{}'".format(func.__name__))
print("Now Unix time is {}.".format(int(time)))
func()
import dis
def dog_bark():
for i in range(3):
print('Miouwo ~~~')
if __name__ == '__main__':
dis.dis(dog_bark)
import tensorflow as tf
### Save the graph g1
g1 = tf.Graph()
with g1.as_default():
a = tf.placeholder(tf.float32, name='a')
b = tf.Variable(initial_value=tf.truncated_normal((1,)), name='b')
c = tf.multiply(a, b, name='c')
@JackInTaiwan
JackInTaiwan / python_iteration_3.py
Last active October 20, 2019 17:00
python_iteration_3.py
class MyIterator:
def __init__(self, max_num):
self.max_num = max_num
def __iter__(self):
num = 1
while num <= self.max_num:
yield num
num += 1
@JackInTaiwan
JackInTaiwan / python_iteration_6.py
Created October 20, 2019 06:32
python_iteration_6.py
x = (i for i in range(2))
print(type(x))
# <class 'generator'>
print(next(x))
print(next(x))
print(next(x))
# 0
@JackInTaiwan
JackInTaiwan / python_iteration_5.py
Created October 20, 2019 06:26
python_iteration_5.py
def generator_func(value=0):
while value < 10:
value = yield value
value += 1
generator = generator_func()
print('step 1')
print(next(generator))
print('step 2')
@JackInTaiwan
JackInTaiwan / python_iteration_4.py
Last active October 19, 2019 16:46
python_iteration_4.py
class MyIterator:
def __init__(self, max_num):
self.max_num = max_num
def __getitem__(self, key):
if key <= self.max_num:
return key
else:
raise IndexError