Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ OPS = {
def answer(question):
question = question.removeprefix("What is").removesuffix("?").strip()
if not question: raise ValueError("syntax error")
if question.isdigit(): return int(question)

if question.startswith("-") and question[1:].isdigit():
return -int(question[1:])
elif question.isdigit():
return int(question)

found_op = False
for name, op in OPS.items():
Expand Down Expand Up @@ -56,8 +60,9 @@ The method calls are [chained][method-chaining], so that the output from one cal
If the input has no characters left,
it uses the [falsiness][falsiness] of an empty string with the [`not`][not] operator to return a `ValueError("syntax error")`.

Next, the [`isdigit`][isdigit] method is used to see if the remaining characters in the input are digits.
If so, it uses the [`int()`][int-constructor] constructor to return the string as an integer.
Next, the [`str.startswith()`][startswith] and [`isdigit`][isdigit] methods are used to see if the remaining characters in the input are either negative or positive digits.
Because "-" is used to denote negative numbers, `str.startswith("-")` is used in the first condition and `question[1:].isdigit()` is then used for the remaining string.
If the `str.isdigit()` checks pass, the [`int()`][int-constructor] constructor is used to return the string as an integer with the proper sign.

Next, the elements in the `OPS` dictionary are iterated over.
If the key name is in the input, then the [`str.replace`][replace] method is used to replace the name in the input with the `dunder-method` value.
Expand Down Expand Up @@ -94,5 +99,6 @@ When the loop exhausts, the first element of the list is selected as the functio
[replace]: https://docs.python.org/3/library/stdtypes.html?#str.replace
[split]: https://docs.python.org/3/library/stdtypes.html?#str.split
[strip]: https://docs.python.org/3/library/stdtypes.html#str.strip
[startswith]: https://docs.python.org/3/library/stdtypes.html#str.startswith
[unpacking]: https://treyhunner.com/2018/10/asterisks-in-python-what-they-are-and-how-to-use-them/
[unpacking-and-multiple-assignment]: https://exercism.org/tracks/python/concepts/unpacking-and-multiple-assignment
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@
from operator import add, mul, sub
from operator import floordiv as div


OPERATIONS = {"plus": add, "minus": sub, "multiplied": mul, "divided": div}


def answer(question):
if not question.startswith("What is") or "cubed" in question:
raise ValueError("unknown operation")

question = question.removeprefix("What is").removesuffix("?").strip()

if question.isdigit():
return int(question)

if not question:
if not question:
raise ValueError("syntax error")


if (question.startswith("-") and question[1:].isdigit()) or question.isdigit():
return int(question)

equation = [word for word in question.split() if word != 'by']

while len(equation) > 1:
try:
x_value, operation, y_value, *rest = equation
equation = [OPERATIONS[operation](int(x_value), int(y_value)),
*rest]
except:
raise ValueError("syntax error")

return equation[0]
```

Expand Down
10 changes: 7 additions & 3 deletions exercises/practice/wordy/.approaches/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def answer(question):

question = question.removeprefix("What is").removesuffix("?").strip()

if question.isdigit():
if (question.startswith("-") and question[1:].isdigit()) or question.isdigit():
return int(question)

if not question:
Expand Down Expand Up @@ -286,7 +286,7 @@ def answer(question):

question = question.removeprefix("What is").removesuffix("?").strip()

if question.isdigit():
if (question.startswith("-") and question[1:].isdigit()) or question.isdigit():
return int(question)

if not question:
Expand Down Expand Up @@ -422,7 +422,11 @@ OPS = {
def answer(question):
question = question.removeprefix("What is").removesuffix("?").strip()
if not question: raise ValueError("syntax error")
if question.isdigit(): return int(question)

if question.startswith("-") and question[1:].isdigit():
return -int(question[1:])
elif question.isdigit():
return int(question)

found_op = False
for name, op in OPS.items():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def answer(question):

question = question.removeprefix("What is").removesuffix("?").strip()

if question.isdigit():
if (question.startswith("-") and question[1:].isdigit()) or question.isdigit():
return int(question)

if not question:
Expand Down Expand Up @@ -67,7 +67,7 @@ def answer(question):

question = question.removeprefix("What is").removesuffix("?").strip()

if question.isdigit():
if (question.startswith("-") and question[1:].isdigit()) or question.isdigit():
return int(question)

if not question:
Expand Down
Loading