Sort by

recency

|

3079 Discussions

|

  • + 0 comments
    func countApplesAndOranges(s: Int, t: Int, a: Int, b: Int, apples: [Int], oranges: [Int]) -> Void {
        // Write your code here
        print(apples.filter { s...t ~= ($0 + a)}.count)
        print(oranges.filter { s...t ~= ($0 + b)}.count)    
    }
    

    Swifty way of doing better things.

  • + 0 comments
    func countApplesAndOranges(s: Int, t: Int, a: Int, b: Int, apples: [Int], oranges: [Int]) -> Void {
        // Write your code here
        print(apples.filter { s...t ~= ($0 + a)}.count)
        print(oranges.filter { s...t ~= ($0 + b)}.count)    
    }
    
  • + 0 comments
    def countApplesAndOranges(s, t, a, b, apples, oranges):
        # Write your code here
        app = []
        for i in range(len(apples)):
            dist = a + apples[i]
            if s <= dist <= t:
                app.append(apples[i])
        print(len(app))
        ora = []
        for i in range(len(oranges)):
            dist = b + oranges[i]
            if s <= dist <= t:
                ora.append(oranges[i])
        print(len(ora))
    
  • + 0 comments
     countA=0
        countO=0
        for i in apples :
            x=i+a
            if x>=s and x<=t:
                countA+=1
        for i in oranges:
            x=i+b
            if x>=s and x<=t:
                countO+=1
        print(countA)
        print(countO)
    
  • + 0 comments

    Code in C++

    void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges) {
        int c1=0,c2=0;
    vector<int>n1;vector<int>n2;
    for(int i=0;i<apples.size();i++){
        n1.push_back(a+apples[i]);
    }
    for(int i=0;i<oranges.size();i++){
        n2.push_back(b+oranges[i]);
    }
    for(int i=0;i<n1.size();i++){
        if(n1[i]>=s && n1[i]<=t){
            c1++;
        }
    }
    for(int i=0;i<n2.size();i++){
        if(n2[i]>=s && n2[i]<=t){
            c2++;
        }
    }
    cout<<c1<<endl;
    cout<<c2<<endl;
    }