Spotify Labs Puzzles -Medium- Solution with Java

by Süleyman Petek 9. Mayıs 2016 11:40
import java.util.*;public class SpotifyMedium {    public static void main(String[] args)

import java.util.*;


public class SpotifyMedium {


    public static void main(String[] args) {       


        List<String> indata = new ArrayList<String>();


        System.out.println("Please enter the count of played times and name of the song with a one char gap...");

        System.out.println("e.g. 487 LetItBe");

        String line;

        Scanner stdin = new Scanner(System.in);

        while (stdin.hasNextLine() && !(line = stdin.nextLine()).equals("")) {

            String[] tokens = line.split(" ");

            indata.add(tokens[0] + ":" + tokens[1]);

        }


        System.out.println("Please enter the total song to select...");

        int totalSelect = Integer.parseInt(stdin.nextLine());

        stdin.close();


        String[][] songList = new String[indata.size()][3];

        for (int i = 0; i < indata.size(); i++) {

            String[] row = indata.get(i).split(":");

            songList[i][0] = row[0];

            songList[i][1] = row[1];

            songList[i][2] = String.valueOf(i + 1);

        }

    

        String[][] calculatedSongList = new String[songList.length][3];


        for (int i = 0; i < songList.length; i++) {

            for (int j = 0; j < songList[i].length; j++) {

                String zipValue = String.valueOf(ZipsLaw(songList.length, i + 1));

                String qualityValue = String.valueOf(SongQuality(Double.parseDouble(songList[i][0]), Double.parseDouble(zipValue)));

                calculatedSongList[i][j] = songList[i][j] + ":" + qualityValue;

            }

        }


        Arrays.sort(calculatedSongList, new Comparator<String[]>() {

            @Override

            public int compare(final String[] entry1, final String[] entry2) {

                String strObj1[] = entry1[0].split(":");

                String strObj2[] = entry2[0].split(":");

                

                Double obj1 = new Double(strObj1[1]);

                Double obj2 = new Double(strObj2[1]);

                int retval = obj2.compareTo(obj1);

                if (retval > 0) {

                    //System.out.println("obj1 is greater than obj2");

                } else if (retval < 0) {

                    //System.out.println("obj1 is less than obj2");

                } else {

                    //System.out.println("obj1 is equal to obj2");                    

                }

                return retval;

            }

        });


        int total = 0;

        for (final String[] s : calculatedSongList) {

            if (total < totalSelect) {

                System.out.println(s[1].split(":")[0]);

            }

            total++;

        }             

    }


    public static double ZipsLaw(double n, double i) {

        return (n * n) / i;

    }


    public static double SongQuality(double fi, double zi) {

        return fi / zi;

    }


}


Tags: , , , ,

Tech | Tip

Calendar

<<  Kasım 2018  >>
PztSalÇarPerCumCmtPaz
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

View posts in large calendar

RecentPosts