473,289 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Interesting Math Problem

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.
Jun 27 '08 #1
5 1285
BEES INC wrote:
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)
Jun 27 '08 #2
On Jun 4, 9:03 am, "BEES INC" <bees....@gmail.comwrote:
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
Jun 27 '08 #3
On Jun 4, 9:03 am, "BEES INC" <bees....@gmail.comwrote:
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! :-)
Jun 27 '08 #4

"BEES INC" <be******@gmail.comschrieb im Newsbeitrag
news:ma*************************************@pytho n.org...
....

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/pyt...il/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/pyt...il/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
Jun 27 '08 #5
Lie
On Jun 6, 2:25*am, "Rüdiger Werner" <larud...@freenet.dewrote:
"BEES INC" <bees....@gmail.comschrieb im Newsbeitragnews:ma******************************** *****@python.org...
...

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/pyt...il/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/pyt...il/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)
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Reiner Apke | last post by:
Hello, I have got a very strange problem with the calcualtion of the the square root (Math.Sqrt()). I calculate in a loop a lot of of diameters maxDiameter = Math.Sqrt(maxCrossSection *...
5
by: Ark | last post by:
Hi everyone, Does anyone know if Direct3D overloads System.Math functions? Also is it possible to access the base functions of the overloaded function (in other words restore original of the...
11
by: Sambo | last post by:
I have the following module: ------------------------------- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j )
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
40
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
1
by: Ivan Illarionov | last post by:
On Mar 19, 2:17 pm, "BJörn Lindqvist" <bjou...@gmail.comwrote: I really want to revive this discussion. Arnaud's approach is definetly cool, but it turns out that in real-world situations it...
0
by: Gary Herron | last post by:
BEES INC wrote: Much simpler this way. This produces the number of whole start and the number of half stars: v = ... calculate the average ... whole = int(v+0.25) half =...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.