Implement
String.eightBitSignedNumber(), which should return True if given object is a number representable by 8 bit signed integer (-128 to -1 or 0 to 127), false/False otherwise.
It should only accept numbers in canonical representation, so no leading
+, extra 0s, spaces etc.signed_eight_bit_number = lambda number: number in map(str, xrange(-128,128))
Where the
map function,map(f, iterable) is basically equivalent to: [f(x) for x in iterable].map on its own can't do a Cartesian product, because the length of its output list is always the same as its input list. You can trivially do a Cartesian product with a list comprehension though:[(a, b) for a in iterable_a for b in iterable_b]
The syntax is a little confusing -- that's basically equivalent to:
result = []
for a in iterable_a:
for b in iterable_b:
result.append((a, b))
Regular Expression
import re
SIGNED_BYTE_PATTERN = re.compile(r'''
\A
(?:
0 |
-? (?:
1 (?:
[01] \d? |
2 [0-7]? |
[3-9] )? |
[2-9] \d? ) |
-128 )
\Z
''', re.X)
def signed_eight_bit_number(number):
return bool(SIGNED_BYTE_PATTERN.search(number))
沒有留言:
張貼留言