擅长:python、mysql、java
<p>以下工作列表长度不超过3(即0、1、2或3)</p>
<pre><code>def sum_dup(a = 0, b = 0, c = 0):
""" Using defaults allows strategy to Works with lists of length 0, 1, 2, or 3.
If we have fewer than three inputs then missing inputs are 0 by default """
if a == b == c:
# all three are equal
return a + b + c
elif a == b:
# equal first & second
return a + b
elif b == c:
# second & third
return b + c
elif a == c:
# first & third
return a + c
else:
# none are equal
return 0
</code></pre>
<p><strong>测试列表</p>
<pre><code>lsts = [[5], [5, 5, 5], [], [0, 5], [2, 3, 3], [5, 0, 5]]
for lst in lsts:
# *lst explands list into input for sum_dup
# meaning same as sum_dup(lst[0], lst[1], etc.)
print(f'List {lst} has sum {sum_dup(*lst)}')
</code></pre>
<p><strong>输出</strong></p>
<pre><code>List [5] has sum 0
List [5, 5, 5] has sum 15
List [] has sum 0
List [0, 5] has sum 0
List [2, 3, 3] has sum 6
List [5, 0, 5] has sum 10
</code></pre>