GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Ask a Question

 

Start Coding Today

   

GeeksQuiz

GeeksforGeeksIDE

Data Structures

Algorithms

Interview Experiences

C Programming

C++ Programming

Java Programming

GATE CS

Books

Interview Question (given a string representing the list of M photos, returns the string representing the list of the new names of all photos (the order of photos should stay the same))

John likes to travel. He has visited a lot of cities over many years. Whenever he visits a city, he takes a few photos and saves them on his computer. Each photo has a name with an extension ("jpg , "png or "jpeg") and there is a record of the name of the city where the photo was taken and the time and date the photo; for example: "photo. jpg, Warsaw, 2013—09—05 14 : 08: 15".

John notices that his way of filing photos on his computer has become a mess. He wants to reorganize the photos. First he decides to group the photos by city, then, within each such group, sort the photos by the time they were taken and finally assign consecutive natural numbers to the photos, starting from 1 . Afterwards he intends to rename all the photos. The new name of each photo should begin with the name of the city followed by the number already assigned to that photo. The number of every photo in each group should have the same length (equal to the length of the largest number in this group); thus, John needs to add some leading zeros to the numbers. The new name of the photo should end with the extension, which should remain the same. Your task is to help John by finding the new name of each photo. Each of John's photos has the format: “<<photoname>>.<<extension>>, <<city_name>>, yyyy—mm—dd hh:n-m: ss , where "<<photoname>>", "<<extension>>" and "<<city_name>>" consist only of letters of the English alphabet and supply the name of the photo, the file name extension and the city name, respectively.

Write a function:

string solution (string S) ;

that, given a string representing the list of M photos, returns the string representing the list of the new names of all photos (the order of photos should stay the same).
 

For example, given a string:
 

photo. jpg, Warsaw, 2013—09—05 14:08:15

john.png, London, 2015—06—20 15:13:22

my-Friends.png, Warsaw, 2013—09—05 14:07:13

Eiffel.ppg, Paris, 2015-07-23 08:03:02

pisatower.jpg, Paris, 2015—07—22 23:59:59

BOB.ipg, London, 2015-08-05 00:02:03

notredame.png, Paris, 2015—09—01 12:00:00

me.jpg, Warsaw, 2013—09—06 15:40: 22

a.png, Warsaw, 2016—02—13 13:33:50

b.jpg, Warsaw, 2016-01-02 15:12:22

c.jpg, Warsaw, 2016-01-02 14:34:30

d.jpg, Warsaw, 2016-01-02 15:15:01

e.png, Warsaw, 2016-01-02 09:49:09

f.png, Warsaw, 2016-01-02 10:5:32

g.jpg, Warsaw, 2016-02-29 22:13:11

 

your function should return:
Warsaw02.jpg
London1.png
Warsaw01.png
Paris2.jg
Paris1.jpg
London2.jpg
Paris3.jpg
Warsaw03.jpg
Warsaw09.jpg
Warsaw07.jpg
Warsaw06.jpg
Warsaw08.jpg
Warsaw04.jpg
Warsaw05.jpg
Warsaw10.jpg
as there are ten photos taken in Warsaw (numbered from 01 to 10), two photos in London (numbered from 1 to 2) and three photos in as there are ten photos taken in Warsaw (numbered from 01 to 10), two photos in London (numbered from 1 to 2) and three photos in Paris (numbered from 1 to 3). 



The new names of the photos are returned in the same order as in the given string. 



Assume that: 



• M is an integer within the range [1 .. 100]; 
• Each year is an integer within the range [2000..2020]; 
• Each line of the input string is of the format 
<<photoname>>.<<extension>> , yyyy—mm—dd hh:n-m:ss" and lines are separated with newline characters; 
• Each photo name (without extension}) and city name consists only of at least 1 and at most 20 letters from the English alphabet; 
• Each name of the city starts with a capital letter and is followed by lower case letters; 
• No two photos share the same date and time; 
• Each extension is "jpg", “png" or “jpeg”
asked Mar 1 by bios123

import java.text.*; import java.time.*; import java.time.format.*; import java.util.*; import java.util.stream.*; public class PhotoRename { public PhotoRename() { super(); } public static void main(String[] args) { PhotoRename photoRename = new PhotoRename(); String S = "photo.jpg Warsaw, 2013-09-05 14:08:15\n" + "john.png London, 2015-06-20 15:13:22\n" + "myFriends.png Warsaw, 2013-09-05 14:07:13\n" + "eiffel.jpg Paris, 2015-07-23 08:03:02\n" + "pisatower.jpg Paris, 2015-07-22 23:59:59\n" + "BOB.jpg London, 2015-08-05 00:02:03\n" + "notradame.png Paris, 2015-09-01 12:00:00\n" + "me.jpg Warsaw, 2013-09-06 15:40:22\n" + "a.png Warsaw, 2016-02-13 13:33:50\n" + "b.jpg Warsaw, 2016-01-02 15:12:22\n" + "c.jpg Warsaw, 2016-01-02 14:34:30\n" + "d.jpg Warsaw, 2016-01-02 15:15:01\n" + "e.png Warsaw, 2016-01-02 09:49:09\n" + "f.png Warsaw, 2016-01-02 10:55:32\n" + "g.jpg Warsaw, 2016-01-02 22:13:11\n" + "h.jpg Warsaw, 2016-01-02 22:12:11\n" + "i.jpg Warsaw, 2016-01-02 22:59:59"; System.out.println(solution(S)); } public static String solution(String S) { String[] photos = S.split("\n"); //System.out.println(Arrays.toString(photos)); int photoNumber = 1; List photoList = new ArrayList<>(); for (String s : photos) { String[] tokens = s.split("\\s"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dateTime = LocalDateTime.parse(tokens[2] + " " + tokens[3], formatter); photoList.add(new Photo(photoNumber, tokens[0], tokens[1].substring(0, tokens[1].lastIndexOf(',')), dateTime)); photoNumber++; } // Collections.sort(photoList,new PhotoComparator()); Map> map = new HashMap>(); for (Photo photo : photoList) { String city = photo.city; if (map.containsKey(city)) { List list = map.get(city); list.add(photo); } else { List list = new ArrayList<>(); list.add(photo); map.put(city, list); } } photoList.clear(); map.forEach((k, v) -> Collections.sort(v, new PhotoComparator())); for (String city : map.keySet()) { List sortedPhoto = map.get(city); int digits = getSize(sortedPhoto.size()); int number = 1; for (Photo p : sortedPhoto) { p.setName(p.getCity() + leadZeros(number, digits) + p.getName().substring(p.getName().lastIndexOf("."))); number++; photoList.add(p); } } Collections.sort(photoList, new Comparator() { @Override public int compare(final Photo mcc1, final Photo mcc2) { // TODO Implement this method return Integer.valueOf(mcc1.getId()).compareTo(mcc2.getId()); } }); //System.out.println("**********"); // photoList.forEach((p) -> System.out.println(p.getName())); return photoList.stream() .map(c -> c.getName()) .collect(Collectors.joining("\n")) .toString(); } public static String leadZeros(int number, int size) { // create variable length array of zeros char[] zeros = new char[size]; Arrays.fill(zeros, '0'); // format number as String DecimalFormat df = new DecimalFormat(String.valueOf(zeros)); return df.format(number); } public static int getSize(long number) { int count = 0; while (number > 0) { count += 1; number = (number / 10); } return count; } public static class Photo { int id; String name; String city; LocalDateTime photoDate; public Photo(int id, String photoName, String cityName, LocalDateTime photoTaken) { this.id = id; this.name = photoName; this.city = cityName; this.photoDate = photoTaken; } public void setId(int id) { this.id = id; } public int getId() { return id; } void setName(String name) { this.name = name; } String getName() { return name; } void setCity(String city) { this.city = city; } String getCity() { return city; } void setPhotoDate(LocalDateTime photoDate) { this.photoDate = photoDate; } LocalDateTime getPhotoDate() { return photoDate; } public String toString() { return this.getName() + "-" + this.getId(); } } public static class PhotoComparator implements Comparator { @Override public int compare(final Photo mcc1, final Photo mcc2) { return mcc1.getPhotoDate().compareTo(mcc2.getPhotoDate()); } } }

1 Answer

import java.text.*; import java.time.*; import java.time.format.*; import java.util.*; import java.util.stream.*; public class PhotoRename { public PhotoRename() { super(); } public static void main(String[] args) { PhotoRename photoRename = new PhotoRename(); String S = "photo.jpg Warsaw, 2013-09-05 14:08:15\n" + "john.png London, 2015-06-20 15:13:22\n" + "myFriends.png Warsaw, 2013-09-05 14:07:13\n" + "eiffel.jpg Paris, 2015-07-23 08:03:02\n" + "pisatower.jpg Paris, 2015-07-22 23:59:59\n" + "BOB.jpg London, 2015-08-05 00:02:03\n" + "notradame.png Paris, 2015-09-01 12:00:00\n" + "me.jpg Warsaw, 2013-09-06 15:40:22\n" + "a.png Warsaw, 2016-02-13 13:33:50\n" + "b.jpg Warsaw, 2016-01-02 15:12:22\n" + "c.jpg Warsaw, 2016-01-02 14:34:30\n" + "d.jpg Warsaw, 2016-01-02 15:15:01\n" + "e.png Warsaw, 2016-01-02 09:49:09\n" + "f.png Warsaw, 2016-01-02 10:55:32\n" + "g.jpg Warsaw, 2016-01-02 22:13:11\n" + "h.jpg Warsaw, 2016-01-02 22:12:11\n" + "i.jpg Warsaw, 2016-01-02 22:59:59"; System.out.println(solution(S)); } public static String solution(String S) { String[] photos = S.split("\n"); //System.out.println(Arrays.toString(photos)); int photoNumber = 1; List photoList = new ArrayList<>(); for (String s : photos) { String[] tokens = s.split("\\s"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dateTime = LocalDateTime.parse(tokens[2] + " " + tokens[3], formatter); photoList.add(new Photo(photoNumber, tokens[0], tokens[1].substring(0, tokens[1].lastIndexOf(',')), dateTime)); photoNumber++; } // Collections.sort(photoList,new PhotoComparator()); Map> map = new HashMap>(); for (Photo photo : photoList) { String city = photo.city; if (map.containsKey(city)) { List list = map.get(city); list.add(photo); } else { List list = new ArrayList<>(); list.add(photo); map.put(city, list); } } photoList.clear(); map.forEach((k, v) -> Collections.sort(v, new PhotoComparator())); for (String city : map.keySet()) { List sortedPhoto = map.get(city); int digits = getSize(sortedPhoto.size()); int number = 1; for (Photo p : sortedPhoto) { p.setName(p.getCity() + leadZeros(number, digits) + p.getName().substring(p.getName().lastIndexOf("."))); number++; photoList.add(p); } } Collections.sort(photoList, new Comparator() { @Override public int compare(final Photo mcc1, final Photo mcc2) { // TODO Implement this method return Integer.valueOf(mcc1.getId()).compareTo(mcc2.getId()); } }); //System.out.println("**********"); // photoList.forEach((p) -> System.out.println(p.getName())); return photoList.stream() .map(c -> c.getName()) .collect(Collectors.joining("\n")) .toString(); } public static String leadZeros(int number, int size) { // create variable length array of zeros char[] zeros = new char[size]; Arrays.fill(zeros, '0'); // format number as String DecimalFormat df = new DecimalFormat(String.valueOf(zeros)); return df.format(number); } public static int getSize(long number) { int count = 0; while (number > 0) { count += 1; number = (number / 10); } return count; } public static class Photo { int id; String name; String city; LocalDateTime photoDate; public Photo(int id, String photoName, String cityName, LocalDateTime photoTaken) { this.id = id; this.name = photoName; this.city = cityName; this.photoDate = photoTaken; } public void setId(int id) { this.id = id; } public int getId() { return id; } void setName(String name) { this.name = name; } String getName() { return name; } void setCity(String city) { this.city = city; } String getCity() { return city; } void setPhotoDate(LocalDateTime photoDate) { this.photoDate = photoDate; } LocalDateTime getPhotoDate() { return photoDate; } public String toString() { return this.getName() + "-" + this.getId(); } } public static class PhotoComparator implements Comparator { @Override public int compare(final Photo mcc1, final Photo mcc2) { return mcc1.getPhotoDate().compareTo(mcc2.getPhotoDate()); } } }

answered Jun 1 by Kuchi Bhargav
...