# Requesting a code review

**URL:** <https://discuss.python.org/t/requesting-a-code-review/20107>\
**Category:** Python Help\
**Tags:** help, new-users\
**Created:** [October 19, 2022, 4:33am UTC](https://discuss.python.org/t/requesting-a-code-review/20107 "2022-10-19T04:33:56Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![Denise\_unique](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/denise_unique/32/13289_2.png) [@Denise\_unique](https://discuss.python.org/u/Denise_unique)\
**Post date:** [October 31, 2022, 6:53am UTC](https://discuss.python.org/t/requesting-a-code-review/20107/21 "2022-10-31T06:53:27Z")

</div>

ic is short for icecream: Never use print() to debug again  
link: [GitHub - gruns/icecream: 🍦 Never use print() to debug again.](https://github.com/gruns/icecream)

So now we are talking about my algorithm, so I created a plan for you to choose from. No matter what you choose. Thank you. Really, thank you @cameron

Plan A  
I will try to apply the things I learned from the discussions and rewrite the code. which I assume will take weeks or months.

Plan B  
I will use icecream to explain the algorithm of the calculator in an implicit way? mix with comments to try to be explicit.

Plan C  
I really do respect you and am grateful for all the wisdom you shared with me, but I feel like I bothered you for far too long. I really respect and understand if we were going to end it here.

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [October 31, 2022, 9:19pm UTC](https://discuss.python.org/t/requesting-a-code-review/20107/22 "2022-10-31T21:19:42Z")

</div>

> ic is short for icecream: Never use print() to debug again  
> link: [GitHub - gruns/icecream: 🍦 Never use print() to debug again.](https://github.com/gruns/icecream)

Ah, ok.

> So now we are talking about my algorithm, so I created a plan for you  
> to choose from. No matter what you choose. Thank you. Really, thank you  
> @cameron
> 
> Plan A  
> I will try to apply the things I learned from the discussions and rewrite the code. which I assume will take weeks or months.

I vote for Plan A.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![Denise\_unique](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/denise_unique/32/13289_2.png) [@Denise\_unique](https://discuss.python.org/u/Denise_unique)\
**Post date:** [October 31, 2022, 10:15pm UTC](https://discuss.python.org/t/requesting-a-code-review/20107/23 "2022-10-31T22:15:00Z")

</div>

Okay, while I’m doing that, I will try to learn C along the way. I hope to talk to you again in the future! @cameron

---

<div class="post-metadata">

**Author:** ![Denise\_unique](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/denise_unique/32/13289_2.png) [@Denise\_unique](https://discuss.python.org/u/Denise_unique)\
**Post date:** [November 4, 2022, 2:01am UTC](https://discuss.python.org/t/requesting-a-code-review/20107/24 "2022-11-04T02:01:38Z")

</div>

@cameron

So I made a little progress, and now I’m starting to try to apply what I learned in this discussion, but I have one question.

> [@cameron](#):
>
> I’m going to suggest another change. The good times are back again.

What do you mean by “another change”?

Do you mean I should add nested brackets like your example (3.0 \* (4 + 7))?

I assume you didn’t run my code for security reasons. And I understand that, but my basic calculator algorithm is correct math-wise from my perspective because I tested it several times.

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [November 4, 2022, 10:11pm UTC](https://discuss.python.org/t/requesting-a-code-review/20107/25 "2022-11-04T22:11:32Z")

</div>

> So I made a little progress, and now I’m starting to try to apply what  
> I learned in this discussion, but I have one question.
> 
> > [@cameron](#):
> >
> > I’m going to suggest another change. The good times are back again.
> 
> What do you mean by “another change”?

I meant this:

The change I’m thinking of suggesting is to progressively reduce the  
expression until there is a single number left, or a syntax error (no  
matches). If you do that you don’t need to count brackets or anything -  
just consume them in their innermost pairs until you can’t.

So your order of evaluation precedence is something like:

- ( number op number )
- number \* number
- number / number
- number + number
- number - number

> Do you mean I should add nested brackets like your example (3.0 \* (4 + 7))?

No, i meant to treat the task as a symbol manipulation problem, not a  
“math” problem. Which you’re kind of doing already. But by selecting the  
“most tightly bound” expressions from the _entire_ string, one at a  
time, you can reduce the string in steps. At the end you _either_ have a  
result, _or_ you have a syntax error. And you don’t need to write very  
complex regexps to do it.

> I assume you didn’t run my code for security reasons.

You code seemed benign - that wasn’t it. I was just looking at it  
semanticly instead of “running” it.

> And I understand that, but my basic calculator algorithm is correct  
> math-wise from my perspective because I tested it several times.

The operations are. I was less convinced that the high level precedence  
(MDAS) stuff was, because of the way your regexp tests were written.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![Denise\_unique](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/denise_unique/32/13289_2.png) [@Denise\_unique](https://discuss.python.org/u/Denise_unique)\
**Post date:** [November 4, 2022, 11:51pm UTC](https://discuss.python.org/t/requesting-a-code-review/20107/26 "2022-11-04T23:51:33Z")

</div>

@cameron

I will try to change the algorithm without using regex.

> [@cameron](#):
>
> But by selecting the  
> “most tightly bound” expressions from the _entire_ string, one at a  
> time, you can reduce the string in steps. At the end you _either_ have a  
> result, _or_ you have a syntax error. And you don’t need to write very  
> complex regexps to do it.

That is what my algorithm does, e.g., 47 + 478 \* 7, which will get the second priority in this case, 478 \* 7. Then it will calculate it, which equals 3346. The last step is to replace the string with the result, which is equal to 47 + 3346, then repeat. The result is 3393.

But. Yeah, I can solve this without using regex, which I originally used with arrays when I tried to create the algorithm; that’s why I said I couldn’t remember how many times I changed the algorithm, and it feels like it was a month before I used regex. Probably because I did not try using while loops to calculate each one one by one, this may be the reason I have had a lot of problems in the past.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [November 5, 2022, 1:16am UTC](https://discuss.python.org/t/requesting-a-code-review/20107/27 "2022-11-05T01:16:44Z")

</div>

The usual method of parsing expressions, and programming languages in general, is by “recursive descent”. Even regexes are parsed by recursive descent.

---

<div class="post-metadata">

**Author:** ![Denise\_unique](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/denise_unique/32/13289_2.png) [@Denise\_unique](https://discuss.python.org/u/Denise_unique)\
**Post date:** [November 5, 2022, 3:56am UTC](https://discuss.python.org/t/requesting-a-code-review/20107/28 "2022-11-05T03:56:26Z")

</div>

That seems too complex for me. I’m just going to try to use arrays or lists to solve my problem. But thank you @MRAB for the information. much appriciated.

---

<div class="post-metadata">

**Author:** ![Denise\_unique](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/denise_unique/32/13289_2.png) [@Denise\_unique](https://discuss.python.org/u/Denise_unique)\
**Post date:** [November 25, 2022, 8:48am UTC](https://discuss.python.org/t/requesting-a-code-review/20107/29 "2022-11-25T08:48:15Z")

</div>

@cameron  
Just an update: I’ve already done, if not almost done, the calculator algorithm without using regex. I just have to clean it up. but I’m not going to post it yet! I still want to apply what I learned here to my whole code, but I can’t wait to post it and learn more!

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [November 25, 2022, 9:09am UTC](https://discuss.python.org/t/requesting-a-code-review/20107/30 "2022-11-25T09:09:52Z")

</div>

Good to hear! - Cameron

---

<div class="post-metadata">

**Author:** ![Denise\_unique](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/denise_unique/32/13289_2.png) [@Denise\_unique](https://discuss.python.org/u/Denise_unique)\
**Post date:** [December 24, 2022, 11:09am UTC](https://discuss.python.org/t/requesting-a-code-review/20107/31 "2022-12-24T11:09:29Z")

</div>

So I found myself being lazy for more than 20 days, and I decided to stop the project altogether. The reason is that I exhaust myself overthinking how to be verbose about my code. But as I said, I’ve done the calculator algorithm but not cleaned. I will post it if you want to know my code’s outcome. The knowledge I learned from you is super significant to my dream career, so thank you very much and happy holidays! @cameron

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [December 24, 2022, 9:24pm UTC](https://discuss.python.org/t/requesting-a-code-review/20107/32 "2022-12-24T21:24:06Z")

</div>

> So I found myself being lazy for more than 20 days, and I decided to  
> stop the project altogether. The reason is that I exhaust myself  
> overthinking how to be verbose about my code.

Aye. Sometimes you need to recognise this situation, stick in a TODO  
comment or something about tidying things up later, and Just Make It  
Work for the rest of the code. Then come back later to something like  
verbosity.

One advantage is that you might find it easier to tune “verbosity” after  
you’ve got the code running and have some real world feel for what’s  
useful verbiage and what isn’t.

> But as I said, I’ve done the calculator algorithm but not cleaned. I  
> will post it if you want to know my code’s outcome. The knowledge I  
> learned from you is super significant to my dream career, so thank you  
> very much and happy holidays! @cameron

I’m glad you’ve found it useful.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![Denise\_unique](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/denise_unique/32/13289_2.png) [@Denise\_unique](https://discuss.python.org/u/Denise_unique)\
**Post date:** [December 25, 2022, 6:21am UTC](https://discuss.python.org/t/requesting-a-code-review/20107/33 "2022-12-25T06:21:32Z")

</div>

That’s really helpful; it’s like making the ingredients for your cake, and after you’ve baked it, you taste it to make sure it isn’t too sweet. thank you.

[Previous page](https://discuss.python.org/t/requesting-a-code-review/20107.md?page=1)
