Flip a graph

Vincent Davis vincent at vincentdavis.net
Sat Jan 4 17:13:49 EST 2014


When printing the rows of the array/canvas you might add \n to the end of
each row and print the canvas all at once rather than a print statement for
each row.

Vincent Davis
720-301-3003


On Sat, Jan 4, 2014 at 3:10 PM, Vincent Davis <vincent at vincentdavis.net>wrote:

> 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/223335d6/attachment.html>


More information about the Python-list mailing list