Skip to content

Instantly share code, notes, and snippets.

View SZ-Edward's full-sized avatar

CodingPoet SZ-Edward

  • sudbury, canada
View GitHub Profile
@SZ-Edward
SZ-Edward / week3-after-class
Last active September 25, 2018 16:04
第三周课后练习参考答案
# 1、编写一个函数完成密码生成器的功能,输入参数有密码长度和密码组成的内容,密码组成的内容可以有大写字母(A-Z)、小写字母(a-z)、数字(0-9)、特殊符号(~!@#$%^&*()),返回值为按照用户要求生成的随机密码
def passwd_gen(p_len, lower_case=True, upper_case=True, number=True, special_char=True):
import string, random
lower_case_set = set(list(string.ascii_lowercase))
upper_case_set = set(list(string.ascii_uppercase))
number_set = set([x for x in range(10)])
special_char_set = set(list('~!@#$%^&*()'))
user_choice = set()
if lower_case:
user_choice = user_choice | lower_case_set
@SZ-Edward
SZ-Edward / week3-in-class
Last active September 19, 2018 17:51
第三周课堂练习参考答案
# 1、写一个函数:获取用户输入的名字(name)和年龄(age),计算一下该用户到哪一年(返回值)为100岁,并打印结果”{用户名字}在XXXX年为100岁”
def get_year_of_hundred():
name = input('请输入您的名字:')
age = int(input('请输入您当前的年龄:'))
which_year = 100 + 2018 - age
print(f'{name} 在 {which_year} 年为 100 岁')
return which_year
get_year_of_hundred()
@SZ-Edward
SZ-Edward / week2-after-class
Last active September 18, 2018 03:48
第二周课后练习参考答案
# 1、编写一个函数输出乘法口诀表,执行结果如图所示(提示:print 默认输出后会换行,可以通过 print 参数控制是否换行)
for i in range(1, 10):
for j in range(1, i + 1):
print('{}*{}={}'.format(i, j, i*j), end='\t')
print()
################################
for i in range(1, 10):
for j in range(1, i + 1):
end_flag = '\n' if i == j else '\t'
print(f'{i}*{j}={i*j}', end=end_flag)
@SZ-Edward
SZ-Edward / week2-in-class
Last active September 18, 2018 03:51
第二周课堂练习参考答案
# 1、将字符串'hello, world'中的 l 替换为 *
'hello, world'.replace('l', '*')
# 2、现有字符串 'Good' ,期望结果 'good!good!good!',至少用两种方法实现
string = 'Good'.lower() + '!'
string + string + string
##############################
string = 'Good'
string = string.lower() + '!'
string * 3
def all_indices(value, qlist):
indices = []
idx = -1
while True:
try:
idx = qlist.index(value, idx + 1)
indices.append(idx)
except ValueError:
break
return indices
@SZ-Edward
SZ-Edward / del_duplicates_of_list.py
Created December 7, 2017 11:04
Remove subsequent duplicates from list with keeping order.
def remove_duplicates_of_list(lst):
if len(lst) < 2:
return lst
d = {}
for i in xrange(len(lst)):
if lst[i] in d.values():
continue
d.update({i: lst[i]})
return [lst[key] for key in sorted(d.keys())]
@SZ-Edward
SZ-Edward / dict-like.py
Created December 7, 2017 10:38
Implement a data structure(dict-like), you can get value by index or key. Just like data[index] or data[key].
import unittest
def get_of_data(data, key):
if type(key) is int:
if key < len(data[1]):
k = data[1][key]
return data[0][k]
raise IndexError('The index is out of range.')
elif type(key) is str:
return data[0][key]
@SZ-Edward
SZ-Edward / TheForgettingCurve
Created July 26, 2014 17:25
The Forgetting Curve for yourself
if __name__ == "__main__":
import datetime
today = None#开始日期
forgettingCurve = [0, 1, 2, 4, 7, 15, 30]#在这里设置您的记忆曲线间隔天数,0表示无间隔
title = "UnitName FirstDate SecondDate ThirdDate FourthDate FifthDate SixthDate SeventhDate"
print("Your vocabulary memorization schedule as following\n%s\n%s" % ("="*100, title))
for num in range(1, 22):
if today is None and num == 1:
today = datetime.date.today()
else: