Pure Storage | OA 2019 | Number Score V1

Thanks @waste049 for pointing out the missed test case. This solution passes all cases. There is also a newer version being asked which is linked below!

Newer version here
All questions here

Find the score for each number, you got points for:
+1 for number divisible by 7
+2 for even digits
+4 for each 9
+5 for 2 consecutive 1 and +5 for each consecutive followine one
+N2 for every continous sequnce of the form 123 or 4567 of length N

e.g. If the number is 11129 you get:

  • 5+5 points for conseutive 1s,
  • 2 points for an even digit(2)
  • 1+1+2^2+1 for the increasing sequence of lengths 1,1,2, 1
  • 0 points for divisibility by 7
  • 4 points for the 9

    // Complete the compute_number_score function below.
    static int compute_number_score(int number) {
        if(number==0){
            return 2;
        }
        // System.out.println( getDigitWiseScore(number)  + " "  + countConsecutiveOnes(number) +" " +get(number));
        return getDigitWiseScore(number)  + countConsecutiveOnes(number) +get(number);

    }
    public static int getDigitWiseScore(int num){
        int score = 0;
        if(num%7==0) score+=1;
        while(num>0){
            int d = num%10;
            if(d==9) score+=4;
            if(d%2==0) score+=2;
            num = num/10;
        }
        return score;
    }
    public static int countConsecutiveOnes(int num){
        int score =0;
        int currentOnes = 0;
        while(num > 0 ){
            int d = num%10;
            num = num/10;
            if(d!=1 && currentOnes > 1 ){
                score+=5;
                score += (currentOnes-2)*5;
                currentOnes = 0;
            }
            if(d==1){
                currentOnes++;
            }
			if(d!=1 && currentOnes == 1 ){
				currentOnes = 0;
			}
        }
        if(currentOnes> 1){
            score+=5;
            score += (currentOnes-2)*5;
        }
        return score;
    }
    public static  int getSeq(int num){
        String s = Integer.toString(num);
        int score=0;
        int pointer = 1;
        int current = 1;
        int previous = s.charAt(0);
        while(pointer< s.length()){
            if(s.charAt(pointer) == previous){
                current++;
                previous++;
            } else if( current > 1 && s.charAt(pointer) != previous+1 ){
                score = score+ (current*current);
                current =01;
                score = score+1;
            } else{
                score = score+1;
            } 
            
            previous = s.charAt(pointer);
            pointer++;
        }
        return score;
    }

    public static int get(int num) {
        int x=0, count=1;
        List<Integer> store = new ArrayList<>();
        String s = Integer.toString(num);
        while(x<s.length()-1) {
            if (s.charAt(x)+1 == s.charAt(x+1)){
                count ++;
                x++;
            }
            else {
                x++;
                store.add(count);
                count = 1;
            }
        }
        store.add(count);
        int score = 0;
        for(Integer i: store){
            score += i*i;
        }
        return score;

    }
Comments (11)