File Directory

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:If “.” and “..” are the last two elements of files (in any order), you are done.

Find the first element of files that is either “.” or “..”. Swap it with the last element of files.

If “.” and “..” are now the last two elements of files (in any order), you are done.

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.


Examples

1){“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”.

2){“Image.jpg”, “..”, “.”} => {“Image.jpg”, “..”, “.” }

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

3){“..”, “.”, “Image.jpg”} => {“Image.jpg”, “.”, “..” }

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

4){“No”, “..”, “Zaphod”, “.”, “Just”, “very”, “very…”, “Improbable”}

=> {“No”, “Improbable”, “Zaphod”, “very…”, “Just”, “very”, “.”, “..” }

4){“www.tc.com”, “Ever.tried”, “.”, “Ever.failed”, “..”, “No”, “Matter.”, “Try”, “Again.”, “Fail”, “Again..”, “Fail.Better”}

=>{“www.tc.com”,”Ever.tried”,”Fail.Better”,”Ever.failed”,”Again..”,”No”,”Matter.”,”Try”,”Again.”,”Fail”,”..”,”.” }

5){“This”, “.”, “is”, “tricky”, “test”, “..”} => {“This”, “test”, “is”, “tricky”, “..”, “.” }

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

import java.util.Arrays;
public class EllysDirectoryListing {
 public String[] getFiles(String[] files) {
  int fl = files.length-1;
  int count = 0;
  for (int i=0;i < files.length-2;i++){
   if ((files[i].equals(".") && files[i].length()==1) || (files[i].equals("..") && files[i].length()==2)) {
    if (files[fl].equals(".") || files[fl].equals("..")) {
     String sdtl = files[fl];
     fl--;
     String sdtp = files[fl];
     files[fl] = sdtl;
     files[fl+1] = files[i];
     files[i] = sdtp;
     count +=1;
    } else {
     String sdt = files[fl];
     files[fl--] = files[i];
     files[i] = sdt;
     count +=1;
    }
   }
   if (count==2) i=files.length-1;
  }
  return files;
 }

 public static void main(String[] args) {
  EllysDirectoryListing el = new EllysDirectoryListing();

  System.out.println(Arrays.toString(el.getFiles(new String[]{"ContestApplet.jnlp", ".", "Image.jpg", "..", "Book.pdf", "Movie.avi"})));
  System.out.println(Arrays.toString(el.getFiles(new String[]{"Image.jpg", "..", "."})));
  System.out.println(Arrays.toString(el.getFiles(new String[]{"..", ".", "Image.jpg"})));
  System.out.println(Arrays.toString(el.getFiles(new String[] {"No", "..", "Zaphod", ".", "Just", "very", "very...", "Improbable"})));
  System.out.println(Arrays.toString(el.getFiles(new String[]{"This", ".", "is", "tricky", "test", ".."})));
 }
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: