2 Replies - 337 Views - Last Post: 27 December 2012 - 12:11 PM Rate Topic: -----

#1 recheej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-December 12

Spotify Puzzle has me... puzzled

Posted 27 December 2012 - 10:03 AM

Hello peeps again. I feel like I've been asking a lot of questions. I'm new to this forum so if there's any thing I'm doing wrong please let me know. But here's my dilemma. I'm trying to solve this problem located on the spotify website:

http://www.spotify.c...ticket-lottery/


It's very interesting. I finally got all the code up for it, and it seems like I am getting the right answer. But when I submit the code, they tell me it's wrong. Here's my code:

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStreamReader;

/**
 * 
 * @author Rechee Jozil
 * Written 12/26/2012
 */


public class Internship {
	
	
	public static void main(String[] args) throws IOException{
		
		double denomenator = 0.0,  numerator = 0.0, probability = 0.0, pEntered = 0.0, winners = 0.0, tickets = 0.0, pGroup = 0.0;
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
		
		//Array list to hold values.
		ArrayList<Double> lotteryInput = new ArrayList<Double>();


		//Add values entered from input stream to arraylist
		System.out.println("Enter values");
		
		//Split string up
		String[] st = r.readLine().split(" ");
		
		//Parse each element in st into double
		for(int i = 0; i < 4 ; i++){
				lotteryInput.add(Double.parseDouble(st[i]));
		}
		r.close();

		//Value checking
		while(lotteryInput.get(0) < 1 || lotteryInput.get(0) > 1000){
			System.out.println("Number of people in lottery cannot be less than 1 or more than 100. Enter number of people in lottery again.");
			Double.parseDouble(r.readLine());
			lotteryInput.set(0, pEntered);
	
		}
		while(lotteryInput.get(1) < 1 || lotteryInput.get(1) > lotteryInput.get(0)){
			System.out.println("Number of winners cannot be less than 1 or greater than number of people in lottery. Enter number of winners again.");
			Double.parseDouble(r.readLine());
			lotteryInput.set(1, winners);
		}
		while(lotteryInput.get(2) < 1 || lotteryInput.get(2) > 100){
			System.out.println("Number of tickets cannot be less than 1 or greater than 100. Enter numbef of tickets again. ");
			Double.parseDouble(r.readLine());
			lotteryInput.set(2, tickets);
		}
		while(lotteryInput.get(3) < 1 || lotteryInput.get(3) > lotteryInput.get(0)){
			System.out.println("Number of people in your group cannot be less than 1 or greater than number of people that entered. Enter number of people in group again. ");
			Double.parseDouble(r.readLine());
			lotteryInput.set(3, pGroup);
		}
		
		//Close buffer 
		r.close();
	
		pEntered = lotteryInput.get(0);
		winners = lotteryInput.get(1);
		tickets = lotteryInput.get(2);
		pGroup = lotteryInput.get(3);
		
		//Main for loop for calculation of probablility
		for(int k = 1; k <= pGroup; k++){
		denomenator = (double) (factorial(pEntered) / (double) (factorial(winners) * factorial(pEntered - winners)));
		numerator = (double) (factorial(pGroup)/ (double) ((factorial(k) * factorial(pGroup - k))) * (double) ((factorial(pEntered - pGroup)) / (double) (factorial(winners - k) * factorial((pEntered - pGroup)- (winners - k)))));
		probability =probability + ((numerator/ denomenator) * TicketProb(pGroup, k, tickets));
		}
		
		System.out.println(probability);
		
	
		
	}
	
	// static recursive factorial method
	 static double factorial(double n){
		if (n == 0){
			return 1;
		}else{
			return n * factorial(n-1);
		}

	}
	 
	 static double TicketProb(double x, double y, double z){
		 if(x <= (y * z)){
			 return 1.0;
		 }
		 else{
			 return 0.0;
		 }
	 }
		 
	 }




If you plug in the sample input given by spotify, my answers are the same. Except with the third sample input where mine is 1.0 and there's is 1.0 with 0 repeating.

I think I've covered all angles. I've edited the code several times with no avail. Any body have any insight on how I might solve this problem?

Is This A Good Question/Topic? 0
  • +

Replies To: Spotify Puzzle has me... puzzled

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Spotify Puzzle has me... puzzled

Posted 27 December 2012 - 11:52 AM

Now I would normally say you are right, and spotify's output is wrong, there is no need to repeat trailing zeros. But if you want to make them happy and give your answers to the power of 10^9. You can use decimal format:
DecimalFormat df = new DecimalFormat("#.#########");
System.out.println( df.format( yourResult )) ;


Was This Post Helpful? 1
  • +
  • -

#3 recheej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-December 12

Re: Spotify Puzzle has me... puzzled

Posted 27 December 2012 - 12:11 PM

View PostSwiftStriker00, on 27 December 2012 - 11:52 AM, said:

DecimalFormat df = new DecimalFormat("#.#########");
System.out.println( df.format( yourResult )) ;


Hmm. It seems like the format object doesn't print out the last sample input right. I appreciate the help though. Seems like no one else has a clue so I'll just keep working on it. Might be something I missed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1