RU beehive logo ITEC dept promo banner
ITEC 109
2014spring
ibarland

homeinfolabsexamshws
D2LMediaSamples/breeze (snow day)tutor/PIs

hw04
functions calling your functions; loops

Due Apr.02 (Wed) 10:00; submit a file hw04.py (written in JES) to hw04-dropbox and turn in a hardcopy in class Apr.02.

  1. An Example of calling triZag
    Figure 1: An example of calling triZag

    (6pts) In lab, we wrote a Turtle function zag : Turtle, int → None, which had a turtle draw a zag-shape of a given size end-to-end1. It leaves the turtle ended up facing the same way it started.

    Your task is to write triZag : Turtle, int → None which takes a Turtle, and draws three zags, turning between them to complete a zaggy triangle, as in Figure 1. Leave the turtle facing the same way it started.

  2. An example of calling zagLevel2
    Figure 2: An example of calling zagLevel2

    (8pts) Write the function zagLevel2 : Turtle, size → None which is kind of like zag2, except that it instead of drawing shorter lines and turning, it draws shorter zags and turns! If the turtle starts facing North, the result would be as in Figure 2 (though the Turtle's color may differ, of course).

  3. (4pts) On codingbat.com, complete the problem Python > String-1 > make_abba.
    Account setup: Be sure to create/use a codingbat account using yourRadfordUserName@radford.edu, and that you have set, at some point, prefs > Share To is set to ibarland@radford.edu. (If you re-register sharing with me, it'll say you're already sharing, so no harm done.)

    When you're done, paste your code into your hw04.py file. Include a comment immediately preceding the function, reporting “    codingbat test-cases didn't pass”, where the blank is filled in with a number (hopefully, the number will be 0!).

  4. (6pts) On codingbat.com, complete the problem Python > String-1 > make_out_word.

    You will want to include, in the part you type in to codingbat.com, our definition of substring:

    # substring : string, int, int → string
    #   Given some text, a start-index, and a stop-index,
    #   return the characters of `txt` from `start` up until (but not including) `stop`.
    #   Indices are 0-based.
    #   Examples:   substring( "radford", 3, 6) = "for"
    #               substring( "radford", 0, 3) = "rad"
    #
    def substring(txt, start, stop):
      return txt[start:stop]
    

    Paste your code (and report # test cases that don't pass), as in the previous problem.

  5. (6pts) Using JES's image-functions, write speckleAt : Picture, int, int → None which takes a Picture and x, y coordinates, and “speckles” the pixel at that location by setting its red component to be a random number in 0 up to (but not including) 256.
    Hint: Here is an example of how to get a random number in the range 0 up to (but not including) 4; try typing it in JES's interactions (lower) pane:
    import random
    
    random.randrange(4)
    random.randrange(4)
    random.randrange(4)
    
    In your program-file itself (the top page), have the import statement just once, up near the top of your file.

    Beware: randrange is not like range, despite the similar names (one returns a single integer; the other returns an entire list-of-ints that can be looped over). They are no more similar that Ford and Radford.

  6. (8pts) Write speckleRow : Picture, int → None which is given a picture and a valid row-number (a y-coordinate), and speckles every pixel in that row — from column 0 up to (but not including) the picture's width. (This function should, of course, call speckleAt many times.)
  7. (8pts) Write speckle : Picture → None which takes in a picture, and speckles it.

    Clearly, this function should work by calling speckleRow on every single row in the picture (that is, row# 0 up to (but not including) the picture's height).

  8. (extra credit: 8pts) Write a the turtle function polygon : Turtle, int, int → None which takes in a turtle, a number of sides n, and a size, and has the turtle draw an n-sided polygon where each side is the indicated size. Your function should leave the turtle at it's starting location and direction.

    Calling this function

    Hint: If you call your function to draw a large number of sides, you'll want to pass it a small number for the size, so that it stays on the screen. Turtles get a bit wonky if they run into the edge of the screen.
    Extra: For a little further extra credit: have this function take in not the length-of-each-side, but rather the desired diameter (of the circumscribing circle). This will require some geometry/trig, to figure out how long a side should be based on the diameter!

1Some people may have written a version which drew a zag-shape three times longer than the requested size, end-to-end. If that was you, you'll want to fix it.      

2If this is a level-2 zag, we might say that the original was a level-1 zag. After you complete this task, it's left as a fun self-task, to draw a level-3 zag.      

homeinfolabsexamshws
D2LMediaSamples/breeze (snow day)tutor/PIs


©2014, Ian Barland, Radford University
Last modified 2014.Apr.02 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme