Courses‎ > ‎Intro to CS - Full Year‎ > ‎Spring‎ > ‎Dyrland-Weaver‎ > ‎

Work

Unless otherwise noted, all assignments are due by 8am the next class day.

All submissions must be made via the homework server: http://bert.stuy.edu/jdyrland/spring2019/pages.py?page=profile

Late homework can be submitted for half credit up until solutions have been posted.

You can find homework solutions here: https://github.com/mks22-dw/python

Work 29: 4/17

posted Apr 17, 2019, 9:42 AM by JonAlf Dyrland-Weaver


  • count_hops
    • Imagine a frog sitting in a circle of 5 lilypads.
      • Assign each pad a number in the range [0, 4]
      • Assume the frog stars on pad 0
    • The frog can hop either 2 pads forward or 1 pad backward.
      • If the frog goes off either end, it wraps around (one hop backward from 0 is 4)
    • Write a function that will return the number of times the frog will end up at a particular pad, given all combinations of a set number of hops.
      • count_hops(position, target, hops)
    • Here are lists representing the total times the frog, starting at position 0, would end up on a specific pad given some number of hops
      • 1 hop: [0, 0, 1, 0, 1]
      • 2 hops: [0, 2, 0, 1, 1]
      • 3 hops: [3, 1, 1, 3, 0]
      • 5 hops: [2, 10, 5, 5, 10]
    • Examples
      • count_hops(0, 1, 2) ==> 2
      • count_hops(0, 2, 2) ==> 0
      • count_hops(0, 0, 5) ==> 2
  • random_slice
    • Takes a list as a parameter
    • Returns a randomly created slice of the parameter list.
    • The start and end of the slice should be randomly selected valid indices of the list.
    • The slice could be a forwards or backwards slice of the list.
  • average
    • Takes a single list containing only numbers as a parameter.
    • Returns the average of all the elements of the list.
      • Example
        • average( [8, 4.5, -83, 98.2, 100] ) ==> 25.54
  • list_min
    • Takes a single list as a parameter.
    • Returns the smallest element in the list.
    • Examples:
      • list_min([8, 4.5, -83, 98.2, 100]) ==> -83
      • list_min(['cat', 'dog', 'avocado', 'bacon']) ==> 'avocado'
Submit this as test4_review

Work 28: 4/15

posted Apr 15, 2019, 6:32 AM by JonAlf Dyrland-Weaver

  • In class, we developed a recursive function that finds the nth fibonacci number. It looked like this:
    • def fib(n):
          if n < 2:
              return 1
          else:
              return fib(n-1) + fib(n-2)
  1. Using a global counter variable, print out how many times this function must run for fib(20)
  2. Create a global variable containing the list [1, 1]. These are the first 2 fibonacci numbers.
  3. Write a second fibonacci function, list_fib(n). It will also find the nth fibonacci number but it should run much faster
    • If the function looks for a fibonacci number that is already in the list, it uses the value from the list.
    • If the function looks for a fibonacci number that is not in the list, it will recursively calculate that number, then add it to the list before returning.
  4. Use the counter variable to count how many times it takes to run list_fib(20)
    1. It should be significantly faster. 
    2. In comments in your code, explain why it is so much faster.
submit this as fast_fib

Work 28: 4/12

posted Apr 12, 2019, 10:27 AM by JonAlf Dyrland-Weaver   [ updated Apr 12, 2019, 10:29 AM ]

  • A fractal tree can be drawn as a recursive process. 
  • Consider the function draw_tree(t, branch_levels), it instructs turtle t to draw a tree with bench_depth levels.
    • draw_tree(t, 1)
      • result:
    • draw_tree(t, 2)
      • result: 
    • draw_tree(t, 3)
      • result:
    • draw_tree(t, 4)
      • result:
  • Notice the position and orientation of the turtle when the function is done.
  • Write the recursive draw_tree function and test it.
  • Ask yourself
    • What is the easiest tree to draw
      • What are the instructions for the turtle in that case
    • How many trees are drawn each time you increase the number of branch levels?
  • There are things that you can modify to make your trees look more interesting ONLY LOOK AT THESE IF YOU HAVE THE REGULAR TREE DONE
    • Try out different angles between branches.
      • What if there's some randomness in the angle?
    • Try out different lengths of the branches.
      • What about decreasing or increasing branch sizes for each sub-tree?
submit this as tree

Work 27: 4/11

posted Apr 10, 2019, 8:03 PM by JonAlf Dyrland-Weaver

submit the completed lab file as turtleg

Work 26: 4/9

posted Apr 9, 2019, 12:37 PM by JonAlf Dyrland-Weaver

Write a function that will print out (no returning) the steps to solve the towers of hanoi.
  • Use the following function header: hanoi(n, start, end, temp)
    • n: number of disks
    • start: starting pole to move disks off
    • end: goal pole for all the disks
    • temp: pole to be used to store disks temporarily
    • start, end and temp should be 0, 1 or 2
  • The starting call should look like this:
    • hanoi(4, 0, 2, 1)
  • The output should be in the format pole to pole
    • example:
      • hanoi(2, 0, 2, 1)
        • 0 to 1
        • 0 to 2
        • 1 to 2
  • You can test your output by copying it into the "Move List" text box here: http://homer.stuy.edu/~dw/hanoi/
    • Click Solve and it will play out your moves. Don't forget to initialize with the correct number of disks.
Submit this as hanoi

Work 25: 4/5

posted Apr 5, 2019, 12:58 PM by JonAlf Dyrland-Weaver

Submit this as fast_search

Work 24: 4/3 - 4/4

posted Apr 4, 2019, 11:54 AM by JonAlf Dyrland-Weaver

  • With your table buddy, work on the following lab: https://github.com/mks22-dw/python/blob/master/merge_lab.py
  • You need to at least complete merge_lists, but if that is as far as you get, that's ok.

Work 23: 4/1

posted Apr 1, 2019, 10:52 AM by JonAlf Dyrland-Weaver   [ updated Apr 1, 2019, 11:39 AM ]

Write the following python functions
  • random_list
    • Takes 2 parameters (n, limit).
    • Returns a list of n random integers in the range [0, limit)
  • bar_graph:
    • Takes a list of non-negative integer values and returns a string of a bar graph representation of the data.
    • Example:
      • a = [9, 5, 5, 3, 1, 5, 9, 5, 7, 8]
      • print(bar_graph(a))
      • 0: =========
      • 1: =====
      • 2: =====
      • 3: ===
      • 4: =
      • 5: =====
      • 6: =========
      • 7: =====
      • 8: =======
      • 9: ========
  • find_mode:
    • Takes a list of values and returns the value that appears most often (the mode).
    • If there are multiple modes, returning any of them will suffice.
submit this as lists_1

Work 22: 3/29

posted Mar 29, 2019, 9:25 AM by JonAlf Dyrland-Weaver

  1. Do the problems located here: https://codingbat.com/home/dw@stuy.edu/lists-0
    • Only use the python features we have covered in class so far.
  2. In addition completing the problems on CodingBat, copy all the functions you write into a single .py file and submit that using the homework server.
Submit your code as lists_0

Work 21: 3/27

posted Mar 27, 2019, 1:04 AM by JonAlf Dyrland-Weaver

Write the following python functions:

  • is_palindrome
    • Takes a single string as a parameter.
    • Returns True if the string is a palindrome and False otherwise.
    • Examples
      • is_palindrome('racecar') ==> True
      • is_palindrome('ferrari') ==> False
  • third_char
    • Takes a single string parameter
    • Returns a new string made up of every third character from the original string, starting with the third.
    • Example:
      • third_char( 'abcdefghij' ) ==> 'cfi'
      • third_char('jeIgh ina7Hm9u w9t.uhr0e 8 dhDvwo 2c') ==> <you figure it out>
  • bold_string
    • Takes two strings as parameters. The first is a regular string (s) and the second is a substring to look for (key).
    • Returns a new string that is a copy of s except that every instance of key should be surrounded by HTML h1 tags.
    • Example:
      • boldString( 'do, or do not, there is not try.' , 'do' ) ==> '<h1>do</h1> or <h1>do</h1> not, there is no try.'

submit this as test3-review

1-10 of 31