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

Spotify Labs Puzzles -Easy- Solution with Java

by Süleyman Petek 5. Mayıs 2016 17:25
public static void main(String[] args) {&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;

public static void main(String[] args) {        

        int yourNumber; 

        Scanner in = new Scanner(System.in);

        System.out.println("Enter your number");

        yourNumber = in.nextInt();        

        

        if (yourNumber < 0) {

            System.out.println("Error: Less then Zero");

        } 

        else 

        { 

            System.out.print("Binary is: ");           

            toBinary(yourNumber);

        }

    }

 

    public static void toBinary(int int1){       

       System.out.println(Integer.toBinaryString(int1));               

       String b = new StringBuffer(Integer.toBinaryString(int1)).reverse().toString();       

       System.out.println("The reverse in binary format is: "+ b);                  

       int decimalValue = Integer.parseInt(b, 2);           

       System.out.println("The reverse decimal is:" +decimalValue); 

   }


For harder ones, please visit https://labs.spotify.com/puzzles/ (:

Tags: , , , , ,

Geyik | Innovation | Tech | Tip

Calendar

<<  Temmuz 2018  >>
PztSalÇarPerCumCmtPaz
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar

RecentPosts