Random Problem Challenge

Post Reply
kensie
Posts: 1
Joined: Wed Jul 30, 2008 11:32 am

Random Problem Challenge

Post by kensie »

What should the format of the answer be? Just a number? also many numbers would throw an Exception which one will work?
gfoot
Posts: 269
Joined: Wed Sep 05, 2007 11:34 pm
Location: Brighton, UK

Post by gfoot »

Just a number. I'm not a Java expert so I can't comment on how many numbers would throw an exception... but if the site is not accepting your answers, have you tried compiling the code and trying them out to make sure they really do throw exceptions?
User avatar
sabretooth
Posts: 61
Joined: Sun Jul 12, 2009 3:13 pm

Post by sabretooth »

I really don't understand this problem.

2147483647 is the max integer value in java - therefore this value and every possible value above this will cause an Exception. So why isnt it accepted? If you are looking for a 'specific' integer then the challenge should say so.

Any advice?

sabre
gfoot
Posts: 269
Joined: Wed Sep 05, 2007 11:34 pm
Location: Brighton, UK

Post by gfoot »

It's asking specifically for a value for 'randomNumber', which is declared as an int, so your answer has to be a valid int. I don't think 2147483647 will cause an exception, so no, that's not the answer, and higher numbers are not valid ints, so they don't count as answers either.
Theino
Posts: 8
Joined: Sun Mar 15, 2009 10:35 pm

Post by Theino »

The answer is a valid int.

There is only one answer and thus only one valid int that would cause an exception.
User avatar
sabretooth
Posts: 61
Joined: Sun Jul 12, 2009 3:13 pm

Post by sabretooth »

Many thanks for the response :)

sabre

edit - solved.

Thanks again :)
General_Payne
Posts: 20
Joined: Mon Jun 20, 2011 6:25 pm

Struggling

Post by General_Payne »

Guessing there is some trick to this? Fairly sure i have run it for everything in the range -2147483647 to 2147483647 without an error

[Edit] -Scratch That. Was Being Retarded. Can't believe i spent over an hour on something so easy and obvious!!
meis
Posts: 16
Joined: Tue Aug 28, 2012 5:22 pm
Location: Italy

Post by meis »

I've rewritten it in PHp to read it better (I'm more familiar with it).
So I have:

Code: Select all

<?php
function bucketFromRandom($randomNumber) {
    $a = array();
	for ($i = 0; $i < 10; $i++)
		$a[$i] = $i * $randomNumber;
	$index = abs($randomNumber) % 10;
	return $a[$index];
}
echo bucketFromRandom(2147483648);
?>
Will it work in PHP too?
And what do I have to look for? I can't get it! But maybe i just don't understand what an exception is...
User avatar
Rat62
Posts: 17
Joined: Tue Nov 23, 2010 5:00 pm

Post by Rat62 »

meis wrote:I've rewritten it in PHp to read it better (I'm more familiar with it).
So I have:

Code: Select all

<?php
...
?>
Will it work in PHP too?
...
Many challlenges on this site urge you to learn new things: Computer languages, cryptologic techniques, file formats.
This one is special to Java (and probably close relatives like C#). It won't work in PHP.
Post Reply