Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. /* package codechef; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.text.DateFormat;
  7. import java.text.SimpleDateFormat;
  8.  
  9. class Solution {
  10.  
  11. public String solution(String S) {
  12. String [] files = S.split("\n");
  13. int l = files.length;
  14. String []fName =new String[l];
  15. String []fExt =new String[l];
  16. String []fTime= new String[l];
  17. String []result =new String[l];
  18. Photo[]fileList =new Photo[l];
  19.  
  20. // Loop through the file list and store in corresponding arrays and an array-like object
  21. for(int i=0; i<files.length; i++){
  22. String file = files[i];
  23. fName[i] = file.substring(file.indexOf(",") + 2, file.lastIndexOf(","));
  24. fExt[i] = file.substring(file.indexOf("."), file.indexOf(","));
  25. fTime[i] = file.substring(file.lastIndexOf(",") + 2);
  26. fileList[i] = new Photo(fName[i],fExt[i],fTime[i],""+i);
  27. }
  28.  
  29. // Find the list of places (unique) and collect entries and corresponding timestamps for each place
  30. Set<String> placesSet = new HashSet<String>(Arrays.asList(fName));
  31. String [] places = new String[placesSet.size()];
  32. int i = 0;
  33. for (String p : placesSet){
  34. places[i++] = p;
  35. }
  36. for(int j=0; j<places.length; j++){
  37. List<Integer> fiList = new ArrayList<Integer>();
  38. List<Date> tStamp = new ArrayList<Date>();
  39. for(int k=0; k<fileList.length; k++){
  40. if(fileList[k].name == places[j]){
  41. fiList.set(fiList.size(), k);
  42. tStamp.set(tStamp.size() ,getDateTime(fTime[k]));
  43. }
  44. }
  45.  
  46. // Sort based on timestamp and update 'position' value (with prefix, if required) based on resulting order
  47. Collections.sort(tStamp);
  48. int position = 1;
  49. String prefix = (fiList.size() >= 100 ? "100" : (fiList.size() >= 10 ? "10" : ""));
  50.  
  51. for(int d=0; d<fiList.size(); d++){
  52. for(int m=0; m<tStamp.size(); m++){
  53. if(fileList[fiList.get(d)].timestamp == tStamp.get(m)){
  54. fileList[fiList.get(d)].position = (prefix.equals("10") ? ((m+1) < 10 ? "0" : "") : (prefix.equals("100") ? ((m+1) < 100 ? "0" : "") : "")) + (m + 1);
  55. }
  56. }
  57. }
  58. }
  59.  
  60. // Loop through the file list and store in the new format
  61. for(int n=0; n<fileList.length; n++){
  62. result[n] = fileList[n].name + fileList[n].position + fileList[n].extension;
  63. }
  64. return arrayToString(result);
  65. }
  66. public static String arrayToString(String array[])
  67. {
  68. if (array.length == 0) return "";
  69. StringBuilder sb = new StringBuilder();
  70. for (int i = 0; i < array.length; ++i)
  71. {
  72. sb.append(",'").append(array[i]).append("'");
  73. }
  74. return sb.substring(1);
  75. }
  76.  
  77. public Date getDateTime(String dateString) {
  78. try {
  79. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  80. return dateFormat.parse(dateString);
  81. }
  82. catch (Exception e) {
  83. return null;
  84. }
  85. }
  86. }
  87.  
  88. class Photo implements Comparable<Photo> {
  89.  
  90. public String name;
  91. public String position;
  92. public String DesiredPosition;
  93. public String FinalName;
  94. public String extension;
  95. public String City;
  96. public Date timestamp;
  97. public Date DateTime;
  98. public int compareTo(Photo other) {
  99. if ((other == null)) {
  100. return 1;
  101. }
  102. else {
  103. return this.DateTime.compareTo(other.DateTime);
  104. }
  105.  
  106. }
  107.  
  108. public Photo(String name, String extension, String timestamp, String position) {
  109. this.name = name;
  110. this.extension = extension;
  111. this.timestamp = getDateTime(timestamp);
  112. //this.DateTime = getDateTime(timestamp);
  113. this.position = position;
  114. }
  115. public Date getDateTime(String dateString) {
  116. try {
  117. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  118. return dateFormat.parse(dateString);
  119. }
  120. catch (Exception e) {
  121. return null;
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement