Skip to content
Open
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
Binary file not shown.
127 changes: 127 additions & 0 deletions homeworks/A10316/homework1/经济学人词频-A10316.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# -*- coding: utf-8 -*-

import codecs
import os

#1. 读取文件
#['aa', 'aaa-bbb-sds'] => ['aa', 'aaa', 'bbb', 'sds']
def word_split(words):
new_list = []
for word in words:
if '-' not in word:
new_list.append(word)
else:
lst = word.split('-')
new_list.extend(lst)
return new_list


def read_file(file_path):
f = codecs.open(file_path, 'r', "utf-8") #打开文件
lines = f.readlines()
word_list = []
for line in lines:
line = line.strip()
words = line.split(" ") #用空格分割
words = word_split(words) #用-分割
word_list.extend(words)
return word_list

def get_file_from_folder(folder_path):
file_paths = []
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
file_paths.append(file_path)
return file_paths

#读取多文件里的单词
def read_files(file_paths):
final_words = []
for path in file_paths:
final_words.extend(read_file(path))
return final_words


#2.获取格式化之后的单词
def format_word(word):
fmt = 'abcdefghijklmnopqrstuvwxyz-'
for char in word:
if char not in fmt:
word = word.replace(char, '')
return word.lower()

def format_words(words):
word_list = []
for word in words:
wd = format_word(word)
if wd:
word_list.append(wd)
return word_list

#3. 统计单词数目
# {'aa':4, 'bb':1}
def statictcs_words(words):
s_word_dict = {}
for word in words:
if s_word_dict.has_key(word):
s_word_dict[word] = s_word_dict[word] + 1
else:
s_word_dict[word] = 1
#排序
sorted_dict = sorted(s_word_dict.iteritems(), key=lambda d: d[1], reverse=True)
return sorted_dict
#3.1 读取词典
def read_dict(file_path):
f = codecs.open(file_path, 'r', "utf-8") #打开文件
lines = f.readlines()
dict_list = []
word_dict = {}
for line in lines:
line = line.strip() #删掉一行的空格
word, space, meaning = line.partition(' ') #空格区分
meaning = meaning.strip()
if word:
dict_list.append(word)
dict_list.append(meaning)
word_dict[word] = meaning
return word_dict

#4.输出成csv
def print_to_csv(volcaulay_list, word_dict, to_file_path, total_count, start_and_end):
nfile = codecs.open(to_file_path, 'w+', "utf-8")
current_count = 0
for val in volcaulay_list:
num = val[1]
current_count = current_count + num
word_rate = (float(current_count)/total_count) * 100
meaning = u'未找到释义'
if val[0] in word_dict:
meaning = word_dict[val[0]]
if (word_rate/100 >= start_and_end[0]) and (word_rate/100 <=start_and_end[1]):
nfile.write("%s,%d,%0.2f,%s\n" % (val[0], val[1], word_rate, meaning))
nfile.close()


def main():
#1. 读取文本
words = read_files(get_file_from_folder('data1'))
print '获取了未格式化的单词 %d 个' % (len(words))

#2. 清洗文本
f_words = format_words(words)
total_word_count = len(f_words)
print '获取了已经格式化的单词 %d 个' % (len(f_words))

#3. 统计单词和排序
word_list = statictcs_words(f_words)

start_and_end = [0.5, 0.7] #截取这一部分的单词

word_dict = read_dict("8000-words.txt") #读释义

#4. 输出文件
print_to_csv(word_list, word_dict, 'output/test.csv', total_word_count, start_and_end)

if __name__ == "__main__":
main()
100 changes: 100 additions & 0 deletions homeworks/A10316/homework2/pygame2-ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# -*- coding: UTF-8 -*-


import pygame

MOU_CON = True

board = pygame.Rect(280, 400, 80, 5) #弹球的板
circle = [100, 100] #球的圆心位置
RADIUS = 10 #求的半径
speed = [5, 5]
WIDTH = 640
HEIGHT = 480

def renew_ball():
if circle[1] > HEIGHT:
circle[0] = 100
circle[1] = 100

def update_ball(): #更新球的位置
if circle[1] - RADIUS == 0: #碰撞到上边缘(小球圆心减去半径等于0)
speed[1] *= -1 #那么y轴的方向发生了变化
if circle[0] + RADIUS ==WIDTH or circle[0] - RADIUS ==0: #碰撞右边缘
speed[0] *= -1 #x轴速度发生变化
# if circle[0] - RADIUS ==0: #和碰右边缘是一样的 所以直接合并成一条
# speed[0] *= -1
if circle[1] + RADIUS == 400 and circle[0] >= board[0] and circle[0] <= board[0] + 80 : #圆心加半径等于小板子的y轴
speed[1] *= -1

circle[0] += speed[0] #x轴 加速度
circle[1] += speed[1] #y轴 加速度


def draw_surface(screen):
screen.fill([255, 255, 255]) #white 每一个动作后重新填充了一遍画布
pygame.draw.circle(screen, [255, 0, 0], circle, RADIUS)
pygame.draw.rect(screen, [0, 255, 255], board)
pygame.display.flip()

def update_board():
if MOU_CON:
(x, y) = pygame.mouse.get_pos() #获取鼠标的位置信息
board.centerx = x #板的x轴中心跟着鼠标的中心走
# board.centery = y #全板移动

def fail():
if circle[1] > HEIGHT:
return True
return False

def w_down_cb():
# if not MOU_CON:
# board.centery -= 5 #y轴是向下加的 往上走得用减
pass

def s_down_cb():
# if not MOU_CON:
# board.centery += 5
pass

def a_down_cb():
if not MOU_CON:
board.centerx -= 5

def d_down_cb():
if not MOU_CON:
board.centerx += 5

def main():
pygame.init()
screen = pygame.display.set_mode([WIDTH, HEIGHT])
running = True

while running:
pygame.time.delay(40) #50是毫秒
update_board() #根据鼠标输入更新弹球板的位置
update_ball()
draw_surface(screen) #对于画布表面的设置都打包成一个函数

if fail():
break

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
w_down_cb() #回调函数 call back
elif event.key == pygame.K_s:
s_down_cb()
elif event.key == pygame.K_a:
a_down_cb()
elif event.key == pygame.K_d:
d_down_cb()

pygame.quit()

if __name__ == '__main__':
main()

120 changes: 120 additions & 0 deletions homeworks/A10316/homework2/pygame3_snake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# -*- coding: UTF-8 -*-


import pygame
import random


SCALE = 20 #20*20的图 地图中有多少格
SIZE = 20 #每一格的大小
WIDTH = SCALE * SIZE
HEIGHT = SCALE * SIZE

DIRECT = [[0, -1], [-1, 0], [0, 1], [1, 0]] # UP FORWARD DOWN BACKWARD
dirt = 1 #初始化方向 方向相当于是上面数组的下标

snake = [[4, 3], [5, 3], [6, 3]]
apple = [3, 1]

def screen_show(screen):
screen.fill([255, 255, 255])
for body in snake:
pygame.draw.rect(screen, [0, 0, 0], [snake[0][0] * SIZE, snake[0][1] * SIZE, SIZE - 1, SIZE - 1])
#蛇头的颜色换一下
pygame.draw.rect(screen, [0, 255, 0], [body[0] * SIZE, body[1] * SIZE, SIZE - 1, SIZE - 1])
# size-1的原因是这样身体之间会有个间隙
pygame.draw.circle(screen, [255, 0, 0], [apple[0] * SIZE + SIZE / 2, apple [1] * SIZE + SIZE / 2], SIZE / 2)
#圆心的位置要先确认左上角*一个格子的大小,再加上格子长度的一般。

pygame.display.flip()


def snake_update():
new_body = [0, 0]
new_body[0] = (snake[0][0] + DIRECT[dirt][0]) % SCALE #snake[0] 是蛇头
new_body[1] = (snake[0][1] + DIRECT[dirt][1]) % SCALE

if new_body == apple:
snake.insert(0, new_body) #注意是圆括号
return True

else:
snake.insert(0, new_body)
snake.pop()
return False

def new_apple(): #生成新苹果 但是蛇什么时候吃到苹果要取决于蛇
apple[0] = random.randint(0, 19) #随机生成一个包括0,包括19的数字
apple[1] = random.randint(0, 19)

def fail(): #蛇要是吃到自己就输掉 头在身体里面
if snake.count(snake[0]) >= 2:
return True
return False

# 在按键回调函数中判断,
# 如果当前蛇和回调函数需要设置的方向为同向或反向,则不必更新蛇运行的方向
def w_down_cb():
global dirt
if dirt % 2 != 0:
dirt = 0

def s_down_cb():
global dirt
if dirt % 2 != 0:
dirt = 2

def a_down_cb():
global dirt
if dirt % 2 != 1:
dirt = 1

def d_down_cb():
global dirt
if dirt % 2 != 1:
dirt = 3


def main():
pygame.init()
screen = pygame.display.set_mode([WIDTH, HEIGHT])
running = True

while running:
pygame.time.delay(200) #50是毫秒

if snake_update():
new_apple()

if fail():
break

screen_show(screen) #这个都是放在动作的最后的


for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
w_down_cb() #回调函数 call back
elif event.key == pygame.K_s:
s_down_cb()
elif event.key == pygame.K_a:
a_down_cb()
elif event.key == pygame.K_d:
d_down_cb()

font = pygame.font.Font(None, 50)
text = font.render("Failed", True, [255,0,0])
screen.blit(text, [40, 60])
# img = pygame.image.load("beach_ball.png")
# screen.blit(img, [0, 0])
pygame.display.flip()


pygame.quit()

if __name__ == '__main__':
main()

22 changes: 22 additions & 0 deletions homeworks/A10316/homework3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# uband-s2-seven
uband友班编程课程

#背景
应用轻量级web框架完成个人主页设计

#要求
小组页面:
0、基本元素:
头像-圆形,居中
背景图片
昵称-居中,uband红
1、自我介绍
2、相册
3、项目展示
4、留言板

#发布

#其他

#参考资料
2 changes: 2 additions & 0 deletions homeworks/A10316/homework3/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CSRF_ENABLED = True
SECRET_KEY = '123456'
Binary file added homeworks/A10316/homework3/config.pyc
Binary file not shown.
Loading