hw2 - COMPUTER SCIENCE 50100 Computing for Science and...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
COMPUTER SCIENCE 50100 Computing for Science and Engineering FALL 2014 ASSIGNMENT # 2 (35 points) September 10 Due Wednesday, October 1 at 11:30 am This assignment covers Section 2.3–2.6 of the class notes. 0. (Required) Please state whether you are using Python 2.7 or 3.4 and which operating system you are using. This will assist in the grading of your solutions. Hand in at most two of the following three problems. If you choose to do Problems 1 and 2, you earn at most 30 points. 1. (15 points) Write a Python script clean.py that moves files in the current directory with names ending in ~ , .aux , or .log to the hidden directory ~/.Trash . Begin by making the ~/.Trash directory, but do it inside a try / except construct to protect against a FileExistsError . Then announce the move by printing the message “ Moving the following files to ~/.Trash: and, on the next line, list the files. Here is an example: #!/usr/bin/env python # testclean.py from os import listdir, mkdir, chdir, pardir, sep, remove, rmdir if ’Test’ in listdir(): print(’Directory "Test" already exists; exiting.’) exit() mkdir(’Test’) chdir(’Test’) for name in [’1aux’, ’2.aux’, ’3~’, ’4.log’]: open(name, ’w’).close() print(’ ’.join(listdir())) # execute clean.py clean_py = open(pardir + sep + ’clean.py’) exec(compile(clean_py.read(), ’clean.py’, ’exec’)) clean_py.close() # names = listdir() print(’ ’.join(names)) # clean up for name in names: remove(name) chdir(pardir) rmdir(’Test’) Its output is 1aux 2.aux 3~ 4.log Moving the following files to ~/.Trash: 3~ 2.aux 4.log 1aux 1
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Possibly useful functions and data: os.getcwd , os.getlogin , os.sep , os.mkdir , os.rename , glob.glob , str.find . To determine the home directory, you may assume that the path of the current working directory contains the path of the home directory and that the name of the home directory is the login name. Your solution must not be Unix-specific, that is, use functions from the os module rather than the subprocess module. The following two problems are based on material from Data Structures and Algorithm Anal- ysis in Java , by Mark Allen Weiss, Addison-Wesley, 1999. 2. (15 points) Given below is an implementation of a disjoint sets class. You are being asked to implement this class using a “fast” algorithm, which is described further down. A disjoint sets object is a partitioning of a set into disjoint subsets. For convenience, assume the elements of
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern