Navigation Menu

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
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
@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
@JackInTaiwan
JackInTaiwan / python_iteration_1.py
Last active October 19, 2019 14:48
python_iteration_1.py
x = [1, 2, 3]
### Method 1: use "dir()"
print(dir(x))
# ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
# '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
# '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
# '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__',
# '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__',
# '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
import dis
def dog_bark():
for i in range(3):
print('Miouwo ~~~')
if __name__ == '__main__':
dis.dis(dog_bark)
# main.py
if __name__ == '__main__':
from sample_code import x
print(x)
=================================
# sample_code.py
x = 1000