Closed GoogleCodeExporter closed 9 years ago
I claim this task.
Original comment by mgsl...@gmail.com
on 28 Nov 2007 at 8:53
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
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
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
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
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
Original comment by doug.hel...@gmail.com
on 28 Nov 2007 at 1:37
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
Original comment by doug.hel...@gmail.com
on 28 Nov 2007 at 11:58
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
Looks good.
Original comment by doug.hel...@gmail.com
on 29 Nov 2007 at 12:30
Original issue reported on code.google.com by
the.good...@gmail.com
on 26 Nov 2007 at 6:26