Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions renzongxian/0010/0010.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-21
# Python 3.4

"""

第 0010 题:使用 Python 生成字母验证码图片

"""

from PIL import Image, ImageDraw, ImageFont
import string
import random


def generate_authenticode():
# Generate random letters
letters = ''.join([random.choice(string.ascii_letters) for i in range(4)])

# Set the size of the image
width = 100
height = 40

# Generate an image
im = Image.new("RGB", (width, height), (255, 255, 255))

# Draw letters on the image
dr = ImageDraw.Draw(im)
font = ImageFont.truetype("arial.ttf", 30)
for i in range(4):
dr.text((5+i*20, 5), letters[i], (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), font)
del dr

# Change the background color
for x in range(width):
for y in range(height):
if im.getpixel((x, y)) == (255, 255, 255):
im.putpixel((x, y), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))

# Save the image
im.save('t1.png')

if __name__ == "__main__":
generate_authenticode()

49 changes: 49 additions & 0 deletions renzongxian/0011/0011.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-21
# Python 3.4

"""

第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。

北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge

"""


def filter_words(words):
# Read filtered words from file named 'filtered_words.txt'
file_object = open('filtered_words.txt', 'r')
filtered_words = []
for line in file_object:
filtered_words.append(line.strip('\n'))
file_object.close()

# Check if the input words include the filtered words
filtered = False
for filtered_word in filtered_words:
if filtered_word in words:
filtered = True
break

if filtered is True:
print('Freedom')
else:
print('Human Rights')

if __name__ == '__main__':
while True:
input_words = input('Input some words:')
filter_words(input_words)

11 changes: 11 additions & 0 deletions renzongxian/0011/filtered_words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
����
����Ա
����Ա
�쵼
ţ��
ţ��
����
����
love
sex
jiangge
33 changes: 33 additions & 0 deletions renzongxian/0012/0012.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-22
# Python 3.4

"""

第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,
例如当用户输入「北京是个好城市」,则变成「**是个好城市」。

"""


def filter_words(words):
# Read filtered words from file named 'filtered_words.txt'
file_object = open('filtered_words.txt', 'r')
filtered_words = []
for line in file_object:
filtered_words.append(line.strip('\n'))
file_object.close()

# Check if the input words include the filtered words and replace the filtered with '*'
for filtered_word in filtered_words:
if filtered_word in words:
words = words.replace(filtered_word, '*'*len(filtered_word))

print(words)

if __name__ == '__main__':
while True:
input_words = input('Input some words:')
filter_words(input_words)

11 changes: 11 additions & 0 deletions renzongxian/0012/filtered_words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
����
����Ա
����Ա
�쵼
ţ��
ţ��
����
����
love
sex
jiangge
41 changes: 41 additions & 0 deletions renzongxian/0014/0014.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-22
# Python 3.4

"""

第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
{
"1":["张三",150,120,100],
"2":["李四",90,99,95],
"3":["王五",60,66,68]
}
请将上述内容写到 student.xls 文件中。

"""

import xlwt3
import json


# 该部分参考了agmcs的代码,https://github.com/Show-Me-the-Code/python/blob/master/agmcs/0014/0014.py
def write_txt_to_xls(txt_file):
# Read form the txt file
txt_object = open(txt_file, 'r')
file_content = json.load(txt_object)

# Write to the xls file
xls_object = xlwt3.Workbook()
sheet = xls_object.add_sheet('student')
for i in range(len(file_content)):
sheet.write(i, 0, i+1)
data = file_content[str(i+1)]
for j in range(len(data)):
sheet.write(i, j+1, data[j])
xls_object.save('student.xls')


if __name__ == '__main__':
write_txt_to_xls('student.txt')

5 changes: 5 additions & 0 deletions renzongxian/0014/student.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"1":["����",150,120,100],
"2":["����",90,99,95],
"3":["����",60,66,68]
}
39 changes: 39 additions & 0 deletions renzongxian/0015/0015.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-22
# Python 3.4

"""

第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:
{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
请将上述内容写到 city.xls 文件中.

"""

import xlwt3
import json


def write_txt_to_xls(txt_file):
# Read form the txt file
txt_object = open(txt_file, 'r')
file_content = json.load(txt_object)

# Write to the xls file
xls_object = xlwt3.Workbook()
sheet = xls_object.add_sheet('city')
for i in range(len(file_content)):
sheet.write(i, 0, i+1)
data = file_content[str(i+1)]
sheet.write(i, 1, data)
xls_object.save('city.xls')


if __name__ == '__main__':
write_txt_to_xls('city.txt')

5 changes: 5 additions & 0 deletions renzongxian/0015/city.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"1" : "�Ϻ�",
"2" : "����",
"3" : "�ɶ�"
}