Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
逆流者blog
python3-learn
提交
b2fd5d21
P
python3-learn
项目概览
逆流者blog
/
python3-learn
通知
23
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
1
Wiki
分析
仓库
DevOps
项目成员
Pages
P
python3-learn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
1
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b2fd5d21
编写于
11月 21, 2020
作者:
逆流者blog
🇨🇳
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
py
上级
eeb71511
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
98 addition
and
98 deletion
+98
-98
base/language_survey.py
base/language_survey.py
+4
-4
base/test_survey.py
base/test_survey.py
+39
-39
matplotlib/random_walk.py
matplotlib/random_walk.py
+37
-37
matplotlib/rw_visual.py
matplotlib/rw_visual.py
+18
-18
未找到文件。
base/language_survey.py
浏览文件 @
b2fd5d21
...
...
@@ -8,10 +8,10 @@ my_survey = AnonymousSurvey(question)
my_survey
.
show_question
()
print
(
"Enter 'q' at any time to quit.
\n
"
)
while
True
:
response
=
input
(
"language: "
)
if
response
==
'q'
:
break
my_survey
.
store_response
(
response
)
response
=
input
(
"language: "
)
if
response
==
'q'
:
break
my_survey
.
store_response
(
response
)
# 显示调查结果
print
(
"
\n
Thank you to everyone who participated in the survey!"
)
...
...
base/test_survey.py
浏览文件 @
b2fd5d21
...
...
@@ -2,44 +2,44 @@ import unittest
from
survey
import
AnonymousSurvey
class
TestAnonymousSurvey
(
unittest
.
TestCase
):
"""针对AnonymousSurvey的测试"""
# def test_store_single_response(self):
# """测试单个答案会被妥善的存储"""
#
question = "What language did you first learn to speak?"
#
my_survey = AnonymousSurvey(question)
#
my_survey.store_response('English')
# self.assertIn("English", my_survey.responses)
# def test_store_three_responses(self):
#
"""测试三个答案会被妥善地存储"""
#
question = "What language did you first learn to speak?"
#
my_survey = AnonymousSurvey(question)
#
responses = ['English', 'Spanish', 'Mandarin']
#
for response in responses:
#
my_survey.store_response(response)
#
for response in responses:
#
self.assertIn(response, my_survey.responses)
def
setUp
(
self
):
"""创建一个调查对象和一组答案, 供使用的测试方法使用"""
question
=
"What language did you first learn to speak?"
self
.
my_survey
=
AnonymousSurvey
(
question
)
self
.
responses
=
[
'English'
,
'Spanish'
,
'Mandarin'
]
def
test_store_single_response
(
self
):
"""测试单个答案会被妥善的存储"""
self
.
my_survey
.
store_response
(
self
.
responses
[
0
])
self
.
assertIn
(
self
.
responses
[
0
],
self
.
my_survey
.
responses
)
def
test_store_three_responses
(
self
):
"""测试三个答案会被妥善地存储"""
for
response
in
self
.
responses
:
self
.
my_survey
.
store_response
(
response
)
for
response
in
self
.
responses
:
self
.
assertIn
(
response
,
self
.
my_survey
.
responses
)
"""针对AnonymousSurvey的测试"""
# def test_store_single_response(self):
# """测试单个答案会被妥善的存储"""
#
question = "What language did you first learn to speak?"
#
my_survey = AnonymousSurvey(question)
#
my_survey.store_response('English')
#
# self.assertIn("English", my_survey.responses)
# def test_store_three_responses(self):
#
"""测试三个答案会被妥善地存储"""
#
question = "What language did you first learn to speak?"
#
my_survey = AnonymousSurvey(question)
#
responses = ['English', 'Spanish', 'Mandarin']
#
for response in responses:
#
my_survey.store_response(response)
#
#
for response in responses:
#
self.assertIn(response, my_survey.responses)
def
setUp
(
self
):
"""创建一个调查对象和一组答案, 供使用的测试方法使用"""
question
=
"What language did you first learn to speak?"
self
.
my_survey
=
AnonymousSurvey
(
question
)
self
.
responses
=
[
'English'
,
'Spanish'
,
'Mandarin'
]
def
test_store_single_response
(
self
):
"""测试单个答案会被妥善的存储"""
self
.
my_survey
.
store_response
(
self
.
responses
[
0
])
self
.
assertIn
(
self
.
responses
[
0
],
self
.
my_survey
.
responses
)
def
test_store_three_responses
(
self
):
"""测试三个答案会被妥善地存储"""
for
response
in
self
.
responses
:
self
.
my_survey
.
store_response
(
response
)
for
response
in
self
.
responses
:
self
.
assertIn
(
response
,
self
.
my_survey
.
responses
)
unittest
.
main
()
\ No newline at end of file
matplotlib/random_walk.py
浏览文件 @
b2fd5d21
...
...
@@ -2,40 +2,40 @@ from random import choice
class
RandomWalk
:
"""一个生成随机漫步数据的类"""
def
__init__
(
self
,
num_points
=
5000
):
"""初始化随机漫步的属性"""
self
.
num_points
=
num_points
# 所有随机漫步都始于(0,0)
self
.
x_values
=
[
0
]
self
.
y_values
=
[
0
]
def
fill_walk
(
self
):
"""计算随机漫步包含的所有点"""
# 不断漫步,知道列表达到指定的长度
while
len
(
self
.
x_values
)
<
self
.
num_points
:
# choice() 随机选择列表中的一个数
# 决定前进方向以及沿这个方向前进的距离
# x 的方向
x_direction
=
choice
([
1
,
-
1
])
# x 的距离
x_distance
=
choice
([
0
,
1
,
2
,
3
,
4
])
x_step
=
x_direction
*
x_distance
y_direction
=
choice
([
1
,
-
1
])
y_distance
=
choice
([
0
,
1
,
2
,
3
,
4
])
y_step
=
y_direction
*
y_distance
# 拒绝原地踏步
if
x_step
==
0
and
y_step
==
0
:
continue
# 计算下一个点的x和y值
next_x
=
self
.
x_values
[
-
1
]
+
x_step
next_y
=
self
.
y_values
[
-
1
]
+
y_step
self
.
x_values
.
append
(
next_x
)
self
.
y_values
.
append
(
next_y
)
"""一个生成随机漫步数据的类"""
def
__init__
(
self
,
num_points
=
5000
):
"""初始化随机漫步的属性"""
self
.
num_points
=
num_points
# 所有随机漫步都始于(0,0)
self
.
x_values
=
[
0
]
self
.
y_values
=
[
0
]
def
fill_walk
(
self
):
"""计算随机漫步包含的所有点"""
# 不断漫步,知道列表达到指定的长度
while
len
(
self
.
x_values
)
<
self
.
num_points
:
# choice() 随机选择列表中的一个数
# 决定前进方向以及沿这个方向前进的距离
# x 的方向
x_direction
=
choice
([
1
,
-
1
])
# x 的距离
x_distance
=
choice
([
0
,
1
,
2
,
3
,
4
])
x_step
=
x_direction
*
x_distance
y_direction
=
choice
([
1
,
-
1
])
y_distance
=
choice
([
0
,
1
,
2
,
3
,
4
])
y_step
=
y_direction
*
y_distance
# 拒绝原地踏步
if
x_step
==
0
and
y_step
==
0
:
continue
# 计算下一个点的x和y值
next_x
=
self
.
x_values
[
-
1
]
+
x_step
next_y
=
self
.
y_values
[
-
1
]
+
y_step
self
.
x_values
.
append
(
next_x
)
self
.
y_values
.
append
(
next_y
)
matplotlib/rw_visual.py
浏览文件 @
b2fd5d21
...
...
@@ -4,27 +4,27 @@ from random_walk import RandomWalk
# 只要程序处于活动状态,就不断地模拟随机漫步
while
True
:
# 创建一个RandomWalk实例,并将其包含的点都绘制出来
rw
=
RandomWalk
(
50000
)
rw
.
fill_walk
()
# 创建一个RandomWalk实例,并将其包含的点都绘制出来
rw
=
RandomWalk
(
50000
)
rw
.
fill_walk
()
# 设置绘图窗口的尺寸
plt
.
figure
(
dpi
=
256
,
figsize
=
(
10
,
6
))
# 设置绘图窗口的尺寸
plt
.
figure
(
dpi
=
256
,
figsize
=
(
10
,
6
))
point_numbers
=
list
(
range
(
rw
.
num_points
))
# 给点着色看出漫步的轨迹
plt
.
scatter
(
rw
.
x_values
,
rw
.
y_values
,
c
=
point_numbers
,
cmap
=
plt
.
cm
.
Blues
,
edgecolor
=
'none'
,
s
=
1
)
point_numbers
=
list
(
range
(
rw
.
num_points
))
# 给点着色看出漫步的轨迹
plt
.
scatter
(
rw
.
x_values
,
rw
.
y_values
,
c
=
point_numbers
,
cmap
=
plt
.
cm
.
Blues
,
edgecolor
=
'none'
,
s
=
1
)
# 突出起点和终点
plt
.
scatter
(
0
,
0
,
c
=
'green'
,
edgecolor
=
'none'
,
s
=
100
)
plt
.
scatter
(
rw
.
x_values
[
-
1
],
rw
.
y_values
[
-
1
],
c
=
'red'
,
edgecolor
=
'none'
,
s
=
100
)
# 突出起点和终点
plt
.
scatter
(
0
,
0
,
c
=
'green'
,
edgecolor
=
'none'
,
s
=
100
)
plt
.
scatter
(
rw
.
x_values
[
-
1
],
rw
.
y_values
[
-
1
],
c
=
'red'
,
edgecolor
=
'none'
,
s
=
100
)
# 隐藏坐标轴
plt
.
axes
().
get_xaxis
().
set_visible
(
False
)
plt
.
axes
().
get_yaxis
().
set_visible
(
False
)
# 隐藏坐标轴
plt
.
axes
().
get_xaxis
().
set_visible
(
False
)
plt
.
axes
().
get_yaxis
().
set_visible
(
False
)
plt
.
show
()
plt
.
show
()
keep_running
=
input
(
"是否再模拟一次随机漫步?(y/n):"
)
if
keep_running
==
'n'
:
break
keep_running
=
input
(
"是否再模拟一次随机漫步?(y/n):"
)
if
keep_running
==
'n'
:
break
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录