import math
import random
COLOURS = [ 'black' , 'yellow' , 'red' , 'magenta' , 'cyan' , 'purple' ]
class Square( object ) :
def __init__ ( self , side, colour= "black" ) :
self .side = side
self .colour = colour
@ classmethod
def generate_random( cls) :
side = random .random ( ) * 100
colour = COLOURS[ random .randint ( 0 , len ( COLOURS) - 1 ) ]
return cls( side, colour)
@ property
def area( self ) :
return self .side ** 2
@ property
def perimeter( self ) :
return self .side * 4
def __repr__ ( self ) :
return "%s square with the side: %.2f, area: %.2f, perimeter: %.2f" % (
self .colour .title ( ) , self .side , self .area , self .perimeter )
class Circle( object ) :
def __init__ ( self , radius, colour= "black" ) :
self .radius = radius
self .colour = colour
@ classmethod
def generate_random( cls) :
radius = random .random ( ) * 100
colour = COLOURS[ random .randint ( 0 , len ( COLOURS) - 1 ) ]
return cls( radius, colour)
@ property
def area( self ) :
return self .radius ** 2 * math .pi
@ property
def perimeter( self ) :
return self .radius * 2 * math .pi
def __repr__ ( self ) :
return "%s circle with the radius: %.2f, area: %.2f, perimeter: %.2f" % (
self .colour .title ( ) , self .radius , self .area , self .perimeter )
classes = [ Square, Circle]
figs = [ ]
numfigs = 10
for i in range ( numfigs) :
fig = classes[ random .randint ( 0 , len ( classes) - 1 ) ]
figs.append ( fig.generate_random ( ) )
for fig in figs:
print ( fig)
aW1wb3J0IG1hdGgKaW1wb3J0IHJhbmRvbQoKQ09MT1VSUyA9IFsnYmxhY2snLCAneWVsbG93JywgJ3JlZCcsICdtYWdlbnRhJywgJ2N5YW4nLCAncHVycGxlJ10KCmNsYXNzIFNxdWFyZShvYmplY3QpOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHNpZGUsIGNvbG91cj0iYmxhY2siKToKICAgICAgICBzZWxmLnNpZGUgPSBzaWRlCiAgICAgICAgc2VsZi5jb2xvdXIgPSBjb2xvdXIKICAgIAogICAgQGNsYXNzbWV0aG9kCiAgICBkZWYgZ2VuZXJhdGVfcmFuZG9tKGNscyk6CiAgICAgICAgc2lkZSA9IHJhbmR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LnBpCiAgICAKICAgIEBwcm9wZXJ0eQogICAgZGVmIHBlcmltZXRlcihzZWxmKToKICAgICAgICByZXR1cm4gc2VsZi5yYWRpdXMgKiAyICogbWF0aC5waQogICAgCiAgICBkZWYgX19yZXByX18oc2VsZik6CiAgICAgICAgcmV0dXJuICIlcyBjaXJjbGUgd2l0aCB0aGUgcmFkaXVzOiAlLjJmLCBhcmVhOiAlLjJmLCBwZXJpbWV0ZXI6ICUuMmYiICUgKAogICAgICAgICAgICBzZWxmLmNvbG91ci50aXRsZSgpLCBzZWxmLnJhZGl1cywgc2VsZi5hcmVhLCBzZWxmLnBlcmltZXRlcikKCmNsYXNzZXMgPSBbU3F1YXJlLCBDaXJjbGVdCgoKZmlncyA9IFtdCm51bWZpZ3MgPSAxMApmb3IgaSBpbiByYW5nZShudW1maWdzKToKICAgIGZpZyA9IGNsYXNzZXNbcmFuZG9tLnJhbmRpbnQoMCwgbGVuKGNsYXNzZXMpIC0gMSldCiAgICBmaWdzLmFwcGVuZChmaWcuZ2VuZXJhdGVfcmFuZG9tKCkpCgoKZm9yIGZpZyBpbiBmaWdzOgogICAgcHJpbnQoZmlnKQ==
stdout
Cyan square with the side: 25.46, area: 648.45, perimeter: 101.86
Black square with the side: 31.15, area: 970.18, perimeter: 124.59
Black square with the side: 64.85, area: 4205.88, perimeter: 259.41
Black circle with the radius: 58.15, area: 10623.88, perimeter: 365.38
Yellow square with the side: 65.48, area: 4287.10, perimeter: 261.90
Black circle with the radius: 12.90, area: 522.62, perimeter: 81.04
Black circle with the radius: 42.91, area: 5784.75, perimeter: 269.62
Yellow circle with the radius: 58.98, area: 10928.17, perimeter: 370.58
Yellow circle with the radius: 66.43, area: 13862.94, perimeter: 417.38
Red square with the side: 98.27, area: 9656.36, perimeter: 393.07