INI2 - maccergit/Rosalind GitHub Wiki
This is the first step in a very long journey and is pretty easy - it provides small taste of what is to come.
001
Just a simple program to read in a couple numbers and write out the sum of the squares. However, to follow the steps of :
- read the input data
- process the input data
- write the results as an output file
requires some boiler plate code and introduces some Python concepts, such as using the "with" construct to ensure the file being used is properly closed, even if an exception is thrown.
In addition, the code also shows the idea of running against the data provided in the example and verifying the result of processing that data matches the result provided in the example. If this fails, then we have missed something (our algorithm is incorrect, or the data is not correctly formatted, or whatever...), and we are not likely to get a good solution for the main problem data.
NOTE
Here, we can see some of the quirks that can make things a bit annoying. The main discussion is based on Python 2, which is a particularly ancient version of Python these days - most Python installs will install Python 3, which is not 100% compatible with Python 2. In this case the "print" statements do not have parentheses around the data to be displayed - this was optional in Python 2, but is mandatory in Python 3. Also, the discussion states that using "/" to divide an integer by an integer will truncate the result to an integer (that is, it will perform "integer division") - this was the case in Python 2, but is no longer the case with Python 3. Instead, Python 3 provides the "//" operator to explicitly perform integer division, and the normal "/" operator will return a floating point value if one is needed.
TODO
Using Google Gemini as a light code review, some suggestions have been made that I am incorporating as TODO items :
- The problem statement includes limits on the input values. Rather than simply ignoring them, use those statements to validate the input data.
- Consider other error checking (or at least test these cases and document that I am good with the results - uncaught exceptions may be good enough here) - empty input file, non-numeric data, extra input values.