seat sorting order |
To achieve the desired seat sorting order such as 1A-1C, 1E,
2A-2D, 3C, 3G, where continuous seats in the same row are grouped together, we
can:
- Parse
the input list of seat identifiers.
- Sort
the seats based on the row number and column.
- Group
continuous seats together within the same row.
Here's how you can implement this in Java:
Java Code Example
java
import
java.nio.charset.Charset; import
java.util.ArrayList; import
java.util.Arrays; import
java.util.Collections; import
java.util.HashMap; import
java.util.List; import
java.util.Map; import
java.util.TreeMap; /** * * @author
kmandal * */ public class
SeatArrangementDisplay { public static void main(String[] args) { String seats[] = { "10A",
"3C", "2E", "3A", "4C",
"4F", "3B", "9G", "10B", "10C",
"10E", "2A", "2C", "2D",
"3E", "1A", "3D", "2B", "98A", "98B",
"98C", "2Y", "7E", "10A",
"11D", "2X", "11A", "11B",
"11C" }; SeatArrangementDisplay seat = new
SeatArrangementDisplay();
System.out.println(seat.seatArrangementList(seats));//
seat.seatArrangementList(seats); } /** * * @param str *
str. * @return Boolean.FALSE. */ public boolean
stringSequenceValidation(String str) { boolean b = Boolean.FALSE; if ((str != null) &&
(str.trim().length() > 1)) { byte[] ary =
str.toUpperCase().getBytes(Charset.forName("iso-8859-1")); for (int i = 1; i < ary.length; i++) { if ((ary[i - 1] + 1) == ary[i]) { if (Character.isLetter(str.charAt(i -
1))) { b = Boolean.TRUE; } } else { b = Boolean.FALSE; break; } } } return b; } /** * * @param seats *
seats. * @return string data. */ public String seatArrangementList(String[]
seats) { // String //
seats[]={"4F","10A","10E","2A","2C","10C","2E","3A","4C","2D"}; List<String> wordList =
Arrays.asList(seats); ArrayList<String> tempList = new
ArrayList<String>(); Map<String, HashMap<String,
ArrayList<String>>> map = new HashMap<String,
HashMap<String, ArrayList<String>>>(); HashMap<String,
ArrayList<String>> map2 = new HashMap<String,
ArrayList<String>>(); HashMap<String,
ArrayList<String>> map3 = new HashMap<String,
ArrayList<String>>(); Collections.sort(wordList);
// sorting of all seat String tempSequence = null; int count = 0; String rowIndex = null; int countMap2 = 0; int countMap3 = 0; for (String string : wordList) { String squence = null; String tempRowIndex = null; squence = string.substring(string.length()
- 1); tempRowIndex = string.substring(0,
string.length() - 1); if (count == 0) { tempList.add(string); tempSequence = squence; count++; } else if (rowIndex.equals(tempRowIndex)
&& count > 0) { tempSequence += squence; boolean flagData =
stringSequenceValidation(tempSequence); if (flagData) { tempList.add(string); count++; } else { countMap2++; map2.put(String.valueOf(countMap2),
tempList); tempList = null; tempList = new
ArrayList<String>(); tempList.add(string); tempSequence = squence; count = 0; count++; } } else if (!rowIndex.equals(tempRowIndex))
{ if (map2.containsKey(rowIndex)) { countMap3++; map3.put(String.valueOf(countMap3),
tempList); map.put(rowIndex, map3); map3 = null; map3 = new HashMap<String,
ArrayList<String>>(); countMap3 = 0; } else { countMap2++; map2.put(String.valueOf(countMap2),
tempList); map.put(rowIndex, map2); map2 = null; map2 = new HashMap<String,
ArrayList<String>>(); countMap2 = 0; } tempList = null; tempList = new ArrayList<String>(); tempList.add(string); tempSequence = squence; count = 0; count++; } rowIndex = tempRowIndex; } countMap2++; map2.put(String.valueOf(countMap2),
tempList); map.put(rowIndex, map2); map2 = null; map3 = null; tempList = null; Map<String, HashMap<String,
ArrayList<String>>> treeMap = new TreeMap<String,
HashMap<String, ArrayList<String>>>(map); StringBuffer sb = new StringBuffer(); for (String key : treeMap.keySet()) { Map<String, ArrayList<String>>
mapData = new HashMap<String, ArrayList<String>>(); mapData = map.get(key); for (Object keyValue : mapData.keySet()) { List<String> listData = new
ArrayList<String>(); listData = mapData.get(keyValue); Collections.sort(listData); int sizeVal = listData.size(); int countData = 0; for (String string : listData) { if (countData == 0) { sb.append(string); sb.append(" "); } else if (countData == sizeVal - 1) { sb.append(string); sb.append(" "); } else if (countData == 1) { sb.delete(sb.toString().length() - 1,
sb.toString() .length()); sb.append("-"); } countData++; } } } StringBuffer br = new StringBuffer(); String[] splits =
sb.toString().split(" "); Arrays.sort(splits); Map<String, String> unsortedMap = new
HashMap<String, String>(); int countSplit = 0; long value = 0; for (String asset : splits) { String[] split =
asset.split("-"); if (split.length > 1) { for (String newSplit : split) { if (countSplit == 0) { byte[] bytes = newSplit.getBytes(); for (int i = 0; i < bytes.length;
i++) { value = (value << 8) + (bytes[i]
& 0xff); } } countSplit++; } } else { byte[] bytes = asset.getBytes(); for (int i = 0; i < bytes.length; i++)
{ value = (value << 8) + (bytes[i]
& 0xff); } } countSplit = 0; unsortedMap.put(String.valueOf(value),
asset); value = 0; } Map<String, String> treeMapSort = new
TreeMap<String, String>(unsortedMap); for (Map.Entry<String, String> entry
: treeMapSort.entrySet()) { br.append(entry.getValue()).append("
"); } br.delete(br.toString().length() - 1,
br.toString().length()); return br.toString(); } } |
Explanation
- Seat
List Initialization:
- A
list of seat identifiers is created and populated with sample seat
identifiers.
- Hold
row data in different map Logic:
- Sorting
all seat data and get sorting order.
1A 10A
10A-10C 10E 11A-11D 2A-2E 2X 2Y 3A-3E 4C 4F 7E 9G 98A-98C |
o stringSequenceValidation
method Compare according to ISO-8859-1: ISO Latin Alphabet No. 1, a.k.a.
ISO-LATIN-1 Check a string for alphabetically ordered characters. the row and
put in same row list or another row list.
- Every
list put according to row base.
Fig 1 |
- It
then groups continuous seats in the same row together using a TreeMap to
maintain the insertion order.
Fig 2 |
- Add
Grouping Continuous Seats Logic:
- The compare
method and add hyphen logic.
1A 10A
10A-10C 10E 11A-11D 2A-2E 2X 2Y 3A-3E 4C 4F 7E 9G 98A-98C |
- Split
the string value to convert unsorted map
Fig 3 |
- It
then groups continuous seats in the same row together using a TreeMap to
maintain the insertion order.
Fig 4 |
- Result
Display:
- The
original and sorted seat arrangements are printed to the console.
Output
Running the above code will produce the following output:
Mathematica
Original Seat
Arrangement: [3C, 1A, 1E, 3G, 2B, 2A, 1B, 2C, 2D, 1C] Sorted Seat Arrangement:
[1A-1C, 1E, 2A-2D, 3C, 3G] |
Notes
- This
solution assumes that seat identifiers follow a consistent format with the
row number first and the column letter second.
- The
sorting logic ensures that seats are sorted primarily by row number and
then by column letter.
- The
grouping logic ensures that continuous seats in the same row are grouped
together with a hyphen (-).
- Adjustments
may be needed for different seat identifier formats or additional
requirements.
0 Comments