-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMusicYourWay.java
78 lines (57 loc) · 2.05 KB
/
MusicYourWay.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Problem: https://open.kattis.com/problems/musicyourway
Author: Adrian Reithaug
Submitted: January 7th, 2018
Time: 0.49s / 1.00s
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class MusicYourWay {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] attributes = reader.readLine().split(" ");
int numSongs = Integer.parseInt(reader.readLine());
Song[] songData = new Song[numSongs];
String attributesLine = attributesToString(attributes);
for (int i = 0; i < numSongs; i++) {
songData[i] = new Song(reader.readLine().split(" "));
}
int numSortingCommands = Integer.parseInt(reader.readLine());
while (numSortingCommands-- > 0) {
String sortByAttribute = reader.readLine();
int type = Arrays.asList(attributes).indexOf(sortByAttribute);
System.out.println(attributesLine);
sortSongs(songData, type);
printSongs(songData);
}
reader.close();
}
public static String attributesToString(String[] attributes) {
String attributesLine = "";
for (String attribute : attributes) {
attributesLine += attribute + " ";
}
return attributesLine;
}
public static void printSongs(Song[] songData) {
for (Song song : songData) {
for (String metadata : song.metadata) {
System.out.print(metadata + " ");
}
System.out.println();
}
System.out.println();
}
public static void sortSongs(Song[] songData, int type) {
Arrays.sort(songData, (Song song1, Song song2) -> song1.metadata[type].compareTo(song2.metadata[type]));
}
public static class Song {
String[] attributes;
String[] metadata;
public Song(String[] metadata) {
this.metadata = metadata;
}
}
}