[Tutor] Program help

dn PyTutor at DancesWithMice.info
Thu Oct 22 19:40:18 EDT 2020


On 23/10/2020 06:02, Garrett Phelps wrote:
> Perhaps someone can help me write this code:

Sure!

> The USPopulation.txt file contains the midyear population of United States,
> in thousands, during the years 1950 through 1990. The first line in the
> file contains the population for 1950, the second line contains the
> population for 1951, and so forth. Create an application that reads the
> file's contents into a list. The application should do the following:
> 
>     - (15 points) Read the file into an list of integers (multiply by 1000)
>     - Iterate through the array and display:
>        - (10 points) Year
>        - Population
>        - (10 points) Change from previous year (subtract current year minus
>        previous year)
>        - (10 points) Percent change in population over last year (change in
>        population divided by previous year population)
>     - When complete, display the following based on the changes in
>     population (not changes in percent):
>        - (10 points) Average Population Change
>        - (10 points) Minimum Population Change and Year
>        - (10 points) Maximum Population Change and Year


I have very mixed-feelings about the use of rubrics. On the one hand 
they are useful marking/grading aids, and help to justify an assessor's 
decisions in a semi-objective fashion (cf a subjective "I think your 
partial-effort is worth..."). On the other hand, there is a degree of 
'solve the problem like this' and thus 'there is only one way to solve 
this problem'; whereas I prefer people to learn how to approach their 
'homework' problems because that's preparation for what happens on-the-job.

Generally-speaking, a competent assessment will be an opportunity for 
you to prove what you have just learned (mainly) and your ability to add 
that to what was learned earlier in the course. So, if you've just 
covered Python-lists, do you really need to be told to use them in the 
very next quiz?

[rant over]


Have you come-across the Python docs? (https://www.python.org/doc/). The 
"Tutorial" will have similar example-problems to the above. I find 
myself referring to the "Library Reference" frequently. Both are worth 
opening as tabs in your web-browser! Additionally, whilst I've been 
typing @Alan has also replied 'here', and you will find a web-ref to his 
tutorial at the foot of that message.


Are you aware that the Python eco-system offers great ways to experiment 
with code and structures without needing to write whole programs? On my 
system (likely differs from yours) I open a "terminal", fire-up Python, 
and can immediately test my solutions, eg

<<<
dn $ ... python
Python 3.8.5 (default, Aug 12 2020, 00:00:00)
[GCC 10.2.1 20200723 (Red Hat 10.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> x = 2
 >>> y = 3
 >>> x + y
5
 >>> print( x + y )
5
 >>> z = x + y
 >>> print( z )
5
 >>>

I didn't need to start pointing at successive fingers on my hands to 
answer the question - Python tells me the answer! Notice three ways to 
'see' the answer.

If you use PyCharm or similar IDE, they usually enable a built-in 
"command line" or Python environment.

Alternately, you will find various on-line 'fiddles' and REPLs (defined: 
https://pythonprogramminglanguage.com/repl/), of which I heartily 
recommend Philip Guo's pythontutor.com because it will also help when 
you want to 'see' what happens to your data and act as a "debugger" when 
you reach such topics.


Aside from my comments, you have been given a rubric which acts as a 
useful guide, so let's use it!

How does one open a file?
Will you read its data one record/line at a time, or given that it is so 
short, read the whole file with one command?

If the former, how does one take the record and add it to a list?
If the latter, is there are string/list function which will break-up the 
single string into a list of elements.
NB Python (itself) does not have an "array" data-structure!

There's a 'gotcha' in the midst of all this. Are the years/dates strings 
or numbers? Are the population-values strings or numbers? (read the 
docs, and experiment in your REPL)

It is not crystal-clear, above, if the year is part of the incoming 
data. It would appear not. If not, how will you relate "1950" to the 
first-read data-item? There are a couple of approaches to this, but we 
don't know your level of Python knowledge. You may think to calculate 
the year, each time; or perhaps to construct a logically-linked list. 
(draw them side-by-side on a sheet of paper it you can't see what I mean)

You need to be able to perform arithmetic to compare 1990's with 1989's 
population (for example) - did you solve the "gotcha" above? The next 
issue is how to "address" the 1989 data when you are 'looking at' 1990?

The arithmetic I leave to you... (it's probably too difficult for me)

Lastly, there are the aggregate analyses. You could partially compute or 
assemble these within the 'display' loop, above, finishing with some 
final-touches at this end. Alternately, you could look for Python 
functions in 'the docs' (per above), and find that Python will do 
most?all of it for you. Again, multiple solutions, depending mostly upon 
your level of Python knowledge/progress through the training course.


As with all programming tasks, the "systems approach" is to break the 
'big problem' into smaller component-problems (and them into ...) until 
each can be solved one-at-a-time. In your case, each sub-problem should 
be solved by experimenting with code in the REPL. Thereafter it is a 
simple matter (hah - easy for me to say!) of assembling all the 
small-solutions into 'the whole thing'! Without the benefit of your 
rubric, this is exactly how professionals approach their 
work-assignments - the only difference is how small the 'small problem' 
("chunk") needs to become, before we start 'building'.


So, start as suggested, and get back to us when you either have 
'something', or you strike a coding problem. (in which case, copy-paste 
the code and any error-messages into your email - no attachments!)
-- 
Regards =dn


More information about the Tutor mailing list