Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Re: Range function
Here's the Python solution.

----------
# -*- coding: utf-8 -*-
# Python

# http://xahlee.org/tree/tree.html
# Xah Lee, 2005-05

# implementation note: When iStep is a decimal, rounding error
# accumulates. For example, the last item returned from
# Range(0,18,0.3) is 17.7 not 18. A remedy is to turn iStep into a
# fraction and do exact arithmetics, and possibly convert the result
# back to decimal. A lesser workaround is to split the interval as to
# do multiple smaller range and join them together.

def Range(iMin, iMax=None, iStep=None):
if (iMax==None and iStep==None):
return Range(1,iMin)
if iStep==None:
return Range(iMin,iMax,1)
if iMin <= iMax and iStep > 0:
if (isinstance(iStep,int) or isinstance(iStep,long)):
return range( iMix, iMax, iStep)
else:
result=[];temp=iStep
while iMin <= iMax:
result.append(iMin)
iMin += iStep
return result

# test
print Range(0, 18, 0.3)


Report this thread to moderator Post Follow-up to this message
Old Post
Xah Lee
05-15-05 01:57 PM


Re: Range function
Xah Lee wrote:
> Here's the Python solution.
> # implementation note: When iStep is a decimal, rounding error
> # accumulates. For example, the last item returned from
> # Range(0,18,0.3) is 17.7 not 18. A remedy is to turn iStep into a
> # fraction and do exact arithmetics, and possibly convert the result
> # back to decimal. A lesser workaround is to split the interval as to
> # do multiple smaller range and join them together.

Good lord no!  The correct way is to use an integer count and simply
multiply it each time by the step, and add that to the range.  No
accumulation of errors then.  Where did you learn to program?
(Rhetorical question of course, as you haven't, yet.)

> def Range(iMin, iMax=None, iStep=None):
>   if (iMax==None and iStep==None):
>     return Range(1,iMin)
>   if iStep==None:
>     return Range(iMin,iMax,1)
>   if iMin <= iMax and iStep > 0:
>     if (isinstance(iStep,int) or isinstance(iStep,long)):
>       return range( iMix, iMax, iStep)
>     else:
>       result=[];temp=iStep
>       while iMin <= iMax:
>         result.append(iMin)
>         iMin += iStep
>       return result

That's some of the worst Python code I've seen recently.  Please, no one
take this as representative of how decent Python programmers write code.

-Peter

Report this thread to moderator Post Follow-up to this message
Old Post
Peter Hansen
05-15-05 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Scheme archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:57 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.