dhellmann / google-highly-open-participation-psf

Automatically exported from code.google.com/p/google-highly-open-participation-psf
0 stars 0 forks source link

Implement rosettacode.org math-related programming tasks with Python #133

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
RosettaCode.org is a wiki containing implementations of common
algorithms in multiple languages.  Many of the pages include Python
sample code, but some do not.  To complete this task, submit
implementations for the algorithms listed below.

- http://rosettacode.org/rosettacode/w/index.php?title=Bitwise_operations
- http://rosettacode.org/rosettacode/w/index.php?title=Logical_operations
- http://rosettacode.org/rosettacode/w/index.php?title=MD5
- http://rosettacode.org/rosettacode/w/index.php?title=Prime_numbers

Completion:

Edit the RosettaCode.org wiki to add a Python example to each of the 
pages listed above.  Once all of your examples are posted, add a
comment here and send an email to the ghop-python mailing list.  All
of the code samples will be tested before this task is marked as
completed, so check your work!

Other information:

Please read the instructions for contributing content to
RosettaCode.org
(http://rosettacode.org/rosettacode/w/index.php?title=Help:Contribute_Content
and
http://rosettacode.org/rosettacode/w/index.php?title=Help:Adding_a_new_programmi
ng_example)
before posting anything to the site.

Feel free to ask for help via email on the ghop-python list or
comments on this task.  The existing sample code for other languages
may not be the best possible solution for Python, but should give you
an idea of how to implement the algorithms.  All of the programming
tasks listed above can be implemented using native language operations
or standard library modules.

Task duration: please complete this task within 5 days (120 hours) of claiming 
it.

Original issue reported on code.google.com by the.good...@gmail.com on 26 Nov 2007 at 6:26

GoogleCodeExporter commented 9 years ago
I claim this task.

Original comment by mgsl...@gmail.com on 28 Nov 2007 at 8:53

GoogleCodeExporter commented 9 years ago
Task Completed.

- http://rosettacode.org/rosettacode/w/index.php?title=Bitwise_operations#Python
- http://rosettacode.org/rosettacode/w/index.php?title=Logical_operations#Python
- http://rosettacode.org/rosettacode/w/index.php?title=MD5#Python
- http://rosettacode.org/rosettacode/w/index.php?title=Prime_numbers#Python

Tests:

bitwise(4, 7)

yields:
a and b: 4
a or b: 7
a xor b: 3
not a: -5

logic(False, True)

yields:
a and b: False
a or b: True
not a: True

print [prime(1), prime(2), prime(3), prime(4), prime(5), prime(73), prime(77),
prime(113)]

yields:
[False, True, True, False, True, True, False, True]

It seemed a bit odd to me that the MD5 examples for all the languages simply 
used
their library MD5 functions.  It would be much more illustrative if they 
actually
showed the algorithm implemented in each.  Since all the other languages just 
called
lib functions, I did as well.

Original comment by mgsl...@gmail.com on 28 Nov 2007 at 9:51

GoogleCodeExporter commented 9 years ago
Hi, Michael,

The keyword operators ("and", "or", "not") actually do "logical" operations, 
rather
than bitwise operations on their arguments.  Have a look at
http://docs.python.org/ref/bitwise.html for more info on the bitwise operators.

Doug

Original comment by doug.hel...@gmail.com on 28 Nov 2007 at 12:06

GoogleCodeExporter commented 9 years ago
D'oh, I should have read the actual rosettacode.org pages!  Sorry about that; 
your
solutions all look good.  For what it's worth, you don't have to convert values 
to
strings before printing them.  That happens "for free".  But it isn't wrong to 
do it.

Original comment by doug.hel...@gmail.com on 28 Nov 2007 at 1:12

GoogleCodeExporter commented 9 years ago
As far as the MD5 examples go, I think the point is to show that the language 
has an
easy-to-use facility for calculating the checksums, rather than illustrating 
how to
implement complex mathematical algorithms in the language.  The algorithm 
doesn't
change that much, but the API for the MD5 library might be a little different.

Original comment by doug.hel...@gmail.com on 28 Nov 2007 at 1:16

GoogleCodeExporter commented 9 years ago
When I run your prime number function under Python 2.5.1, I see a 
DeprecationWarning
because range() doesn't want a floating point value as an argument.

That list comprehension will creates a list of all of the candidate factors 
before
any() is ever called.  If you use a generator instead, the first time an actual
factor is identified, any() will return True and skip the rest of the values.  
It
also won't hold the list of numbers in memory all at one time.

Original comment by doug.hel...@gmail.com on 28 Nov 2007 at 1:35

GoogleCodeExporter commented 9 years ago

Original comment by doug.hel...@gmail.com on 28 Nov 2007 at 1:37

GoogleCodeExporter commented 9 years ago
Ah, yes, I neglected to notice the warning.  I'm not sure exactly why it is 
triggered
- the two operands of min are ints - ceil and a.  This is why I don't 
particularly
relish dynamic checking..  The error doesn't tell me where the issue is, either.

Oh, I assumed that list comprehensions were generators.  I suppose I've gotten 
some
bad habits from Haskell's laziness :)  It really seems like the easiest way to
implement comprehensions would be generators, internally.  Ah, I see that 
generator
expressions are essentially comprehensions without the [].

Original comment by mgsl...@gmail.com on 28 Nov 2007 at 10:39

GoogleCodeExporter commented 9 years ago

Original comment by doug.hel...@gmail.com on 28 Nov 2007 at 11:58

GoogleCodeExporter commented 9 years ago
I've fixed the warning and switched to a generator on the prime, as per our IRC
discussion.

Thanks!

Original comment by BotBuil...@gmail.com on 29 Nov 2007 at 12:22

GoogleCodeExporter commented 9 years ago
Looks good.

Original comment by doug.hel...@gmail.com on 29 Nov 2007 at 12:30