| The following warnings occurred: | |||||||||||||||
Warning [2] Undefined property: MyLanguage::$archive_pages - Line: 2 - File: printthread.php(287) : eval()'d code PHP 8.2.30 (Linux)
|
![]() |
|
Brain tickler if you're bored - Printable Version +- Madison Motorsports (https://forum.mmsports.org) +-- Forum: Madison Motorsports (https://forum.mmsports.org/forumdisplay.php?fid=3) +--- Forum: Lounge (https://forum.mmsports.org/forumdisplay.php?fid=6) +--- Thread: Brain tickler if you're bored (/showthread.php?tid=5601) Pages:
1
2
|
Brain tickler if you're bored - JackoliciousLegs - 03-14-2007 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.) - HAULN-SS - 03-14-2007 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 - HAULN-SS - 03-14-2007 i just refreshed, and I dont know why that stuff is bolded now? am i missing somethig? - JackoliciousLegs - 03-14-2007 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. - HAULN-SS - 03-14-2007 i left out some parens - HAULN-SS - 03-14-2007 hrmm..oh, i see now - you want any page, not the last page - JackoliciousLegs - 03-14-2007 12 - (5 *( 1 - 1)) = 12 not 2 F :lol: - Apoc - 03-14-2007 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) - JackoliciousLegs - 03-14-2007 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. - white_2kgt - 03-14-2007 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. That's a ternary operator num nuts. - Apoc - 03-14-2007 Your mom! I missed the ternary operator part... yes, reading is fundamental. P.S. - It's numbnuts. - HAULN-SS - 03-14-2007 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 - Apoc - 03-14-2007 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. - white_2kgt - 03-14-2007 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. - JackoliciousLegs - 03-14-2007 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 changedMy 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? - white_2kgt - 03-14-2007 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 ) - HAULN-SS - 03-14-2007 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 - JackoliciousLegs - 03-14-2007 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,you are obviously not using the MVC pattern. My view doesn't have access to the result set. - HAULN-SS - 03-14-2007 Can you implement a design pattern on one line? - white_2kgt - 03-14-2007 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,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? |