Ühel kenal päeval, T, 2009-07-07 kell 13:28, kirjutas Christophe: > Aaron S. Meurer a écrit : > > > > Interval(3, 4, endpoints='oc'), which would create (3, 4] ('oc' stands > > for open-closed).
How about using '[' for closed and ']' instead of 'o' and 'c'? I find '[]' or '][' a bit more intuitive, as it took a bit time to figure out that 'oo' means open-open interval, not infinity.. So: cc = '[]', oc = ']]', oo = '][' and co = '[['. > Hello here is a first try fopr working with sets. This code has been > writing directly as I was thinking of what I neede. I think that it > could be better. I'll work on unions of intervals : ythis would be > simply a list of single intervals with additional methods so as to > simplify a union. For the moment, only the intersection has been made. I > hope that I didn't forget something. > > Every kind of suggestion is welcome. Some of the comments below are influenced by "PEP 8 -- Style Guide for Python Code" - http://www.python.org/dev/peps/pep-0008/ :) > #!/usr/bin/env python > #coding=utf-8 > > > # Code writing by BAL Christophe > # Mail : projet...@club.fr > > class Interval(): > def __init__(self, a, b, endpoints ='cc'): > if endpoints not in ['cc', 'oc', 'co', 'oo']: > raise Exception, 'Unkown endpoints of interval : "' + > str(endpoints) + '"' please use raise Exception (...) ...and note the grammar and typo. > > self.endpoints = endpoints > > if a>b: Please use proper spacing here: a > b... > self.min,self.max = b,a ...and here... > self.endpoints_min, self.endpoints_max = endpoints[1], > endpoints[0] > else: > self.min = a > self.max = b > self.endpoints_min = endpoints[0] > self.endpoints_max = endpoints[1] self.endpoints handling looks broken - they are not reversed here ^^ > > def __str__(self): > if self.min == self.max: > if self.endpoints == 'cc': > return '{' + str(self.min) + '}' Should we use [ or { ?? > else: > return 'Empty Set' > > if self.endpoints_min=='c': ..spacing.. > t = '[' > else: > t =']' > > t+=str(self.min) + ';' + str(self.max) ... > if self.endpoints_max=='c': spacing > t += ']' > else: > t +='[' > > return t > > def intersection(self, otherInterval): Please add docstring... > # The intersection is empty. > if self.min == self.max or otherInterval.min == otherInterval.max: > return Interval(0, 0, 'oo') > > maxOfMins = max(self.min, otherInterval.min) > minOfMaxs = min(self.max, otherInterval.max) > > if maxOfMins > minOfMaxs: > return Interval(0, 0, 'oo') > > if self.min == self.max or otherInterval.min == otherInterval.max: > return Interval(0, 0, 'oo') > > # The intersection could be a set with a single element. > if minOfMaxs == maxOfMins: > if self.max == minOfMaxs: > if self.endpoints_max == 'o' or otherInterval.endpoints_min > =='o': > return Interval(0, 0, 'oo') > else: > return Interval(minOfMaxs, minOfMaxs) > > else: > if self.endpoints_min == 'o' or otherInterval.endpoints_max > =='o': > return Interval(0, 0, 'oo') > else: > return Interval(minOfMaxs, minOfMaxs) > > # Wath is the endpoints of the intersection ? > answerendpoints_min = 'c' > > if self.min == maxOfMins : spacing... > answerendpoints_min = self.endpoints_min > > if otherInterval.min == maxOfMins and answerendpoints_min == 'c': > answerendpoints_min = otherInterval.endpoints_min > > answerendpoints_max = 'c' > > if self.max == minOfMaxs : spacing.. > answerendpoints_max = self.endpoints_max > > if otherInterval.max == minOfMaxs and answerendpoints_max == 'c': > answerendpoints_max = otherInterval.endpoints_max > > return Interval(maxOfMins, minOfMaxs, > answerendpoints_min+answerendpoints_max) spacing... Also, I would prefer to have lower-case variables: minOfMaxs vs min_of_max Cheers, Priit :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---