Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Leah Culver, well rounded programmer.

Name: Anonymous 2009-11-29 8:37

Name: TRUE TRUTH EXPERT !!TthtFzrtPXElUy7 2009-11-29 8:39

tHREAD SUCKS.

Name: Anonymous 2009-11-29 8:41

/prog/

Name: Anonymous 2009-11-29 8:56

In case she learns about robots.txt:


Star Ratings

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 leave a comment if you’ve got a better solution.

Update (hooray!)
I’ve updated my solution to:
stars = round(star_sum*2/num_raters)/2

I knew there was a way to do this mathematically, I just couldn’t remember how. I suppose the same trick would work for rounding to thirds, fourths, etc. Thanks so much to r, Christopher Finke, and Bob for the better approach.


_____



1. r Says:
April 19th, 2007 at 2:11 pm

might be misunderstanding what you’re trying to do, but

formatting an average value to the nearest half (forgive the lack of python syntax):

round(AvgValue * 2) / 2

should work

---

2. Christopher Finke Says:
April 19th, 2007 at 2:58 pm

If you’re willing to change your boundaries to .75 and .25 (rather than .7 and .3), you could do this:

stars = float(round(star_sum / num_raters * 2) / 2);

---

3. J Taylor Says:
April 19th, 2007 at 5:55 pm

#I don’t program in Python and I’m not sure if this code has the correct syntax
#Calculate rating out of 10
rating = round(star_sum*2/num_raters, 1)
#Calculate number of whole stars by integer division
whole_stars = rating // 2
#Calculate number of half stars by remainder
half_stars = rating % 2
#Combine into one number
stars = whole_stars + (half_stars * 0.5)

---

4. Bob Ippolito Says:
April 19th, 2007 at 6:02 pm

In general you should never have to convert a number to a string. If you needed to get the fractional and whole parts of a float there’s math.modf(someFloat) which returns (frac, whole). Anyway, I would probably just double the sum, round, then half it.

stars = 0.5 * round((2.0 * star_sum) / num_raters)

---

5. Alexander Repty Says:
April 20th, 2007 at 12:13 am

I gave this to some of my co-workers (system administrators and software developers) as a challenge. Only 2 out of 7 could get it right (both software developers - one of them an apprentice).

I immediately knew the solution when I finished reading the problem at hand, and I would have done it exactly like Bob did. I guess it’s just the experience in solving problems like this one that enables you to see this solution.

---

6. Richard Kiss Says:
April 20th, 2007 at 10:11 am

Here’s an idea:

stars = float(str(round(float(star_sum)/num_raters,1)).split(’.')[0]) + {’0′:0, ‘1′:0, ‘2′:0, ‘3′:0.5, ‘4′:0.5, ‘5′:0.5, ‘6′:0.5, ‘7′:0.5, ‘8′:1.0,’9′:1.0}[str(round(float(star_sum)/num_raters,1)).split(’.')[1]]

or more concisely,

stars = round(float(star_sum+star_sum)/num_raters)/2

---

7. leah.culver Says:
April 20th, 2007 at 10:52 am

Alexander - seems to me like something people would remember if they had used it before. I’d never had to round like that before and I kept thinking that there was some way to do it mathematically, but I couldn’t remember how. I hope it’s not as important to know the best solution as it is to know a better solution exists.

Richard and Christopher - I suppose I was being too generous with .7. I don’t mind the rounding difference and I doubt the cheeseburgers will either.

Everybody - Thanks so much for the help! I suppose I could have looked around more for the solution but now I feel like my blog was actually useful.

---

8. Sam Purtill Says:
April 21st, 2007 at 1:48 pm

Hey Leah… Why Django over Ruby on Rails? I picked Django too, and we all have our reasons. Write a post about it!!!

---

9. Tom Says:
April 23rd, 2007 at 8:07 am

A perfect example of why everyone’s first course in programming should be Fortran.

Name: Anonymous 2009-11-29 9:03

>People can rate cheeseburgers on my website
Web 2.0 genius

Name: Anonymous 2009-11-29 9:04

>>2

Name: Anonymous 2009-11-29 9:15

>>6
Go back to bed, Leah.

Name: Anonymous 2009-11-29 9:28

This is really depressing, however to be honest I was expecting 11 different cases, each with individual lower and upper bounds.

Name: Anonymous 2009-11-29 9:29

We've already seen this a couple of times already.

Name: Anonymous 2009-11-29 9:49

>>1
I dont get it xD

Name: Leah Culver !1LEahRIBg. 2009-11-29 11:12

It's ok, leah, I still love you

Name: Anonymous 2009-11-29 12:20

WEB PROGRAMMERS

Name: Anonymous 2009-11-29 12:22

>>12
web
programmers
this->lol();

Name: Anonymous 2009-11-29 12:27

It's a shame, you would think that a guy like Eric Lippert (http://blogs.msdn.com/ericlippert/) could find himself a more computer literate girlfriend.

Name: Anonymous 2009-11-29 12:27

The wheels of the short bus go round and round!

Name: Anonymous 2009-11-29 12:29

WHY DO HASKELL FUNCTIONS LOOK LIKE GENITALS

see .|. or .&.

Name: Anonymous 2009-11-29 12:34

>>14
I don't think that the Labia is married, and Eric claims he has a wife. The only logical conclusion is that you are talking about someone else, you silly off-topicer.

Name: Anonymous 2009-11-29 12:36

>>16
U MENA PASCAL[1]

References                      
1. :=

Name: Anonymous 2009-11-29 12:54

>>16
In what twisted universe does .&. resemble any genital?

Name: Anonymous 2009-11-29 13:00

>>19
Obvious virgin.

Name: HMA FAN 2009-11-29 13:10

4chan GENERAL BROADCAST SYSTEM
New announcement - Area of application: /prog/
From now on it follows that `` .&. '' shall be known as the official haxed anus operator. ``Twisted universe genital'' shall be accepted as a valid synonym.

Thank you for your attention - HMA committee

Name: Anonymous 2009-11-29 13:14

>>21
Can we get a small tutorial on it's use? ty

Name: Anonymous 2009-11-29 13:29

>>19
TWIST MY ANUS

Name: Anonymous 2009-11-29 13:36

Also this _|_

Seriously haskell what the fuck

Name: Anonymous 2009-11-29 13:37

>>17
Go search for the word ,,Leah`` in his blog, moron.

Name: Anonymous 2009-11-29 13:54

>>25
I did. In the about page and in several entries he mentions a Leah. I saw no mention of "Culver", which is the only Leah of relevance to this thread. The back story of his wife and the Labia do not match up either: he claims his wife is a software tester at Microsoft, whereas the culver claims to work at Six Apart; his girlfriend drives a Honda Fit, whereas the Culver drives a '91 VW Jetta.

Name: Anonymous 2009-11-29 13:55

>>26
I realise I swapped between using "wife" and "girlfriend", though his about page specifies wife.

Name: Anonymous 2009-11-29 14:04

If you're cute and rich and a programmer there's a good chance you fucked Leah Culver.

I'd never marry such a woman.

Name: Anonymous 2009-11-29 14:07

>>28
Hi, Eric Lippert

Name: Anonymous 2009-11-29 14:16

>>28
Do you prefer men?

Name: Anonymous 2009-11-29 14:33

>>30
No, I prefer non-promiscuous women.

Name: Anonymous 2009-11-29 14:39

>>30
I prefer 2D women.

Name: Anonymous 2009-11-29 14:44

fmap ({;}) [(.|.), (./.), (.\.)]

Name: Anonymous 2009-11-29 14:44

>>30
I prefer amorphous beings of pure energy

Name: Anonymous 2009-11-29 15:11

Leah's Epsilon-0.30 Clamping Star Algorithm

Epsilon represents the range from the nearest integer than a real must fall within in order to be rounded to that integer, or else the number is rounded to the nearest half.  Implemented with strings in order to increase portability across systems with incorrectly implemented python math libraries, revealing a strong grasp for real-world applications.  That's something you don't see much in Computer Scientists.

A true rebel.  A thinker.  A Web 2.0 evangelist and missionary.

Name: Anonymous 2009-11-29 15:12

>>24
That's the universal ascii representation for the “bottom” symbol, it's not by any means exclusive to Haskell.

Name: Anonymous 2009-11-29 15:13

>>35
I chortled.

Name: Anonymous 2009-11-29 15:18

>>34
ENERGIZE MY ANUS

Name: Anonymous 2009-11-29 15:20

>>35
Oh I bet she likes that missionary position!

Name: Anonymous 2009-11-29 16:48

Doesn't Xarn periodically chat Leah Culver up on Twitter?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List