Sort by

recency

|

2103 Discussions

|

  • + 0 comments

    The AI bros doing L33tCode are heavy now. These 1 liners are ridiculous and help no person. But below hopefully is the logic illustrated by some of the ChatGPT coders.

    public static int pageCount(int n, int p)
        {
            if (p > n)
                return -1; // early exit, can't be done
            
            
            var evenPages = n % 2 == 0;
            
            // p == begin of book, end of book, or left page end of book if odd pages
            if (p == 1 || n == p || (!evenPages && p == n - 1))
                return 0;
            else if (evenPages && p == n - 1)
                return 1;
            
            var fromLeftTurns = (int)Math.Floor((decimal)p / 2);
            var fromRightTurns = (int)Math.Floor((decimal)(n - p) / 2);
            
            return fromLeftTurns <= fromRightTurns ? fromLeftTurns : fromRightTurns;
        }
    
  • + 0 comments

    My logical brain took over and I didn't even second thought that it can be easily done with std::min(p / 2, n / 2 - p / 2);

    int pageCount(int numOfPages, int pageNum) {
        int count1 = 0;
        int count2 = 0;
        if (numOfPages == pageNum || pageNum == 1){
            return 0;
        }
        for (int i = 2; i <= numOfPages; i+=2){
            count1++;
            if (i == pageNum || i+1 == pageNum){
                break;
            }
        }
        if (numOfPages%2 != 0){
            count2--;
            for (int i = numOfPages; i >= 2; i-=2){
                count2++;
                if (i == pageNum || i-1 == pageNum){
                    break;
                }
            }
        } else {
            for (int i = numOfPages-1; i >= 2; i-=2){
                count2++;
                if (i == pageNum || i-1 == pageNum){
                    break;
                }
            }
        }
        return (count1 > count2) ? count2 : count1;
    }
    
  • + 0 comments

    I was overthinking this problem at first, but once I broke it down it made sense—it’s really just about finding the minimum turns from either side of the book. My Python version looks like this:

    def pageCount(n, p): return min(p // 2, (n // 2) - (p // 2))

    Clean and efficient—sometimes the simplest logic is the most powerful! this delta key helps alot

  • + 0 comments
    # Drawing Book 📖
    def page_count(n, p):
        s = p // 2
        e = n // 2 - p // 2
        return min(s, e)
    
  • + 0 comments

    I initially overcomplicated it by trying to simulate the entire book, but then I realized it's just about comparing the number of page turns from the front and back. Here's a simple Python solution: python

    def pageCount(n, p): return min(p // 2, (n // 2 - p // 2))

    Interestingly, this concept is also applied in healthcare systems. For instance, revenue cycle management services help streamline processes by efficiently handling tasks, much like minimizing unnecessary steps in a problem-solving approach. You can learn more about this here: https://swiftmds.com/services/healthcare-revenue-cycle-management-services/.

    Hope this helps!