【发布时间】:2012-03-06 22:45:36
【问题描述】:
有没有一种使用 Python 语言在字母表 a b c 上表达交换运算符 T 的好方法,其中
a T b == cb T c == ac T a == b
我最好的尝试是硬编码:
def T(first, second):
if first is 'a' and second is 'b':
return 'c'
if first is 'a' and second is 'c':
return 'c'
if first is 'b' and second is 'c':
return 'a'
if first is 'b' and second is 'a':
return 'c'
if first is 'c' and second is 'a':
return 'b'
if first is 'c' and second is 'b':
return 'a'
【问题讨论】:
-
请定义“可交换三元算子T”的要求。它有什么作用?
-
交换:
i T j == j T i代表任何i, j。三元:“在字母表上a, b, c” -
不要使用
is比较字符串。使用==。 -
您希望“a T a”、“b T b”和“c T c”具有哪些值?