Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arithmetic Formatter (Certification Project) #231

Closed
beaucarnes opened this issue Nov 27, 2019 · 31 comments
Closed

Arithmetic Formatter (Certification Project) #231

beaucarnes opened this issue Nov 27, 2019 · 31 comments

Comments

@beaucarnes
Copy link
Member

Create project from the Python scientific computing certification.

@beaucarnes beaucarnes added this to Building Example Project in Back Seven Projects Nov 27, 2019
@beaucarnes beaucarnes self-assigned this Nov 27, 2019
@beaucarnes beaucarnes changed the title Arithmetic Formatter (Curriculum Project) Arithmetic Formatter (Certification Project) Nov 27, 2019
@beaucarnes beaucarnes moved this from Building Example Project to Writing Test Descriptions in Back Seven Projects Nov 27, 2019
@scissorsneedfoodtoo scissorsneedfoodtoo moved this from Writing Test Descriptions to Ready to QA in Back Seven Projects Dec 20, 2019
@scissorsneedfoodtoo
Copy link
Contributor

Project prototype: https://repl.it/@BeauCarnes/fcc-arithmetic-arranger

@notingGone
Copy link

Typo on the sixth bullet point.
I assume this:
"There should be a single space between the operator the following number."
should be:
"There should be a single space between the operator and the following number."

@scissorsneedfoodtoo
Copy link
Contributor

Thank you for catching this @notingGone. We'll be sure to update this before the release.

@notingGone
Copy link

notingGone commented Jan 14, 2020

Accepts between one and five problems. Otherwise, the function should return Error: Too many problems.

implies:
if (len(problems) > 5 or len(problems) < 1): return "Error: Numbers cannot be more than four digits."
whereas the solution code only tests for the upper limit:
if len(problems) > 5: return "Error: Too many problems."

@behrtam
Copy link

behrtam commented Jan 15, 2020

def test_simple_arangement(self):
    actual = arithmetic_arranger(["235 + 52"])
    expected = "  235\n+  52\n-----"
    self.assertEqual(actual, expected)

For me it was helpfull to have a very simple test based on the first README example before the test_arangement test with 4 problems.

@borntofrappe
Copy link

Accepts between one and five problems. Otherwise, the function should return Error: Too many problems.

And

Each number is between one and four digits long. Otherwise, the function should return Error: Numbers cannot be more than four digits.

As @notingGone points out, the text describes a range, while the error message seems to suggest we should test for the upper threshold only.

The function has always at least one problem, and we always consider positive integers, so perhaps the text should describe only the greater boundary?


There should be a single space between the operator the following number.

I believe the single space should be between the operator and the longest of the two numbers.

For instance with '235 + 5', the space is between the plus sign + and 2. There is more whitespace between + and the number which follows.

arithmetic_arranger(['235 + 5'])
'''
  235
+   5
-----
'''

There should be four spaces between each vertically arranged problem.

I might be daft, but this one puzzled me a little. It describes the number of spaces between successive problems, right? The word vertically lead me to think the test was about the height of the individual problems.

Apologies if these notes sound like nitpicks. The README file was overall easy to understand and I was able to complete the project.

@x-dune
Copy link

x-dune commented Jan 18, 2020

I agree with what @borntofrappe said (#231 (comment)).
The parts he highlighted did come off as a little confusing to me.

All I have to add is a nitpick (I know these aren't important 😅): these two bullet points do not have their full stops.

  • Each number should only contain digits. Otherwise, the function should return Error: Numbers must only contain digits.
  • Each number is between one and four digits long. Otherwise, the function should return Error: Numbers cannot be more than four digits.

@scissorsneedfoodtoo
Copy link
Contributor

Thanks everyone for your review. We'll take this all into account as we work on the next draft.

Given that each number in the problem should be between one and four digits long, the text There should be a single space between the operator the following number should probably be changed to There should be at least one space between the operator the following number.

As for testing only the upper range of problems passed to the function, we could include text in the README saying that there will always be at least one problem passed to the function. We could also mention that each number in the problems will be at least one digit long. Otherwise we could include a requirement for another error message for each requirement, maybe something like Error: Expected at least one problem. and Error: Expected each number to have at least one digit..

@borntofrappe, that's a good point about the There should be four spaces between each vertically arranged problem requirement. Perhaps there's a way to make it more clear that each arithmetic problem should be aligned vertically, but printed on the same horizontal plane with 4 spaces between each problem. It's a very difficult thing to describe using only text 😆 We're open to any suggestions about how to make this more explicit.

@borntofrappe
Copy link

@scissorsneedfoodtoo quite challenging indeed

Beside updating the text, I would also suggest we'd swap the last two bullet points. This because the very last point describing the dashes refers to the individual problem, and is more connected to the previous points than the penultimate one. Perhaps this is what confused me in the first place.

Hope it's not brash, but maybe the following description might help:

  • There should be dashes at the bottom of each problem. The dashes should consider the entire length of the operation.
  • There should be four spaces between different problems.

The example above shows the result of these rules.

I'd label them different, or successive, or multiple. Whichever describes the disconnect between the operations best.

@scissorsneedfoodtoo
Copy link
Contributor

@borntofrappe, not brash at all, we really appreciate all the input. We're so familiar with these problems and what the expected output should be that it's difficult for us to see the parts that are unclear. Your perspective is a huge help making these descriptions as clear as possible.

I agree, switching those two bullet points is a good idea and should help with clarity. Also those alternative word choices for the last bullet point are all good. Maybe something like "There should be four spaces between each successive arithmetic problem" or "There should be four spaces between each problem."

@beaucarnes
Copy link
Member Author

Thanks everybody for the suggestions! I've implemented all the suggestions so far.

@Gerschel
Copy link

I may have gotten carried away, but I felt there were some subtleties that would make the assignment more clear. So I went through most of the README and made the modifications, mostly in the Rules section that I added.

Assignment

Students in primary school often arrange arithmetic problems vertically to make them easier to solve. For example, "235 + 52" becomes:

  235
+  52
-----

Create a function that receives a list of strings that are arithmetic problems and returns the problems arranged vertically and side-by-side.

For example

Function Call:

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

Output:

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

Rules

The function will return the correct conversion if the supplied problems are properly formatted, otherwise, it will return a string that describes an error that is meaningful to the user.

  • Situations that will return an error:
    • If there are too many problems supplied to the function. The limit is five, anything more will return:
      Error: Too many problems.
    • The appropriate operators the function will accept are addition and subtraction. Multiplication and division will return an error. Other operators not mentioned in this bullet point will not need to be tested. The error returned will be:
      Error: Operator must be '+' or '-'.
    • Each number (operand) should only contain digits. Otherwise, the function will return:
      Error: Numbers must only contain digits
    • Each operand (aka number on each side of the operator) has a max of four digits in width. Otherwise, the error string returned will be:
      Error: Numbers cannot be more than four digits.
  • If the user supplied the correct format of problems, the conversion you return will follow these rules:
  • There should be a single space between the operator and the longest of the two operands, the operator will be on the same line as the second operand, both operands will be in the same order as provided (the first will be the top one and the second will be the bottom.
  • Numbers should be right-aligned.
  • There should be four spaces between each problem.
  • There should be dashes at the bottom of each problem. The dashes should run along the entire length of each problem individually. (The example above shows what this should look like.)

Development

Write your code in arithmetic_arranger.py. For development, you can use main.py to test your arithmetic_arranger() function. Click the "run" button and main.py will run.

Testing

The unit tests for this project are in test_module.py. We imported the tests from test_module.py to main.py for your convenience. The tests will run automatically whenever you hit the "run" button.

Submitting

Copy your project's URL and submit it to freeCodeCamp.

@Gerschel
Copy link

Perhaps the line that says "four spaces" should have the word "should" be changed to "will", so that way someone doesn't assume they can string fomat a tab in there like I did.

@beaucarnes
Copy link
Member Author

@Gerschel I really like your wording! Thanks for taking the time to rewrite this. I updated the project to incorporate most of your suggestions.

@jdelarubia
Copy link

The url to my project is: arithmetic formatter.
The difficulty for me was not being able to fully use Python string formatting options.
Things like f"{operand:>6d}" , because we cannot include a variable number of spaces that operand can take. However, I managed to find my way around it.
Maybe, relaxing the tests by requiring a fixed length to each operation?
Anyhow, I had fun building it and didn't take me very long.

@kunalrustagi08
Copy link

This is a great project to get learn string formatting. I enjoyed solving it.
This is the link to my arithmetic_arranger .

@robertue1
Copy link

It really helped me to learn some formatting.

Here is the link of my resolution: arithmetic_arranger

Roberto.

@mike-lloyd03
Copy link

The README.md says:

Each number (operand) should only contain digits. Otherwise, the function will return:
Error: Numbers must only contain digits

But the UnitTests look for:

Error: Numbers must only contain digits.

There should be a period at the end of the sentence in the .md file to be consistent with the other error strings.

@SasaKraljevic
Copy link

Link to my solution:
fcc-arithmetic-arranger

@scissorsneedfoodtoo
Copy link
Contributor

@mike-lloyd03, awesome, thank you for catching that. I'll update the README to reflect that.

@manshul1807
Copy link

Hi, Loved the project. Found no glitches. Learnt formatting and googled a lot.

Link : https://repl.it/@ManshulArora/ArithmeticArranger#main.py

@scissorsneedfoodtoo
Copy link
Contributor

@manshul1807, great, thank you for your feedback. Please let us know what you think about the other projects, too.

@borntofrappe
Copy link

I'm revisiting the project since I first examined it back in February. Am I correct to assume the prototype is now at the following URL @scissorsneedfoodtoo ?

https://repl.it/@freeCodeCamp/fcc-arithmetic-arranger

It seems there's an additional test with respect to the original. I hope to have something constructive to add soon 👍

@beaucarnes
Copy link
Member Author

@borntofrappe Yes, that is the new URL to review. And you are right, there is a new test. Thanks for your help reviewing this!

@borntofrappe
Copy link

A few notes on the starter files.

arithmetic_formatter.py

The starter code fails not because the function is incomplete, but because there is an error in the return value

def arithmetic_arranger(problems):

-    return arranged_problems
+    return problems

I believe this comes from the previous version of the project, but it's worth a fix.

test_module.py

A very minor fix to line 7, where the word arrangement is missing a letter.

-test_arangement
+test_arrangement

README.md

The README file is clear enough, but I have a few suggestions for the wording of a few sentences.

The paragraph introducing the assignment can be re-worded to add more emphasis to the input argument and return values.

Create a function that receives arithmetic problems as a list of strings, and returns the problems arranged vertically and side by side. The function should optionally take a second argument in a boolean. When this argument is set to True, the function should also display the answer to the arithmetic problems.

The list of rules is perhaps better introduced describing the requirements in two separate bulleted lists.

In the following instances, the function should return a string describing a meaningful error message.

  • if there are more than five problems, return Error: Too many problems.

  • if the problems include the multiplication * or division / characters, return Error: Operator must be '+' or '-'.

  • if the operands include characters other than numbers, return Error: Numbers must only contain digits.

  • if the operands have more than four digits, return Error: Numbers cannot be more than four digits.

When formatting the problems, the function should return a string following these rules:

  • there should be a single space between the operator and the longest of the two operands

  • the operator should be on the same line as the second operand

  • the operands should be in the order in which they are provided. The first number will be the top one and the second will be the bottom.

  • numbers should be right-aligned

  • there should be four spaces between each problem

  • there should be dashes at the bottom of each problem. These dashes should run along the entire length of each individual problem

Refer to previous examples to see how the output should look like.

@borntofrappe
Copy link

borntofrappe commented Jun 13, 2020

In the spirit of discussion, I also have a couple of questions regarding the code of arithmetic_arranger.py

Function definition

In the starter project, the function is defined as accepting a single argument.

def arithmetic_arranger(problems):

However, the assignment asks for an optional second argument.

def arithmetic_arranger(problems, show_solutions=False):

Should we initialize the argument, or let the campers fill in the required syntax? Given the structure of the README, it seems reasonable to leave the code as-is, but I wanted to ask for a second opinion still.

It is worth mentioning that, without the mention of the optional argument, there is an error with the final test, as the function is called with more positional arguments that it is equipped to.

def test_solutions(self):
        actual = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True)

Something to be aware of when testing the program.

import libraries

Campers should be able to complete the assignment without importing additional libraries. However, and especially after solving the challenges of the Python for Everybody module, it seems reasonable to import the re library. The module provides the perfect function to ensure that the operands contain only digits.

Perhaps it excessive to add the import statement in the initial code, but should we add a line in the README on the use of libraries, whether they are accepted and in which measure?

I hope the questions are clear enough. Let me know if you need further input :)

Update

Here's a link to my solution: https://repl.it/@borntofrappe/fcc-arithmetic-arranger

I completed the assignment without the re library, and looking at the README for the second project, I found a line explicitly mentioning the use of libraries:

Do not import any Python libraries.

Perhaps we should include this instruction in this project as well, at the bottom of the section detailing the rules.

@scissorsneedfoodtoo
Copy link
Contributor

scissorsneedfoodtoo commented Jun 16, 2020

Hi @borntofrappe, thanks for all of your helpful suggestions.

I went in and changed test_arangement to test_arrangement in the test module.

As for return arranged_problems, I believe that was intentional to help communicate that the problems should be arranged as a string, which students should create themselves. But we could certainly change it since the test module isn't directly calling an arranged_problems method.

You bring up good points about the function definition and possibly adding a "Do not import any Python libraries" note. I'm not sure it's necessary here because re doesn't totally trivialize the challenge like time might for the Time Calculator challenge. What do you think @beaucarnes?

@borntofrappe
Copy link

I see your point @scissorsneedfoodtoo

Using the re library requires building a regular expression, which in and of itself is a good exercise.

I now also understand the reasoning behind the default return statement, and apologize for having made the same suggestion in the Time Calculator project. It works as a prompt, to give a direction to campers 👍

@scissorsneedfoodtoo
Copy link
Contributor

scissorsneedfoodtoo commented Jun 17, 2020

No worries @borntofrappe, thanks for all of your great suggestions. It's definitely worth considering everything you pointed out, even the default return statement. I'm sure other people will point out the same thing and we're always open to making changes to improve clarity.

@alwaysasking
Copy link

Hey! When I tried running your code, I received some errors.

(can't use .split() on a list, nor on integers)

for i,problem in enumerate(problems):
num1,op,num2=problems.split()

remedied by:
for i in problems:
a=str(i).split()
num1=a[0]
op=a[1]
num2=a[2]

Then I have a question--I know the test runs to check if we followed the guidelines, but is the output supposed to say FAILED (failures=6) at the bottom? Did I mess up?

https://replit.com/@alwaysasking/boilerplate-arithmetic-formatter#arithmetic_arranger.py

^^ the code I submitted

@scissorsneedfoodtoo
Copy link
Contributor

Hi @alwaysasking, thanks for reaching out about this.

For help with the coding challenges, please check out the forum at https://forum.freecodecamp.org. The contributors there are very active, and can help answer and questions you might have.

Also, there's a good chance that someone else had a similar issue and was able to work it out, so please make sure to search through the existing threads before creating a new one.

@moT01 moT01 closed this as completed Jul 26, 2022
@freeCodeCamp freeCodeCamp deleted a comment from Satish404 Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests