Middle three digits: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Python}}: name change)
(→‎{{header|Python}}: Name changes)
Line 9: Line 9:


=={{header|Python}}==
=={{header|Python}}==
<lang python>>>> def missing_three_digits(i):
<lang python>>>> def middle_three_digits(i):
s = str(abs(i))
s = str(abs(i))
length = len(s)
length = len(s)
Line 20: Line 20:
>>> for x in passing + failing:
>>> for x in passing + failing:
try:
try:
answer = missing_three_digits(x)
answer = middle_three_digits(x)
except AssertionError as error:
except AssertionError as error:
answer = error
answer = error
print("missing_three_digits(%s) returned: %r" % (x, answer))
print("middle_three_digits(%s) returned: %r" % (x, answer))


missing_three_digits(123) returned: '123'
middle_three_digits(123) returned: '123'
missing_three_digits(12345) returned: '234'
middle_three_digits(12345) returned: '234'
missing_three_digits(1234567) returned: '345'
middle_three_digits(1234567) returned: '345'
missing_three_digits(987654321) returned: '654'
middle_three_digits(987654321) returned: '654'
missing_three_digits(10001) returned: '000'
middle_three_digits(10001) returned: '000'
missing_three_digits(-10001) returned: '000'
middle_three_digits(-10001) returned: '000'
missing_three_digits(-123) returned: '123'
middle_three_digits(-123) returned: '123'
missing_three_digits(-100) returned: '100'
middle_three_digits(-100) returned: '100'
missing_three_digits(100) returned: '100'
middle_three_digits(100) returned: '100'
missing_three_digits(-12345) returned: '234'
middle_three_digits(-12345) returned: '234'
missing_three_digits(1) returned: AssertionError('Need odd and >= 3 digits',)
middle_three_digits(1) returned: AssertionError('Need odd and >= 3 digits',)
missing_three_digits(2) returned: AssertionError('Need odd and >= 3 digits',)
middle_three_digits(2) returned: AssertionError('Need odd and >= 3 digits',)
missing_three_digits(-1) returned: AssertionError('Need odd and >= 3 digits',)
middle_three_digits(-1) returned: AssertionError('Need odd and >= 3 digits',)
missing_three_digits(-10) returned: AssertionError('Need odd and >= 3 digits',)
middle_three_digits(-10) returned: AssertionError('Need odd and >= 3 digits',)
missing_three_digits(2002) returned: AssertionError('Need odd and >= 3 digits',)
middle_three_digits(2002) returned: AssertionError('Need odd and >= 3 digits',)
missing_three_digits(-2002) returned: AssertionError('Need odd and >= 3 digits',)
middle_three_digits(-2002) returned: AssertionError('Need odd and >= 3 digits',)
missing_three_digits(0) returned: AssertionError('Need odd and >= 3 digits',)
middle_three_digits(0) returned: AssertionError('Need odd and >= 3 digits',)
>>> </lang>
>>> </lang>

Revision as of 03:30, 2 February 2013

Middle three digits is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The task is to

Write a function/procedure/subroutine that is called with an integer value and returns the middle three digits of the integer if possible or a clear indication of an error if this is not possible.

Your function should be tested with the following values; the first line should return valid answers, those of the second line should return clear indications of an error:

123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345
1, 2, -1, -10, 2002, -2002, 0

Show your output on this page.

Python

<lang python>>>> def middle_three_digits(i): s = str(abs(i)) length = len(s) assert length >= 3 and length % 2 == 1, "Need odd and >= 3 digits" mid = length // 2 return s[mid-1:mid+2]

>>> passing = [123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345] >>> failing = [1, 2, -1, -10, 2002, -2002, 0] >>> for x in passing + failing: try: answer = middle_three_digits(x) except AssertionError as error: answer = error print("middle_three_digits(%s) returned: %r" % (x, answer))


middle_three_digits(123) returned: '123' middle_three_digits(12345) returned: '234' middle_three_digits(1234567) returned: '345' middle_three_digits(987654321) returned: '654' middle_three_digits(10001) returned: '000' middle_three_digits(-10001) returned: '000' middle_three_digits(-123) returned: '123' middle_three_digits(-100) returned: '100' middle_three_digits(100) returned: '100' middle_three_digits(-12345) returned: '234' middle_three_digits(1) returned: AssertionError('Need odd and >= 3 digits',) middle_three_digits(2) returned: AssertionError('Need odd and >= 3 digits',) middle_three_digits(-1) returned: AssertionError('Need odd and >= 3 digits',) middle_three_digits(-10) returned: AssertionError('Need odd and >= 3 digits',) middle_three_digits(2002) returned: AssertionError('Need odd and >= 3 digits',) middle_three_digits(-2002) returned: AssertionError('Need odd and >= 3 digits',) middle_three_digits(0) returned: AssertionError('Need odd and >= 3 digits',) >>> </lang>