How do I round a number to the nearest 5 in my coffee script file? - coffeescript

I have a restaurant recommendation app, built in Rails 4.2, and using Mithril.Js. When user searches for a restaurant, I tell him how many results I found using the code below.
How do I adapt this to show him the number of results rounded up to the nearest 5 (if <10 results), and to the nearest 10 (if <100 results), and to the nearest 100 (if <1000 results)?
RESTAURANTS.COFFEE FILE
App.c.restaurants =
controller: ->
loadMore: ->
loading = true
pubsub.publish 'search', page: store[store.length-1].page+1
view: (ctrl) ->
head = if loading
'Calculating...'
else if store.length
"About #{store[0].totals || 0} restaurants"
else
''

Here is a simple function you could define
round_to_nth = (number, nth) ->
if number % nth >= (nth/2) then parseInt(number / nth) * nth + nth else parseInt(number / nth) * nth
and use
"About #{round_to_nth(store[0].totals, 100) || 0} restaurants" # for nearest 100th
"About #{round_to_nth(store[0].totals, 5) || 0} restaurants" # for nearest 5th

Related

Finding number position in ranges

I am writing a function that has to find the position of a given number within numerical ranges, the range is a variable within the code, for now lets say the range is 4 so the ranges will look like the following:
[ 0-3 ]
[ 4-7 ]
[ 8-11 ]
[ 12-15 ]
[ 16-19 ]
[ 20-23 ]
[ 24-27 ]
What i would like to achieve is to find the range where a given number belongs to, in the quickest way possibly,as this operation is performed over million of events.
So what i have wrote so far, and it works fine, is the following:
public String findRange(int range,int number2bFound)
{
int base = 0;
if (number2bFound == 0)
number2bFound = 1.0;
int higher = 0;
while (base <= number2bFound)
{
higher = base + (range - 1);
if ((base <= number2bFound) && (higher >= number2bFound))
return base + "-" + higher;
base += range;
}
return null;
}
So as i said this works, but i am sure this can be done implemented more efficiently, by only using the value of number2bFonud and the range and excluding the very expensive loop.
if all the ranges have the same size and it start from 0, a simple division will do, additionally you can find the position in the sub-range with a modulo operation.
The procedure is simple, find the integer division of your number n against yours range size and that will give you in which sub-range it belong, to find the position inside the sub-range find the modulo of your number again against the range size
here is a example python
def find_position(n,size):
return (n//size, n%size)
with range of size 4
>>> test=[ [0,1,2,3], [4,5,6,7], [8,9,10,11], [12,13,14,15], [16,17,18,19], [20,21,22,23] ]
>>> find_position(6,4)
(1, 2)
>>> test[1][2]
6
>>> find_position(11,4)
(2, 3)
>>> test[2][3]
11
>>>
with range 5
>>> test=[ [0,1,2,3,4], [5,6,7,8,9] ,[10,11,12,13,14],[15,16,17,18,19], [20,21,22,23,24] ]
>>> find_position(11,5)
(2, 1)
>>> test[2][1]
11
>>>
The procedure is a follow, let Size be the size of each sub-range and n the number you wan to locate, then you only need to the be the take the number n you want to find.
translate that to java should be very simple, excuse if my is a little rusty but is something like this I think
public String findRange(int range,int number2bFound){
int sub_ran_pos, pos;
sub_ran_pos = (int) number2bFound/range; //or however the integer division is in java
pos = number2bFound % range; //or however the modulo operation it is in java
return sub_ran_pos + "-" + pos; //or the appropriate return type, for this
}
(I don't remember, but if java is 1-index then you need to add 1 to each number to get right result)

speed up prime number generating

I have written a program that generates prime numbers . It works well but I want to speed it up as it takes quite a while for generating the all the prime numbers till 10000
var list = [2,3]
var limitation = 10000
var flag = true
var tmp = 0
for (var count = 4 ; count <= limitation ; count += 1 ){
while(flag && tmp <= list.count - 1){
if (count % list[tmp] == 0){
flag = false
}else if ( count % list[tmp] != 0 && tmp != list.count - 1 ){
tmp += 1
}else if ( count % list[tmp] != 0 && tmp == list.count - 1 ){
list.append(count)
}
}
flag = true
tmp = 0
}
print(list)
Two simple improvements that will make it fast up through 100,000 and maybe 1,000,000.
All primes except 2 are odd
Start the loop at 5 and increment by 2 each time. This isn't going to speed it up a lot because you are finding the counter example on the first try, but it's still a very typical improvement.
Only search through the square root of the value you are testing
The square root is the point at which a you half the factor space, i.e. any factor less than the square root is paired with a factor above the square root, so you only have to check above or below it. There are far fewer numbers below the square root, so you should check the only the values less than or equal to the square root.
Take 10,000 for example. The square root is 100. For this you only have to look at values less than the square root, which in terms of primes is roughly 25 values instead of over 1000 checks for all primes less than 10,000.
Doing it even faster
Try another method altogether, like a sieve. These methods are much faster but have a higher memory overhead.
In addition to what Nick already explained, you can also easily take advantage of the following property: all primes greater than 3 are congruent to 1 or -1 mod 6.
Because you've already included 2 and 3 in your initial list, you can therefore start with count = 6, test count - 1 and count + 1 and increment by 6 each time.
Below is my first attempt ever at Swift, so pardon the syntax which is probably far from optimal.
var list = [2,3]
var limitation = 10000
var flag = true
var tmp = 0
var max = 0
for(var count = 6 ; count <= limitation ; count += 6) {
for(var d = -1; d <= 1; d += 2) {
max = Int(floor(sqrt(Double(count + d))))
for(flag = true, tmp = 0; flag && list[tmp] <= max; tmp++) {
if((count + d) % list[tmp] == 0) {
flag = false
}
}
if(flag) {
list.append(count + d)
}
}
}
print(list)
I've tested the above code on iswift.org/playground with limitation = 10,000, 100,000 and 1,000,000.

Randomly selecting letters by frequency of use

After feeding few Shakespeare books to my Perl script I have a hash with 26 english letters as keys and the number of their occurences in texts - as value:
%freq = (
a => 24645246,
b => 1409459,
....
z => 807451,
);
and of course the total number of all letters - let's say in the $total variable.
Is there please a nice trick to generate a string holding 16 random letters (a letter can occur several times there) - weighted by their frequency of use?
To be used in a word game similar to Ruzzle:
Something elegant - like picking a random line from a file, as suggested by a Perl Cookbook receipt:
rand($.) < 1 && ($line = $_) while <>;
The Perl Cookbook trick for picking a random line (which can also be found in perlfaq5) can be adapted for weighted sampling too:
my $chosen;
my $sum = 0;
foreach my $item (keys %freq) {
$sum += $freq{$item};
$chosen = $item if rand($sum) < $freq{$item};
}
Here, $sum corresponds to the line counter $. and $freq{$item} to the constant 1 in the Cookbook version.
If you're going to be picking a lot of weighted random samples, you can speed this up a bit with some preparation (note that this destroys %freq, so make a copy first if you want to keep it):
# first, scale all frequencies so that the average frequency is 1:
my $avg = 0;
$avg += $_ for values %freq;
$avg /= keys %freq;
$_ /= $avg for values %freq;
# now, prepare the array we'll need for fast weighted sampling:
my #lookup;
while (keys %freq) {
my ($lo, $hi) = (sort {$freq{$a} <=> $freq{$b}} keys %freq)[0, -1];
push #lookup, [$lo, $hi, $freq{$lo} + #lookup];
$freq{$hi} -= (1 - $freq{$lo});
delete $freq{$lo};
}
Now, to draw a random weighted sample from the prepared distribution, you just do this:
my $r = rand #lookup;
my ($lo, $hi, $threshold) = #{$lookup[$r]};
my $chosen = ($r < $threshold ? $lo : $hi);
(This is basically the Square Histogram method described in Marsaglia, Tsang & Wang (2004), "Fast Generation of Discrete Random Variables", J. Stat. Soft. 11(3) and originally due to A.J. Walker (1974).)
I have no clue about Perl syntax so I'll just write pseudo-code. You can do something like that
sum <= 0
foreach (letter in {a, z})
sum <= sum + freq[letter]
pick r, a random integer in [0, sum[
letter <= 'a' - 1
do
letter <= letter + 1
r <= r - freq(letter)
while r > 0
letter is the resulting value
The idea behind this code is to make a stack of boxes for each letter. The size of each box is the frequency of the letter. Then we choose a random location on this stack and see which letter's box we landed.
Example :
freq(a) = 5
freq(b) = 3
freq(c) = 3
sum = 11
| a | b | c |
- - - - - - - - - - -
When we choose a 0 <= r < 11, we have the following probabilities
Pick a 'a' = 5 / 11
Pick a 'b' = 3 / 11
Pick a 'c' = 3 / 11
Which is exactly what we want.
You can first built a table of the running sum of the frequency. So if you have the following data:
%freq = (
a => 15,
b => 25,
c => 30,
d => 20
);
the running sum would be;
%running_sums = (
a => 0,
b => 15,
c => 40, # 15 + 25
d => 70, # 15 + 25 + 30
);
$max_sum = 90; # 15 + 25 + 30 + 20
To pick a single letter with the weighted frequency, you need to select a number between [0,90), then you can do a linear search on the running_sum table for the range that includes the letter. For example, if your random number is 20 then the appropriate range is 15-40, which is for the letter 'b'. Using linear search gives a total running time of O(m*n) where m is the number of letters we need and n is the size of the alphabet (therefore m=16, n=26). This is essentially what #default locale do.
Instead of linear search, you can also do a binary search on the running_sum table to get the closest number rounded down. This gives a total running time of O(m*log(n)).
For picking m letters though, there is a faster way than O(m*log(n)), perticularly if n < m. First you generate m random numbers in sorted order (which can be done without sorting in O(n)) then you do a linear matching for the ranges between the list of sorted random numbers and the list of running sums. This gives a total runtime of O(m+n). The code in its entirety running in Ideone.
use List::Util qw(shuffle);
my %freq = (...);
# list of letters in sorted order, i.e. "a", "b", "c", ..., "x", "y", "z"
# sorting is O(n*log(n)) but it can be avoided if you already have
# a list of letters you're interested in using
my #letters = sort keys %freq;
# compute the running_sums table in O(n)
my $sum = 0;
my %running_sum;
for(#letters) {
$running_sum{$_} = $sum;
$sum += $freq{$_};
}
# generate a string with letters in $freq frequency in O(m)
my $curmax = 1;
my $curletter = $#letters;
my $i = 16; # the number of letters we want to generate
my #result;
while ($i > 0) {
# $curmax generates a uniformly distributed decreasing random number in [0,1)
# see http://repository.cmu.edu/cgi/viewcontent.cgi?article=3483&context=compsci
$curmax = $curmax * (1-rand())**(1. / $i);
# scale the random number $curmax to [0,$sum)
my $num = int ($curmax * $sum);
# find the range that includes $num
while ($num < $running_sum{$letters[$curletter]}) {
$curletter--;
}
push(#result, $letters[$curletter]);
$i--;
}
# since $result is sorted, you may want to use shuffle it first
# Fisher-Yates shuffle is O(m)
print "", join('', shuffle(#result));

ThinkinSphinx query not working with sphinx_select with four conditions

I'm trying to use ThinkingSphinx to return records that have a start date within a range OR an end date within the same range, basically any record that starts or ends within this range.
To do this, I am using a computed attribute and sphinx_select as per the documentation in combination with what this post suggests for date ranges, as follows (assume there are two records, record_a starts outside the range, but ends within the range and record_b starts and ends within the range):
with_display = "*, IF(start_at >= #{range_start.to_i}, 1, 0) + " +
"IF(start_at <= #{range_end.to_i}, 1, 0) + " +
"IF(end_at >= #{range_start.to_i}, 10, 0) + " +
"IF(end_at <= #{range_end.to_i}, 10, 0) AS display"
{
sphinx_select: with_display,
with: {'display' => [2, 20, 22]},
}
=> [record_b]
However, if I only use the start_at conditions, I get one record, and if I use only the end_at conditions, it returns both records.
with_display = "*, IF(start_at >= #{range_start.to_i}, 1, 0) + " +
"IF(start_at <= #{range_end.to_i}, 1, 0) AS display"
=> [record_b]
with_display = "*, IF(end_at >= #{range_start.to_i}, 10, 0) + " +
"IF(end_at <= #{range_end.to_i}, 10, 0) AS display"
=> [record_a, record_b]
If I'm understanding this correctly, having all four conditions, should result in both record_a and record_b being returned, since record_a should have a display value of 20, while record_b should have a display value of 22.
Am I missing something?
I just realized my math was wrong, given the cases I want to handle:
record_a will have a display of 21
record_b will have a display of 22
What I needed to do was change my array to:
{
sphinx_select: with_display,
with: {'display' => [12, 21, 22]},
}
in order the handle the cases of records that end within the range (21), records that start within range (12), and records than start and end within the range (22)

Facebook interview: find out the order that gives max sum by selecting boxes with number in a ring, when the two next to it is destroyed

Didn't find any similar question about this.
This is a final round Facebook question:
You are given a ring of boxes. Each box has a non-negative number on it, can be duplicate.
Write a function/algorithm that will tell you the order at which you select the boxes, that will give you the max sum.
The catch is, if you select a box, it is taken off the ring, and so are the two boxes next to it (to the right and the left of the one you selected).
so if I have a ring of
{10 3 8 12}
If I select 12, 8 and 10 will be destroyed and you are left with 3.
The max will be selecting 8 first then 10, or 10 first then 8.
I tried re-assign the boxes their value by take its own value and then subtracts the two next to is as the cost.
So the old ring is {10 3 8 12}
the new ring is {-5, -15, -7, -6}, and I will pick the highest.
However, this definitely doesn't work if you have { 10, 19, 10, 0}, you should take the two 10s, but the algorithm will take the 19 and 0.
Help please?
It is most likely dynamic programming, but I don't know how.
The ring can be any size.
Here's some python that solves the problem:
def sublist(i,l):
if i == 0:
return l[2:-1]
elif i == len(l)-1:
return l[1:-2]
else:
return l[0:i-1] + l[i+2:]
def val(l):
if len(l) <= 3:
return max(l)
else:
return max([v+val(m) for v,m in [(l[u],sublist(u,l)) for u in range(len(l))]])
def print_indices(l):
print("Total:",val(l))
while l:
vals = [v+val(m) for v,m in [(l[u],sublist(u,l)) for u in range(len(l)) if sublist(u,l)]]
if vals:
i = vals.index(max(vals))
else:
i = l.index(max(l))
print('choice:',l[i],'index:',i,'new list:',sublist(i,l))
l = sublist(i,l)
print_indices([10,3,8,12])
print_indices([10,19,10,0])
Output:
Total: 18
choice: 10 index: 0 new list: [8]
choice: 8 index: 0 new list: []
Total: 20
choice: 10 index: 0 new list: [10]
choice: 10 index: 0 new list: []
No doubt it could be optimized a bit. The key bit is val(), which calculates the total value of a given ring. The rest is just bookkeeping.