Interesting Math Problem

B

BEES INC

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.
 
K

Ken Starks

BEES said:
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.

for raw in [0.05 * n for n in range (41)]:
rounded = round(2.0*raw)/2.0
print "%0.2f --> %0.2f" % (raw,rounded)
 
I

Iain King

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.

It'd be easier just to do the whole thing with ints. Represents your
stars by counting half-stars (i.e. 0 = no stars, 1 = half a star, 2 =
1 star, etc). Then you just need to divide by 2 at the end.

stars = round(star_sum/num_raters, 0) / 2.0

Iain
 
M

MRAB

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).
[snip]
I hope you really meant that 'star_sum' was the _sum_ and not the
_average_, otherwise the result would definitely be wrong! :)
 
R

Rüdiger Werner

....

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):


Well seems this is a typical half even rounding problem.

See

http://mail.python.org/pipermail/python-list/2008-April/485889.html

for a long discussion

However since your problem looks like a typical homework problem i made my
example
buggy by intention.

However the result should be correct.

have fun!

def myround(x):
d, m = divmod(x, 2)
return 2*d + round(m - 1) + 1

num = 1.5
for _i in xrange(30):
print num, ' -> ', myround(num * 2)/2
num -= 0.1
pythonw -u "test18.py"
1.5 -> 1.5
1.4 -> 1.5
1.3 -> 1.5
1.2 -> 1.0
1.1 -> 1.0
1.0 -> 1.0
0.9 -> 1.0
0.8 -> 1.0
0.7 -> 0.5
0.6 -> 0.5
0.5 -> 0.5
0.4 -> 0.5
0.3 -> 0.5
0.2 -> 0.0
0.1 -> 0.0
-1.94289029309e-016 -> 0.0 < --- hint for bug
-0.1 -> 0.0
-0.2 -> 0.0
-0.3 -> -0.5
-0.4 -> -0.5
-0.5 -> -0.5
-0.6 -> -0.5
-0.7 -> -0.5
-0.8 -> -1.0
-0.9 -> -1.0
-1.0 -> -1.0
-1.1 -> -1.0
-1.2 -> -1.0
-1.3 -> -1.5
-1.4 -> -1.5
Exit code: 0












to avoid the bug have a look at:

http://mail.python.org/pipermail/python-list/2008-April/485934.html

and try this example:


num = 3
for _i in xrange(num * 10,-num *10, -1):
if myround(float(_i)/10) != myround(num):
print num, " -> ", myround(num ), " --- ", myround(float(_i)/10)
print "-----"
num -= 0.1
 
L

Lie

...

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):

Well seems this is a typical half even rounding problem.

See

http://mail.python.org/pipermail/python-list/2008-April/485889.html

for a long discussion

However since your problem looks like a typical homework problem i made my
example
buggy by intention.

However the result should be correct.

have fun!

def myround(x):
    d, m = divmod(x, 2)
    return 2*d + round(m - 1) + 1

num = 1.5
for _i in xrange(30):
    print num, ' -> ', myround(num * 2)/2
    num -= 0.1


1.5  ->  1.5
1.4  ->  1.5
1.3  ->  1.5
1.2  ->  1.0
1.1  ->  1.0
1.0  ->  1.0
0.9  ->  1.0
0.8  ->  1.0
0.7  ->  0.5
0.6  ->  0.5
0.5  ->  0.5
0.4  ->  0.5
0.3  ->  0.5
0.2  ->  0.0
0.1  ->  0.0
-1.94289029309e-016  ->  0.0    < --- hint for bug
-0.1  ->  0.0
-0.2  ->  0.0
-0.3  ->  -0.5
-0.4  ->  -0.5
-0.5  ->  -0.5
-0.6  ->  -0.5
-0.7  ->  -0.5
-0.8  ->  -1.0
-0.9  ->  -1.0
-1.0  ->  -1.0
-1.1  ->  -1.0
-1.2  ->  -1.0
-1.3  ->  -1.5
-1.4  ->  -1.5


to avoid the bug have a look at:

http://mail.python.org/pipermail/python-list/2008-April/485934.html

and try this example:

num = 3
for _i in xrange(num * 10,-num *10, -1):
    if myround(float(_i)/10) != myround(num):
        print num, " -> ", myround(num ), " --- ",  myround(float(_i)/10)
        print "-----"
    num -= 0.1

Well, I don't think that's the problem. I don't think the OP cares
what the rounding method is, it doesn't really matters that much in
this case since this function is only used for output and the
underlying rating value is untouched.

In that case, try this:

from __future__ import division

rating = round(star_sum / num_raters)
whole, half = divmod(rating, 2)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top