CMU 15-112: Fundamentals of Programming and Computer Science
Homework 10 (Due Fri 1-Apr at 10pm ET)

Important Notes:
  1. TA-Led Mini-Lecture [10 pts]
  2. findLargestFile(path) [90 pts]

  1. TA-Led Mini-Lecture [10 pts]
    Attend one of the TA-Led Mini-Lectures. These are separate from recitations, small-groups, large-groups, etc. See the course schedule for a list of your many fine options to attend. Also, look to Piazza for descriptions and additional information. As always, to receive full credit, you must show up on time, stay the whole time, be focused and uni-tasking (not multi-tasking), and fully participating.

  2. findLargestFile(path) [90 pts] [autograded]
    Write the recursive function findLargestFile(path), which takes a string path to a folder and returns the full path of the largest file in the folder (and all its subfolders). Here "largest" is determined by number of bytes. You can compute the size of a file (in bytes) using os.path.getsize(path). If there are no files in the input folder, your function should return the empty string (""). You do not need to handle the case where the supplied path is not valid or is not a folder, and you may handle ties however you wish.

    Note that file systems can be very large, so in this problem, efficiency is important! Therefore, you may not use listFiles (or anything similar) to gather all the files into one place, and you should avoid calling os.path.getsize on a single file more than once.

    Also note that some file systems list files in different orders than other file systems. Because of this, you need to be sure that your solution does not depend on the order in which your file system lists the files in a folder.

    Solutions which use os.walk will receive no credit.

    To help test your code, here are some assertions for use with sampleFiles (available in sampleFiles.zip):

    assert(findLargestFile("sampleFiles/folderA") == "sampleFiles/folderA/folderC/giftwrap.txt") assert(findLargestFile("sampleFiles/folderB") == "sampleFiles/folderB/folderH/driving.txt") assert(findLargestFile("sampleFiles/folderB/folderF") == "")

    Hints:
    1. You also may need to get rid of those pesky .DS_Store files that Macs like to add to folders that you unzip. To do that, use removeTempFiles. Remember to be careful, since it removes without asking and it does not move the removed files to the trash (they are really gone).
    2. We found the following functions to be especially helpful:
      • os.path.isdir(path)
      • os.listdir(path)
      • os.path.getsize(path)
    3. Here are the headers for each of the functions we wrote, with brief descriptions:
      def findLargestFile(path): # Wrapper to extract just the bestPath from the helper # function that returns both bestPath and bestSize ... def findLargestFileAndSize(path): # Returns (bestPath, bestSize) starting from this path, which could # be to either a folder or a file ...