diff --git a/exercises/practice/wordy/.approaches/dunder-getattribute/content.md b/exercises/practice/wordy/.approaches/dunder-getattribute/content.md index 3f82c0ee51..167460f2d3 100644 --- a/exercises/practice/wordy/.approaches/dunder-getattribute/content.md +++ b/exercises/practice/wordy/.approaches/dunder-getattribute/content.md @@ -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(): @@ -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. @@ -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 diff --git a/exercises/practice/wordy/.approaches/import-callables-from-operator/content.md b/exercises/practice/wordy/.approaches/import-callables-from-operator/content.md index 9f57c1c9c6..9fdf3e20e0 100644 --- a/exercises/practice/wordy/.approaches/import-callables-from-operator/content.md +++ b/exercises/practice/wordy/.approaches/import-callables-from-operator/content.md @@ -5,22 +5,24 @@ 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 @@ -28,7 +30,7 @@ def answer(question): *rest] except: raise ValueError("syntax error") - + return equation[0] ``` diff --git a/exercises/practice/wordy/.approaches/introduction.md b/exercises/practice/wordy/.approaches/introduction.md index cc5e8031d9..821b122842 100644 --- a/exercises/practice/wordy/.approaches/introduction.md +++ b/exercises/practice/wordy/.approaches/introduction.md @@ -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: @@ -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: @@ -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(): diff --git a/exercises/practice/wordy/.approaches/lambdas-in-a-dictionary/content.md b/exercises/practice/wordy/.approaches/lambdas-in-a-dictionary/content.md index df06d3b171..d78f3e7db8 100644 --- a/exercises/practice/wordy/.approaches/lambdas-in-a-dictionary/content.md +++ b/exercises/practice/wordy/.approaches/lambdas-in-a-dictionary/content.md @@ -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: @@ -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: