-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondition.py
More file actions
132 lines (115 loc) · 3.88 KB
/
Copy pathcondition.py
File metadata and controls
132 lines (115 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
from data import RandomData
class CalcException(Exception): pass
class Calc(object):
"""
条件计算规则
例如:
求合
取偶数
取奇数
取第三个值
最大数
最小数
函数的返回结果
问题,答案
"""
Methods = [
"sum",
"get_even",
"get_odd",
"max",
"min",
"index_value",
]
@staticmethod
def sum(*args):
ans = sum(map(int, args))
return '%s的和' % '、'.join(map(str, args)), ans
@staticmethod
def get_even(*args):
ans = ''.join([str(item) for item in map(int, args) if item % 2 == 0])
if not ans:
raise CalcException('No even number found in %s' % '、'.join(map(str, args)))
return '%s里的偶数' % '、'.join(map(str, args)), ans
@staticmethod
def get_odd(*args):
ans = ''.join([str(item) for item in map(int, args) if item % 2 != 0])
if not ans:
raise CalcException('No odd number found in %s' % '、'.join(map(str, args)))
return '%s里的奇数' % '、'.join(map(str, args)), ans
@staticmethod
def max(*args):
ans = max(map(int, args))
return '%s里最大的数' % '、'.join(map(str, args)), ans
@staticmethod
def min(*args):
ans = min(map(int, args))
return '%s里最小的数' % '、'.join(map(str, args)), ans
@staticmethod
def index_value(index=None, *args):
if index is None:
index = RandomData.random_choice(range(len(args)))
ans = args[index]
return '%s里第%s个内容' % ('、'.join(map(str, args)), index + 1), ans
class ConditionConnection(object):
"""
条件连接器,用于连接条件,组成问题
例如:
... 和 ...
... 和 ... 的和
...
"""
Single = 0
Conbine = 1
ConbineSum = 2
Kinds = [
(Single, lambda arg: arg, '%s'), # 单条件
(Conbine, lambda con1, con2: '%s%s' % (con1, con2), '%s和%s的连接'), # 组合条件 "... 和 ..."
(ConbineSum, lambda con1, con2: int(con1) + int(con2), '%s和%s的和'), # 组合条件求和 "... 和 ... 的和"
]
def __init__(self):
pass
@staticmethod
def choice():
return RandomData.random_choice(ConditionConnection.Kinds)
class Condition(object):
"""
条件,用于生产一个简短的条件描述
例如:
第三个数
所有的偶数
第二个奇数
今年的年份
今年的月份
北京奥运会的年份
"""
Any = 0
UseNative = 1
UseData = 2
def __init__(self, role):
self.UseNative_Roles = [
("今年年份", RandomData.current_year()),
("本月月份", RandomData.current_month()),
("今天几号", RandomData.current_day()),
("今天周几[1-7]", RandomData.current_weekday()),
("北京奥运那年", "2008"),
Calc.index_value(None, *[RandomData.get_letter_digit() for i in range(4)]),
]
self.UseData_Roles = [
Calc.sum(*[RandomData.get_int() for i in range(4)]),
Calc.get_even(*[RandomData.get_int() for i in range(4)]),
Calc.get_odd(*[RandomData.get_int() for i in range(4)]),
Calc.max(*[RandomData.get_int() for i in range(4)]),
Calc.min(*[RandomData.get_int() for i in range(4)]),
]
if role == self.Any:
role = RandomData.random_choice([self.UseData, self.UseNative])
self.role = role
def generate_condition(self):
if self.role == Condition.UseNative:
return RandomData.random_choice(self.UseNative_Roles)
elif self.role == Condition.UseData:
return RandomData.random_choice(self.UseData_Roles)
else:
raise Exception("role error")