Sort by

recency

|

950 Discussions

|

  • + 0 comments

    Too many variables? Maybe. Do I care? Absolutely not!

    int workbook(int n, int k, vector<int> arr) {
        int problems = 0;
        int bookMark = 1;
        
        for(int i = 0; i<arr.size(); i++){
            int pages = (arr[i] + k - 1) / k;
            int firs_exe = 1;
            int last_exe = min(firs_exe + k - 1, arr[i]);
            
            while (pages-->0) {
                
                if(firs_exe <= bookMark && bookMark <= last_exe) problems++;    
                
                firs_exe = last_exe + 1;
                last_exe = min(firs_exe + k - 1, arr[i]);
                bookMark++;
            }
        }
        
        return problems;
    }
    
  • + 0 comments

    JAVA

    public static int workbook(int n, int k, List arr)

    {
    int page = 1;
    int count = 0;
    for(int i=0;i<n;i++){
        int noq = arr.get(i);
        int j =1;
        while(j<=noq){
            int ques = j;
            int qcount = 1;
            while(qcount<=k){
                if(ques==page){
                    count++;
                }
                ques++;
                if(ques>noq){
                    break;
                }   
                qcount++;             
            }
            page++;
            j+=k;
        }
        }
        return count;
    }
    
  • + 0 comments
    def workbook(n, k, arr):
        # Write your code here
        
        current_page_no = 0
        special_problem_count = 0
        for chapter in range(1, n+1):
            pages = math.ceil(arr[chapter-1] / k)
            current_problems = 0
            while pages > 0:
                current_page_no += 1
                problems_on_current_page = k if (arr[chapter-1] - current_problems) > k else arr[chapter-1] - current_problems
                if current_page_no > current_problems and current_page_no <= (current_problems+problems_on_current_page):
                    special_problem_count += 1
                current_problems += problems_on_current_page
                pages -= 1            
        return special_problem_count
    
  • + 0 comments

    Python: Semi-short looping solution…

    def workbook(_, k, arr):
      p = s = 0
      for i in arr:
        for j in range(0, i, k):
          p, s = ((p + 1, s + 1)
            if j < p + 1 <= min(i, j + k)
            else (p + 1, s)) 
      return s
    

    Here's a semi-short, non-looping functional solution…

    def workbook(_, k, arr):
        pg = lambda m: m[0]
        prob1 = lambda m: m[1][0]
        probN = lambda m: m[1][1]
        return sum(map(
            lambda pgProbs: int(prob1(pgProbs) < pg(pgProbs) <= probN(pgProbs)), 
            enumerate(sum(map(
                lambda chNumProb: list(map(
                    lambda pgProb1: (pgProb1, min(chNumProb, pgProb1 + k)), 
                    range(0, chNumProb, k))), 
                arr), []), start=1))) 
    

    It could be shorter, but I've seen people complain about the readability of functional code. So, I tried to make this descriptive. I used verbose names for lambda arguments. I also added lambdas for accessing the members of tuples returned from the inner map levels.

    Here's a terse version of the same code…

    def workbook(_, k, arr):
        return sum(map(lambda m: int(m[1][0] < m[0] <= m[1][1]),
                enumerate(sum(map(lambda i:
                    list(map(lambda j: (j, min(i, j + k)), 
                    range(0, i, k))), 
                arr), []), 1))) 
    
  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-lisas-workbook-problem-solution.html