I'm wondering why the red countries produce such skilled hackers.
Maybe the difference be their strict and result-oriented schooling system. Or their lawlessness and disregard for copyrights. Maybe it's because they are bred with older and more hackable computer systems instead of the Fisher Price iPhones fat Americans get to play with.
Could these Russian, Czech and Chinese hackers be driven by the spirit of communism itself. Did our comrade Trotskij miss out on a glorious computing career?
I've been awfully busy programming lately. My Django-based side project is coming along well and I hope to have it ready for use in a few weeks. Please don't ask more about it, that's really all I can say for now. Anyways, I came across an interesting little math problem today and was hoping some skilled programmers out there could come up with a more elegant solution than mine.
Problem: Star Ratings
People can rate cheeseburgers on my website with a star rating of 0-5 stars (whole stars only), 5 being mighty tasty and 0 being disgusting. I would like to show the average of everyone's ratings of a particular cheeseburger to the nearest half star. I have already calculated the average rating as a float (star_sum) and the total number of people that rated the particular cheeseburger (num_raters). The result should be stored as a float in a variable named "stars."
My Solution (in Python):
# round to one decimal place and
# separate into whole and fractional parts
parts = str(round(star_sum/num_raters, 1)).split('.')
whole = int(parts[0])
frac = int(parts[1])
if frac < 3:
___frac = 0
elif frac > 7:
___frac = 0
___whole += 1
else:
___frac = 5
# recombine for a star rating rounded to the half
stars = float(str(whole)+'.'+str(frac))
Mmmm… In-N-Out Burgers… Please reply if you've got a better solution.
Name:
Anonymous2010-02-07 11:38
That seems to use a lot of unnecessary variables and work just to round a number.
THIS IS NOW A post your own rounding algorithm THREAD
Here's mine, written in Sessless (also known as C):
double round (double number)
{
float test = (int)number;
if (number < (test + .3))
return test;
else if (number > (test + .7))
return test + 1.0;
else
return test + 0.5;
}
A program to test it:
#include <stdio.h>
int main ()
{
double derp[] = {1.2, 1.4, 1.6, 1.8};
int x;
for (x = 0; x < 4; x++)
{
printf ("%1.1f rounds to %1.1f\n", derp[x], round (derp[x]));
}
>>55
It rounds in binary, so that's to be expected.
Name:
Anonymous2010-02-07 18:01
>>58
I see. So >>51 is just a general rounding algorithm. Not one to solve the Culver's stars problem.
Name:
Anonymous2010-02-07 18:05
>>55
It's meant to. Did you never take basic mathematics? Using 0.3 and 0.7 for rounding limits as previous posts did is idiotic - for instance, 1.29 is clearly closer to 1.5 than it is to 1.0.