Twilio L2 | OA | Segment
Anonymous User
1640
  1. given a filename - test_00.txt
    In that file is a list of file names with different extension.
    I believe this is limited to 3 different types.
    .c
    .cs
    .cpp
    create a new file per distinct extension type and store each name with matching extension in such file. Name the new file {extension}_orig file name
    c_test_00.txt
    ex:
    test_00.txt have the following lines
    123.c
    124.cs
    125.cpp
    126.c
    127.c
    128.cpp

file c_test_00.txt should contain
123.c
126.c
127.c

file cs_test_00.txt should contain
124.cs

etc.

  1. get the gif request
    given a file name
    the file contains http responses
    you are to parse each response and only store gif request of status 200. rename the original file gif_{orig filename}
    something.other.com - - [01/Jul/1995:00:00:12 -0400] \"GET /images/logosmall.gif HTTP/1.0\" 200 0
    this should savelogosmall.gif to the file.

Anyways I probably failed. Couldn't figure out how to test the first one since we are given the bare main class and running the code doesn't seem to check it to the test case. Even though I was told it failed.
your output: no stdout expected
expected output: no stdout expected
fail.
ok.....
That being said, here's what I did.

public List<File> SeparateTheFiles(String filename) throws FileNotFoundException{
	List<File> newFiles = new ArrayList<>();
	File f = new File(filename);
	@SuppressWarnings("resource")
	Scanner reader = new Scanner(f);
	Map<String, String> filemap = new HashMap<>();
	while(reader.hasNextLine()) {
		String data = reader.nextLine();
		String value;
		String ext = data.substring(data.indexOf(".") + 1);
		if(filemap.containsKey(ext)) value = filemap.get(ext) + ',' + data;
		else value = data;
		filemap.put(ext, value);
	}		
	for(Map.Entry<String, String> entry : filemap.entrySet()) {
		String newFilename = entry.getKey() + "_" + filename; 
		File newFile = new File(newFilename);
		try {
			FileWriter writer = new FileWriter(newFile);
			BufferedWriter bw = new BufferedWriter(writer);
			String[] temp = entry.getValue().split(",");
			for(String s : temp) {
				bw.write(s);
				bw.newLine();
			}
			bw.close();
		} catch(IOException e) {}
		newFiles.add(newFile);
	}
	return newFiles;
}
test
List<File> newFiles = SeparateTheFiles("separatefile.txt");
for(File f : newFiles) {
	try {
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String s;
		while((s = br.readLine()) != null) {
			System.out.println(s);
		}
	} catch(IOException e) {}
}
public File GetRequestForGif(String filename) {		
	String newFilename = "gif" + "_" + filename; 
	File newFile = new File(newFilename);
	try {
		FileWriter writer = new FileWriter(newFile);
		BufferedWriter bw = new BufferedWriter(writer);
		
		File f = new File(filename);
		@SuppressWarnings("resource")
		Scanner reader = new Scanner(f);
		while(reader.hasNextLine()) {
			String data = reader.nextLine();
			if(!data.toLowerCase().contains(".gif")) continue;
			String[] t = data.split(" ");
			if(!t[8].equals("200")) continue;
				
			Pattern p = Pattern.compile("[^/]+\\.gif");
			Matcher m = p.matcher(data.toLowerCase());
			if(m.find()) {
				bw.write(m.group());
				bw.newLine();
			}
		}
		bw.close();
	} catch(IOException e) {}
	
	return newFile;
}

Test
File newFile = GetRequestForGif("getrequestforgif.txt");
try {
	FileReader fr = new FileReader(newFile);
	BufferedReader br = new BufferedReader(fr);
	String s;
	while((s = br.readLine()) != null) {
		System.out.println(s);
	}
} catch(IOException e) {}
		
``
		


Comments (0)