изящнаяclass AnyType(object):
def __str__(self): return 'Any'
Any = AnyType()
import math
def fooo(a, b, c):
'''
>>> fooo(1, 2, 1)
[1]
>>> fooo(1, 2, 10)
[]
>>> fooo(0, 0, 0) is Any
True
'''
if (a == 0) and (b == 0):
if (c == 0):
return Any
else:
return []
elif (a == 0):
return [-c/b]
else:
d = (b*b) - (4*a*c)
e = -b/(2*a)
if d < 0:
return []
else:
f = math.sqrt(d)/(2*a)
if f == 0:
return [-e]
else:
return [e + f, e - f]
def nRoots(a, b, c):
'''
>>> nRoots(1, 2, 1)
1
>>> nRoots(1, 2, 10)
0
>>> nRoots(1.0, 1.0, 0.25)
1
>>> nRoots(1, 5, 6)
2
'''
assert type (fooo(a, b, c)) != Any
return len (fooo(a, b, c))
if __name__ == '__main__':
import doctest
doctest.testmod()неизящная и не работает Т_Тimport math
class AnyType(object):
def __str__(self): return 'Any'
Any = AnyType()
def fooo(a, b, c):
'''
>>> fooo(1, 2, 1)
1
>>> fooo(1, 2, 10) is None
True
>>> fooo(1, 0, -10000000000000000000000 + 1) == fooo(1, 0, -10000000000000000000000)
True
'''
assert a != 0
d = (b*b) - (4*a*c)
e = -b/(2*a)
if d < 0:
return None
else:
f = math.sqrt(d)/(2*a)
if f == 0:
return -e
else:
return (e + f, e - f)
def nRoots(a, b, c):
'''
>>> nRoots(1, 2, 1)
1
>>> nRoots(1, 2, 10)
0
>>> nRoots(1.0, 1.0, 0.25)
1
>>> nRoots(1, 5, 6)
2
'''
if type (fooo(a, b, c)) == tuple:
print 2
elif fooo(a, b, c) is None:
print 0
else:
print 1
if __name__ == '__main__':
import doctest
doctest.testmod()
print (fooo(0, 0, 0))
@темы:
будни,
гордое до невозможности