Flip a graph

Vincent Davis vincent at vincentdavis.net
Sat Jan 4 17:10:41 EST 2014


You might think about using an array to represent the canvas. Starting with
it filled with "" and then for each point change it to "X".
The print the rows of the array.

You can make the array/canvas arbitrarily large and then plot multiple
different paths onto the same array.


Vincent Davis
720-301-3003


On Sat, Jan 4, 2014 at 9:15 AM, Jason Friedman <jsf80238 at gmail.com> wrote:

> I am teaching Python to a class of six-graders as part of an after-school
> enrichment.  These are average students.  We wrote a non-GUI "rocket
> lander" program:  you have a rocket some distance above the ground, a
> limited amount of fuel and a limited burn rate, and the goal is to have the
> rocket touch the ground below some threshold velocity.
>
> I thought it would be neat, after a game completes, to print a graph
> showing the descent.
>
> Given these measurements:
> measurement_dict = { # time, height
>     0: 10,
>     1: 9,
>     2: 9,
>     3: 8,
>     4: 8,
>     5: 7,
>     6: 6,
>     7: 4,
>     8: 5,
>     9: 3,
>     10: 2,
>     11: 1,
>     12: 0,
> }
>
> The easiest solution is to have the Y axis be time and the X axis distance
> from the ground, and the code would be:
>
> for t, y in measurement_dict.items():
>     print("X" * y)
>
> That output is not especially intuitive, though.  A better visual would be
> an X axis of time and Y axis of distance:
>
> max_height = max(measurement_dict.values())
> max_time = max(measurement_dict.keys())
> for height in range(max_height, 0, -1):
>     row = list(" " * max_time)
>     for t, y in measurement_dict.items():
>         if y >= height:
>             row[t] = 'X'
>     print("".join(row))
>
> My concern is whether the average 11-year-old will be able to follow such
> logic.  Is there a better approach?
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140104/ea2cdddb/attachment.html>


More information about the Python-list mailing list