Sort by

recency

|

2243 Discussions

|

  • + 0 comments

    Hi Guys! what do you think about this approach? focused on referencial transparency and immutability. I really wanted to avoid the so popular imperative way.

    C#

    public readonly record struct BreakingRecords(int maxScore, int minScore, int most, int least)
    {
        private BreakingRecords Init(int first)
            => new(first, first, 0, 0);
            
        private BreakingRecords Apply(int score)
            => 
                score > this.maxScore ? this with { most = most + 1, maxScore = score } : 
                score < this.minScore ? this with { least = least + 1, minScore = score } :
                this;    
        
        public string GetRecords(IReadOnlyList<int> scores)
        {                                                               
            var state = Init(scores[0]);
            var finalState = scores.Skip(1).Aggregate(state, (st, t) => st.Apply(t));
            return $"{finalState.most} {finalState.least}";
        }
    }
    
  • + 0 comments

    Java

    	int min = scores.get(0);
            int max = scores.get(0);
            
            int minRecord = 0;
            int maxRecord = 0;
            
            for (int i = 1; i < scores.size(); i++) {
                if (scores.get(i) < min) {
                    min = scores.get(i);
                    minRecord++;
                } else if (scores.get(i) > max) {
                    max = scores.get(i);
                    maxRecord++;
                }
                
            }
                
            return Arrays.asList(maxRecord, minRecord);
    
  • + 0 comments

    Java Solution

    public static List<Integer> breakingRecords(List<Integer> scores) {
    
        int max=scores.get(0),min=scores.get(0),minCount=0,maxCount=0;
        for(int x : scores){
            if(x>max){
                max=x;
                maxCount++;
            }
            if(x<min){
                min=x;
                minCount++;
            }
    
        }   
         List<Integer> resultList = new ArrayList<>();
            resultList.add(maxCount);
            resultList.add(minCount);   
         return resultList;
    
    }
    
  • + 0 comments

    javascript easy solution

    let max=scores[0]

    let count=0
    
    for(let i = 0;i <scores.length;i++)
    {
        if(scores[i] > max){
            max=scores[i]
            count++
        }
    }
    let min=scores[0]
    
    let count1=0
    
    for(let i = 0;i < scores.length;i++){
        if(scores[i] < min){
            min = scores[i]
            count1++
        }
    }
    
    let arr=[]
    arr.push(count)
    arr.push(count1)
    return arr
    

    }
    arr.push(count1) return arr }

  • + 0 comments

    def breakingRecords(scores): # import numpy as np high, low, h_count, l_count = scores[0], scores[0], 0,0

    for i in scores:
        if i < low:
            l_count+=1
            low = i
        elif i > high:
            h_count+=1
            high= i
        else:
            continue
    
    arr = []
    arr.append(h_count)
    arr.append(l_count)
    
    return arr