Optimized solution for a rolling dice code puzzle [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I was asked to provide the solution of this rolling dice problem in an interview. When I showed him my solution, he said that there is another way to find the solution. I am looking for answer in PHP script only.
Question:
Two persons are playing a game of rolling 2 dices. Each person rolls the two dices n times and records the outcomes (sum of value of two dices) of all the n attempts. So after n attempts of both the player we have two list corresponding to the n outcomes of two players.
They want to know that whether they have got all possible outcomes( 1 to 12) same number of times or not. If they got all the possible outcomes same number of times then they are called lucky otherwise unlucky.
Input: Two Integer Arrays (L1, L2) corresponding to outcomes of two players.
Output: Lucky or Unlucky depending on the case
My Answer:
<?php
function rollingdice($input1,$input2)
{
foreach($input1 as $k=>$a)
{
if(in_array($a,$input2))
{$p = array_search($a, $input2);
unset($input2[$p]);
}
else
{ return 'Unlucky';}
}
return 'Lucky';
}
?>

<?php
function rollingdice($input1,$input2)
{
$a = array_count_values($input1);
$b = array_count_values($input2);
if(array_diff_assoc($a,$b) || array_diff_assoc($b,$a)) { return 'Unlucky';}
return 'Lucky';
}

Related

How to speed up these R computations? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have to data frames with X, Y, and Z coordinates. I want to find the distance between all of the points in the two data frames. (Like the distance between entry A1 and every entry in B, A2 and every entry in B, and so on, and vice versa). I basically did this:
1.) Wrote a function that calculates the distance between two points.
2.) Used the distanceFinder function to create a function that finds the distance between one point in a group, and every other point in the opposite group.
3.) Created a function called bigDistance() that calls filter() on every entry in one group, and appends the results to an empty data frame through a for loop until it's completed.
This code takes about 2 minutes to run on the file I'm experimenting with, and I just found out that I have to translate this algorithm to PHP... so I guess this is kind of an optimization question, because I feel like PHP would be way slower at making these computations than R? Sorry if people find this "off-topic" but yeah, super new to programming and Big O notation and stuff, so any tips would be amazing! Thanks!
The dist function does exactly what you are looking for.
myDf <- data.frame(
x = rnorm(8),
y = rnorm(8),
z = rnorm(8)
)
dist(myDf)
# 1 2 3 4 5 6 7
# 2 3.0457054
# 3 1.7260658 3.2107845
# 4 1.2839101 3.4596211 2.9451175
# 5 1.5656231 4.0154389 2.3421445 2.3612348
# 6 1.9294650 1.6655718 1.7977887 2.8726174 2.5815296
# 7 2.1842743 3.5274692 3.8552701 1.0984651 2.9951244 3.3220919
# 8 1.4795857 3.5364663 0.5567753 2.7033371 1.9226225 2.0631788 3.6624082
It seems to be pretty fast as well (73ms on average)
library(microbenchmark)
mb <- microbenchmark(dist(myDf))
mb
# Unit: microseconds
# expr min lq mean median uq max neval
# dist(myDf) 70.436 71.453 77.4083 72.978 82.133 172.911 100
autoplot(mb)

How to check if number of rows = 6n-1 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I wish to count a number of rows in my sql database, and if the number of rows equal 6n-1 perform a function, if not, do nothing.
I know I can specify the integers to look for, but there would be thousands, so I wish to find a way where I can just define the math 6n-1
for example, when I count the rows in a table, I want to check whether there is 5,11,17 and so on.
If the number of rows match, do something, else do nothing.
Since 5,11,17.... is equal to every 6th row starting from 5, this can be expressed as 6n-1.
IS there a way to do this without defining every integer to check?
($rowcount + 1) % 6 == 0
perhaps...
for ( $i=1 ; 6*$i-1 <= $database_rows ; $i++ ) {
if ( 6*$i-1 == $database_rows ) {
do_something();
}
}

All integers in the series will lie in the range [-10^6,+10^6] means? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I found the one of the condition as "All integers in the series will lie in the range
[-10^6,+10^6]" in one of the online test what does it means?
From the Wikipedia page on intervals you can see;
A closed interval includes its endpoints, and is denoted with square brackets. For example [0,1] means greater than or equal to 0 and less than or equal to 1.
10^6 (10 to the power of 6) is the same as 1 million, so All integers in the series will lie in the range
[-10^6,+10^6] means that all numbers in the series are;
Greater than or equal to -1000000 (minus one million)
Less than or equal to 1000000 (one million)

Diffrence between a dot and comma in cost value in php [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
My client(from denmark) give the value to use in cost value in php.
He said " Use this , The Cost is 50.000 Dkr half product , 200.000 Dkr. for full product. "
i am confused in comma and dot ? what is correct value ? its 50.000 or 50,000.
This isn't a coding question
Commas are used in the English convention to separate thousands (50,000 = fifty thousand. 50.000 = fifty point zero zero zero)
In Continental Europe commas are used as the decimal separator (50,5 = fifty and a half)
Please do a bit of research before your next question. This one is easily answered here

Interviewstreet coding challenge input constraints [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
A question about interview street input constraints. (http://interviewstreet.com/)
Is it necessary to check the inputs for errors in the interviewstreet challenges?
For example, one challenge details the following constraints for the STDIN content:
1 <= N <= 1,00,000(10^5)
1 <= K <= N
0 <= profit value of any billboard <= 2,000,000,000(2*10^9)
Do I have to write some code to check the values to make sure that they meet these constraints or can I just assume that they do.
Also, if I do have to write the code to check what do I output if the inputs are incorrect?
Thanks
You can take it for graneted that inputs will always be adhering to the given constraints.
You don't need write any extra code for checking if input is within given constraints.
So if they say N will be <= 1,00,000, you can use an array of exactly 1,00,000 to store the elements and you'll be fine.

Categories