+ 2

Trying to figure out why this code won't work for the "Mathematics" Code Coach

I've been working on this code for hours. I know about eval(), but I'm trying to give myself a challenge by using Regular Expressions with the Code Coach. No matter what I try, I still fail Trial #4, and I have no idea why. At first, I thought maybe a parenthesis was getting stuck in there, but that isn't the case. Then I thought maybe the developers added negative numbers to trip me up, but I added code to handle negative numbers, and it still won't work. I just have no idea what to try now. Any ideas what I could be missing? I'm happy to figure out the code on my own, I just don't know what the code isn't grabbing from the input. Thoughts? https://code.sololearn.com/cqC1wuaMffHA/?ref=app

23rd Apr 2022, 3:33 AM
Jared Glenn
6 Respuestas
+ 2
That helps!
23rd Apr 2022, 7:14 AM
Oma Falk
Oma Falk - avatar
+ 2
There are a few things to mention, none of which are guaranteed to solve your problem, though :). Why? Because I have a hard time predicting what your code does from just looking at it. I need to run it to get a better understanding of where the flaw might be. Anyway ... my neckhair always goes up when I see people modifying the list they iterate over. This is never good. What you do, in at least one case, is just modyfying some elements. Well, that is what maps are for. You can do list comprehension to run a function over each element and turn it into a list again, or use the map function. To have access to the index of the element, you can enumerate the object. No need to use index method. And, as mentioned, don't delete out under the iterator! I would suggest a different approach: l = [resultOf(expr) for expr in ipt.split()] This evaluates each subexpression, if the do not contain whitespaces! Now, all you need to worry about is how to evaluate a single expression in resultOf. And evaluate l.
23rd Apr 2022, 8:23 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
Hi Oma, I can't seem to find a way to link the challenge. If you know how, let me know and I'll get it on here. In the meantime, it's a Code Coach challenge (not in a course) and it is "Hard" difficulty, if that helps. Thanks for looking into it.
23rd Apr 2022, 5:17 AM
Jared Glenn
+ 1
This is the text of the challenge, if you're unsure: Find which math expression matches the answer that you are given, if you have an integer answer, and a list of math expressions. Task: Test each math expression to find the first one that matches the answer that you are given. Input Format: Two inputs: an integer and a space separated string of math expressions. The following operations need to be supported: addition +, subtraction -, multiplication *, division /. An expression can include multiple operations. Output Format: A string that tells the index of the first math expression that matches. If there are no matches, output 'none'. Sample Input: 15 (2+100) (5*3) (14+1) Sample Output: index 1
23rd Apr 2022, 5:19 AM
Jared Glenn
+ 1
please link challenges
24th Apr 2022, 10:33 PM
Pls Dont Block Me
0
Please link the challenge
23rd Apr 2022, 4:52 AM
Oma Falk
Oma Falk - avatar