Sort by

recency

|

3091 Discussions

|

  • + 0 comments

    func countApplesAndOranges(s: Int, t: Int, a: Int, b: Int, apples: [Int], oranges: [Int]) -> Void { let appleNewPos = apples.map { val in return val+a }.filter { val in val>=s && val<=t } let orangeNewPos = oranges.map { val in return val+b }.filter { val in val>=s && val<=t } print("(appleNewPos.count)") print("(orangeNewPos.count)") }

  • + 0 comments
    void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges) {
        // get the coordinates of the fallen fruits and check whether they are in [s, t]
        std::transform(apples.begin(), apples.end(), apples.begin(),
        [a,s,t] (int d) {
            return (a + d >= s && a + d <= t) ? 1 : 0;     
        });
        std::transform(oranges.begin(), oranges.end(), oranges.begin(),
        [b,s,t] (int d) {
           return (b + d >= s && b + d <= t) ? 1 : 0; 
        });
        
        // then just sum up the elements and print the result
        std::cout << std::accumulate(apples.begin(), apples.end(), 0) << std::endl;
        std::cout << std::accumulate(oranges.begin(), oranges.end(), 0) << std::endl;
    }
    
  • + 0 comments

    public static void countApplesAndOranges(int s, int t, int a, int b, List apples, List oranges) { // Write your code here long ab = apples.stream().map(n->n+a).filter(n->(n>=s && n<=t)).count(); System.out.println(ab); long bc = oranges.stream().map(n->n+b).filter(n->(n>=s && n<=t)).count(); System.out.println(bc); }

  • + 0 comments

    Ruby

    def countApplesAndOranges(s, t, a, b, apples, oranges)
      land_on_house = lambda { |value| value.between?(s, t) }
    
      puts apples.count {|apple| land_on_house.(apple + a)}
      puts oranges.count {|orange| land_on_house.(orange + b)}
    end
    
  • + 0 comments

    My JS solution:

    function countApplesAndOranges(s, t, a, b, apples, oranges) {
        console.log(
            apples
                .map(apple => apple + a)
                .filter(apple => apple >= s && t >= apple).length
            );
        console.log(
            oranges
                .map(orange => b + orange)
                .filter(orange => orange <= t && orange >= s).length
            );
    }