Brain tickler if you're bored
#1
So, I'm doing something for work where I need to do some Math/logic. It seems like a question I'd get on a CS exam. If you feel like having a crack at it, dooooo it!

So, I've got a result set and it won't all fit on one page. So, for example, I've got 12 results and I can fit 5 / page. I know only two things: the number of results and the max per page. How can I figure out how many are on a certain page given only the page number?

When you post a solution, use clear variable names like "cur_page" and "num_results", etc... You are allowed to use programming functions like ceil() and floor(). Do it as efficiently as possible. (A friend and I have figured it out 2 ways to get it on one line. Neither are particularly pretty. One uses a ternary operator, other looks like complex math equation. There has GOT to be a good way to do it. i.e. no ternary operator and in one statement.)
My Tumblr
2008 Felt F75 - Pedal Power
  Reply
#2
ternary operators are pretty efficient, i dont know why you wouldnt want to use that...was the first thing i thought of before i read that you had already come up with that.

num_on_page_X = num_results - (max_per_page *( current_page_X - 1))

?

so this would = 12 - (5 * 2) = 2
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#3
i just refreshed, and I dont know why that stuff is bolded now? am i missing somethig?
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#4
so assuming page 1 of the 12 results at 5/page, we've got
12 - ((5 * 1) - 1) or it's unclear how you want this done ... 12 - (5 * (1 - 1))
Either way, it's 8 or 12. Both of which aren't right.
EDIT: Figured I'd bold the question and the expectation.
My Tumblr
2008 Felt F75 - Pedal Power
  Reply
#5
i left out some parens
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#6
hrmm..oh, i see now - you want any page, not the last page
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#7
12 - (5 *( 1 - 1)) = 12 not 2
F :lol:
My Tumblr
2008 Felt F75 - Pedal Power
  Reply
#8
Here's how I'd do it but I an Excel person who's used to If statements so that may color my results.

If
num_results > curr_page * res_per_page
then
res_per_age
else
num_results - ( (curr_page - 1) * res_per_page)
'76 911S | '14 328xi | '17 GTI | In memoriam: '08 848, '85 944

"Here, at last, is the cure for texting while driving. The millions of deaths which occur every year due to the iPhone’s ability to stream the Kim K/Ray-J video in 4G could all be avoided, every last one of them, if the government issued everyone a Seventies 911 and made sure they always left the house five minutes later than they’d wanted to. It would help if it could be made to rain as well. Full attention on the road. Guaranteed." -Jack Baruth
  Reply
#9
Sorry, should've been more clear for the non-nerd folks. Ternary operators are inline if statements. I'm trying to see if there's a way to do it without using conditional logic.
My Tumblr
2008 Felt F75 - Pedal Power
  Reply
#10
Apoc Wrote:Here's how I'd do it but I an Excel person who's used to If statements so that may color my results.

If num_results > curr_page * res_per_page then res_per_age else num_results - ( (curr_page - 1) * res_per_page)

That's a ternary operator num nuts.
  Reply
#11
Your mom! I missed the ternary operator part... yes, reading is fundamental.

P.S. - It's numbnuts.
'76 911S | '14 328xi | '17 GTI | In memoriam: '08 848, '85 944

"Here, at last, is the cure for texting while driving. The millions of deaths which occur every year due to the iPhone’s ability to stream the Kim K/Ray-J video in 4G could all be avoided, every last one of them, if the government issued everyone a Seventies 911 and made sure they always left the house five minutes later than they’d wanted to. It would help if it could be made to rain as well. Full attention on the road. Guaranteed." -Jack Baruth
  Reply
#12
How well tested is your "mathy" algorithm? It's seeming like this might be complex, and I'm wondering if you actually got something generic, in case your max/page or your max results changed
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#13
Sooo... what's wrong with a ternary operator anyway? I've been looking at this for the last 20 minutes including roundups and absolute values and still can't get it.

The hitch, which I'm sure you know, is because page 1 and 2 need to be treated the same but different than 3. I can't figure out a way to do this without an IF statement because the relation to each other are the same.
'76 911S | '14 328xi | '17 GTI | In memoriam: '08 848, '85 944

"Here, at last, is the cure for texting while driving. The millions of deaths which occur every year due to the iPhone’s ability to stream the Kim K/Ray-J video in 4G could all be avoided, every last one of them, if the government issued everyone a Seventies 911 and made sure they always left the house five minutes later than they’d wanted to. It would help if it could be made to rain as well. Full attention on the road. Guaranteed." -Jack Baruth
  Reply
#14
Apoc Wrote:Sooo... what's wrong with a ternary operator anyway?

Nothing. It's just mental mastburation.

Apoc Wrote:The hitch, which I'm sure you know, is because page 1 and 2 need to be treated the same but different than 3. I can't figure out a way to do this without an IF statement because the relation to each other are the same.

I'm having the same problem, mods, nothing. 10 minutes, I give up, out comes the IF.
  Reply
#15
HAULN-SS Wrote:How well tested is your "mathy" algorithm? It's seeming like this might be complex, and I'm wondering if you actually got something generic, in case your max/page or your max results changed
My mathy algorithm works. It's one line, but it's long :-)

Apoc Wrote:Sooo... what's wrong with a ternary operator anyway? I've been looking at this for the last 20 minutes including roundups and absolute values and still can't get it.
Doesn't it seem like it should be easily done without logic?
My Tumblr
2008 Felt F75 - Pedal Power
  Reply
#16
funny thing is, I went back and looked at some code that I wrote that does this, and I do it all on 1 line w/ no ternary operator, here it is,

len( $results )
  Reply
#17
I keep wanting to shove all this stuff into a group so I can redefine the + operation and be able to say things like the inverse of 2 is 7, but that would clearly not be a "simpler" way of doing things, heh. I give up, use a ternary and just use the calculation for the last page
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#18
white_2kgt Wrote:funny thing is, I went back and looked at some code that I wrote that does this, and I do it all on 1 line w/ no ternary operator, here it is,

len( $results )
you are obviously not using the MVC pattern. My view doesn't have access to the result set.
My Tumblr
2008 Felt F75 - Pedal Power
  Reply
#19
Can you implement a design pattern on one line?
2013 Cadillac ATS....¶▅c●▄███████||▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅||█~ ::~ :~ :►
2008 Chevy Malibu LT....▄██ ▲  █ █ ██▅▄▃▂
1986 Monte Carlo SS. ...███▲▲ █ █ ███████
1999 F250 SuperDuty...███████████████████►
1971 Monte Carlo SC ...◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙☼◤
  Reply
#20
JackoliciousLegs Wrote:
white_2kgt Wrote:funny thing is, I went back and looked at some code that I wrote that does this, and I do it all on 1 line w/ no ternary operator, here it is,

len( $results )
you are obviously not using the MVC pattern. My view doesn't have access to the result set.

sure I am, and I'm doing a better job at it than YOU. You shouldn't be computing this stuff in your View, compute everything you need in the controller and give it to your view to display.

What are you using anyway?
  Reply


Forum Jump: