Palindrome
def isPalindrome(x):
"""
>>> isPalindrome(121)
True
>>> isPalindrome(344)
False
>>> isPalindrome(-121)
Traceback (most recent call last):
ValueError: x must be positive integer.
>>> isPalindrome("hello")
Traceback (most recent call last):
TypeError: x must be integer.
"""
try:
x = int(x)
temp=x
rev=0
if(x>0):
while(x>0):
dig=x%10
rev=rev*10+dig
x=x//10
if(temp==rev):
return True
else:
return False
elif(x<0):
raise TypeError
else:
raise ValueError
except ValueError:
raise ValueError("x must be positive integer")
except TypeError:
raise TypeError("x must be an integer")
Circle
class Circle:
def __init__(self, radius):
# Define the initialization method below
"""
>>> c1 = Circle(2.5)
>>> [Link]
2.5
"""
[Link] = radius
def area(self):
# Define doctests for area method:
"""
>>> c1 = Circle(2.5)
>>> [Link]()
19.63
"""
# Define area functionality:
return round([Link]*([Link]**2),2)
def circumference(self):
# Define doctests for circumference method:
"""
>>> c1 = Circle(2.5)
>>> [Link]()
15.71
"""
# Define the circumference functionality below
return round(([Link] * 2 * [Link]),2)
# Define circumference functionality:
class TestCircleCreation([Link]):
def test_creating_circle_with_numeric_radius(self):
# Define a circle 'c1' with radius 2.5, and check if
# the value of [Link] is equal to 2.5 or not.
c1 = Circle(2.5)
[Link]([Link], 2.5)
def test_creating_circle_with_negative_radius(self):
# Define a circle 'c' with radius -2.5, and check
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive".
with [Link](ValueError) as e:
c = Circle(-2.5)
[Link](str([Link]), 'radius must be between 0 and 1000
inclusive')
def test_creating_circle_with_greaterthan_radius(self):
# Define a circle 'c' with radius 1000.1, and check
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive".
with [Link](ValueError) as e:
c = Circle(1000.1)
[Link](str([Link]), 'radius must be between 0 and 1000
inclusive')
def test_creating_circle_with_nonnumeric_radius(self):
# Define a circle 'c' with radius 'hello' and check
# if it raises a TypeError with the message
# "radius must be a number".
with [Link](TypeError) as e:
c = Circle('hello')
[Link](str([Link]), 'radius must be a number')
class TestCircleArea([Link]):
def test_circlearea_with_random_numeric_radius(self):
c1=Circle(2.5)
[Link]([Link](),19.63)
def test_circlearea_with_min_radius(self):
c2=Circle(0)
[Link]([Link](),0.0)
def test_circlearea_with_max_radius(self):
c3=Circle(1000.1)
#print([Link]())
[Link]([Link](),3142221.0)
class TestCircleCircumference([Link]):
def test_circlecircum_with_random_numeric_radius(self):
c1 = Circle(2.5)
[Link]([Link](),15.71)
def test_circlecircum_with_min_radius(self):
# Define a circle 'c2' with radius 0 and check if
# it's circumference is 0.
c2 = Circle(0)
[Link]([Link](),0)
def test_circlecircum_with_max_radius(self):
# Define a circle 'c3' with radius 1000 and check if
# it's circumference is 6283.19.
c3 = Circle(1000)
[Link]([Link](),6283.19)
def __init__(self, radius):
try:
if not isinstance(radius, (int, float)):
raise TypeError
elif 1000 >=radius>=0:
[Link]=radius
else:
raise ValueError
except ValueError:
raise ValueError("radius must be between 0 and 1000 inclusive")
except TypeError:
raise TypeError("radius must be a number")