Connection Status:
Competition Arena > EllysDirectoryListing
SRM 534 · 2011-11-22 · by espr1t · Simulation
Class Name: EllysDirectoryListing
Return Type: String[]
Method Name: getFiles
Arg Types: (vector<string>)
Problem Statement

Problem Statement

Elly wants to write a program that lists all the files in a given directory. She already has the list of all the files. You will be given this list as a String[] files. In addition to the names of files, the variable files will contain exactly two additional elements: the current directory (the String "."), and the parent directory (the String ".."). These two elements may be anywhere in files. However, Elly wants them to be the last two elements. In order to move the two directories to the last two positions in files, she wants you to perform the following steps:
  1. If "." and ".." are the last two elements of files (in any order), you are done.
  2. Find the first element of files that is either "." or "..". Swap it with the last element of files.
  3. If "." and ".." are now the last two elements of files (in any order), you are done.
  4. Find the first element of files that is either "." or "..". Swap it with the element of files that is one position before the last one.
Your method must perform the above steps and return a String[] containing the modified order of elements in files.

Notes

  • Swapping elements at positions i and j means that the element at position i is moved to the position j and vice versa.

Constraints

  • files will contain between 2 and 50 elements, inclusive.
  • All elements of files will be distinct.
  • Exactly one element of files will be ".".
  • Exactly one element of files will be "..".
  • Each element of files will contain between 1 and 20 characters, inclusive.
  • Each element of files will contain only uppercase and lowercase letters and dots ('A'-'Z', 'a'-'z', '.').
Examples
0)
{"ContestApplet.jnlp", ".", "Image.jpg", "..", "Book.pdf", "Movie.avi"}
Returns: {"ContestApplet.jnlp", "Movie.avi", "Image.jpg", "Book.pdf", "..", "." }

The directories are not the last two elements, so we search for the first one (in this case "."), and swap it with the last element (in this case "Movie.avi"). As "." and ".." are still not the last two elements, we search for the second one (in this case "..") and swap it with "Book.pdf".

1)
{"Image.jpg", "..", "."}
Returns: {"Image.jpg", "..", "." }

In this case the directories are already the last two elements, so we do nothing.

2)
{"..", ".", "Image.jpg"}
Returns: {"Image.jpg", ".", ".." }

After swapping ".." with "Image.jpg", the directories are already the last two elements, so we are done.

3)
{"No", "..", "Zaphod", ".", "Just", "very", "very...", "Improbable"}
Returns: {"No", "Improbable", "Zaphod", "very...", "Just", "very", ".", ".." }
4)
{"www.topcoder.com", "Ever.tried", ".", "Ever.failed", "..", "No", "Matter.", "Try", "Again.", "Fail", "Again..", "Fail.Better"}
Returns: {"www.topcoder.com", "Ever.tried", "Fail.Better", "Ever.failed", "Again..", "No", "Matter.", "Try", "Again.", "Fail", "..", "." }
7)
{"This", ".", "is", "tricky", "test", ".."}
Returns: {"This", "test", "is", "tricky", "..", "." }

The first swap moves ".." to position 1, so we need to move it again to index 4 (indexed from zero).

Submissions are judged against all 80 archived test cases, of which 6 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class EllysDirectoryListing with a public method vector<string> getFiles(vector<string> files) · 80 test cases · 2 s / 256 MB per case

Submitting as anonymous