Wednesday, December 6, 2023
HomeSoftware EngineeringListed Capitalization in Java | Discover ways to Grasp Software program Engineering,...

Listed Capitalization in Java | Discover ways to Grasp Software program Engineering, DevOps and Cloud


The problem

Given a string and an array of integers representing indices, capitalize all letters on the given indices.

Instance:

  • capitalize("abcdef",[1,2,5]) = "aBCdeF"
  • capitalize("abcdef",[1,2,5,100]) = "aBCdeF". There isn’t any index 100.

The enter shall be a lowercase string with no areas and an array of digits.

The answer in Java code

Possibility 1:

bundle resolution;

import java.util.*;

public class Answer{
    public static String capitalize(String s, int[] ind){
        char[] chars = s.toCharArray();
        Arrays.stream(ind)
                .filter(c -> c < s.size())
                .forEach(c -> chars[c] = Character.toUpperCase(chars[c]));
        return new String(chars);
    }
}

Possibility 2:

bundle resolution;

public class Answer {
    public static String capitalize(String s, int[] ind){
      char[] end result = s.toCharArray();
    
      for(int i : ind) {
        if(i < end result.size) end result[i] = Character.toUpperCase(end result[i]);
        else proceed;
      }
      String str = String.valueOf(end result);
      return str;
      
    }
}

Possibility 3:

bundle resolution;

public interface Answer {
  static String capitalize(String s, int[] ind) {
    for (int i : ind) {
      if (i < s.size()) {
        s = s.substring(0, i) + (char) (s.charAt(i) - 32) + s.substring(i + 1);
      }
    }
    return s;
  }
}

Check instances to validate our resolution

import java.util.Random;
import java.util.TreeSet;

import static Answer.capitalize;

import org.junit.runners.JUnit4;
import org.junit.Check;
import static org.junit.Assert.assertEquals;

public class SolutionTest{
    personal static last String lowercase = "abcdefghijklmnopqrstuvwxyz";
    personal static last Random random = new Random();
    
    @Check
    public void basicTest(){
        assertEquals("aBCdeF", capitalize("abcdef", new int[]{1,2,5}));
        assertEquals("aBCdeF", capitalize("abcdef", new int[]{1,2,5,100}));
        assertEquals("abRacaDabRA", capitalize("abracadabra", new int[]{2,6,9,10}));
        assertEquals("Indexinglessons", capitalize("indexinglessons", new int[]{0}));
    }
    
    personal String solve_kvWOF(String s, int[] ind){
        StringBuilder sb = new StringBuilder(s);
        for(int i : ind){
            if(i < sb.size())
                sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
        }
        return sb.toString();
    }
    
    @Check
    public void randomTest(){
        for(int i = 0; i < 100; i++){
            int r = random.nextInt(10) + 10;
            StringBuilder sb = new StringBuilder();
            for(int j = 0; j < r; j++)
                sb.append(lowercase.charAt(random.nextInt(lowercase.size())));
            TreeSet<Integer> set = new TreeSet<Integer>();
            for(int j = 0; j < sb.size(); j++)
                set.add(random.nextInt(r));
            int[] a = new int[set.size()];
            for(int j = 0; j < a.size; j++)
                a[j] = set.pollFirst();
            String str = sb.toString();
            assertEquals(solve_kvWOF(str, a), capitalize(str, a));
        }
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments