Last active
June 11, 2018 10:06
-
-
Save thomwolf/0709b5a72cf3620cd00d94791213d38e to your computer and use it in GitHub Desktop.
A Python loop on a list of Python objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import random | |
class Rectangle: | |
def __init__(self, w, h): | |
self.w = w | |
self.h = h | |
def area(self): | |
return self.w * self.h | |
def check_rectangles(rectangles, threshold): | |
n_out = 0 | |
for rectangle in rectangles: | |
if rectangle.area() > threshold: | |
n_out += 1 | |
return n_out | |
def main(): | |
n_rectangles = 10000000 | |
rectangles = list(Rectangle(random(), random()) for i in range(n_rectangles)) | |
n_out = check_rectangles(rectangles, threshold=0.25) | |
print(n_out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment