Skip to content

Commit 4541ebe

Browse files
authored
[Wordy]: Updated Code in Approaches Docs (exercism#3926)
[no important files changed] * Updated code in wordy approaches for new test cases.
1 parent f0125e4 commit 4541ebe

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

exercises/practice/wordy/.approaches/dunder-getattribute/content.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ OPS = {
1313
def answer(question):
1414
question = question.removeprefix("What is").removesuffix("?").strip()
1515
if not question: raise ValueError("syntax error")
16-
if question.isdigit(): return int(question)
16+
17+
if question.startswith("-") and question[1:].isdigit():
18+
return -int(question[1:])
19+
elif question.isdigit():
20+
return int(question)
1721

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

59-
Next, the [`isdigit`][isdigit] method is used to see if the remaining characters in the input are digits.
60-
If so, it uses the [`int()`][int-constructor] constructor to return the string as an integer.
63+
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.
64+
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.
65+
If the `str.isdigit()` checks pass, the [`int()`][int-constructor] constructor is used to return the string as an integer with the proper sign.
6166

6267
Next, the elements in the `OPS` dictionary are iterated over.
6368
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.
@@ -94,5 +99,6 @@ When the loop exhausts, the first element of the list is selected as the functio
9499
[replace]: https://docs.python.org/3/library/stdtypes.html?#str.replace
95100
[split]: https://docs.python.org/3/library/stdtypes.html?#str.split
96101
[strip]: https://docs.python.org/3/library/stdtypes.html#str.strip
102+
[startswith]: https://docs.python.org/3/library/stdtypes.html#str.startswith
97103
[unpacking]: https://treyhunner.com/2018/10/asterisks-in-python-what-they-are-and-how-to-use-them/
98104
[unpacking-and-multiple-assignment]: https://exercism.org/tracks/python/concepts/unpacking-and-multiple-assignment

exercises/practice/wordy/.approaches/import-callables-from-operator/content.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@
55
from operator import add, mul, sub
66
from operator import floordiv as div
77

8+
89
OPERATIONS = {"plus": add, "minus": sub, "multiplied": mul, "divided": div}
910

11+
1012
def answer(question):
1113
if not question.startswith("What is") or "cubed" in question:
1214
raise ValueError("unknown operation")
13-
15+
1416
question = question.removeprefix("What is").removesuffix("?").strip()
1517

16-
if question.isdigit():
17-
return int(question)
18-
19-
if not question:
18+
if not question:
2019
raise ValueError("syntax error")
21-
20+
21+
if (question.startswith("-") and question[1:].isdigit()) or question.isdigit():
22+
return int(question)
23+
2224
equation = [word for word in question.split() if word != 'by']
23-
25+
2426
while len(equation) > 1:
2527
try:
2628
x_value, operation, y_value, *rest = equation
2729
equation = [OPERATIONS[operation](int(x_value), int(y_value)),
2830
*rest]
2931
except:
3032
raise ValueError("syntax error")
31-
33+
3234
return equation[0]
3335
```
3436

exercises/practice/wordy/.approaches/introduction.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def answer(question):
182182

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

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

188188
if not question:
@@ -286,7 +286,7 @@ def answer(question):
286286

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

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

292292
if not question:
@@ -422,7 +422,11 @@ OPS = {
422422
def answer(question):
423423
question = question.removeprefix("What is").removesuffix("?").strip()
424424
if not question: raise ValueError("syntax error")
425-
if question.isdigit(): return int(question)
425+
426+
if question.startswith("-") and question[1:].isdigit():
427+
return -int(question[1:])
428+
elif question.isdigit():
429+
return int(question)
426430

427431
found_op = False
428432
for name, op in OPS.items():

exercises/practice/wordy/.approaches/lambdas-in-a-dictionary/content.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def answer(question):
1616

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

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

2222
if not question:
@@ -67,7 +67,7 @@ def answer(question):
6767

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

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

7373
if not question:

0 commit comments

Comments
 (0)