Java Coding Problems and Solutions Set5

    21. Get Roman Values for the given number .
    22. Get the expected sum from the set of given numbers.
    23. Reg_Exp : sum of numbers with decimal present in a string with chars and special chars.
    24. Reg_Exp : problem number 23 using ArrayList<Double>.
    25. Rotate the chars in a given string. 

21.  //get Roman Values for the given number

       public static void getRoman() {

             int[] num = {  1000,  900,  500, 400, 100,   90,  50,   40,   10,    9,   5,    4,   1};

             String[] roman = {   "M", "CM", "D", "CD", "C", "XC", "L", "XL",  "X", "IX", "V", "IV", "I"};

             int target = 1994;

             int target1 = 0;

             ArrayList<Integer> alist = new ArrayList<>();

             for(int i=0;i<13;i++) {

                    if(target>=num[i]) {

                           target1 = target-num[i];

                           alist.add(num[i]);

                           target = target1;

                    }

             }

             System.out.println("The given target can be reached adding these : "+alist);

            

             //Get Roman values

             String romanVal = ""; //do not use null, you will get output like this "nullMCMXCIV"

             int alistVal = 0;

             int numArr = 0;

             for(int i=0;i<alist.size();i++) {

                    alistVal = alist.get(i);

                    for(int j=0;j<num.length;j++) {

                           if(num[j]==alistVal) {

                                 romanVal=romanVal+roman[j];

                           }

                    }

             }

             System.out.println("The corresponding Roman Value : "+romanVal);

            

       }

 

Output:

The given target can be reached adding these : [1000, 900, 90, 4]

The corresponding Roman Value : MCMXCIV


22. //get the sum from the given numbers

public static void getSum() {

       int[] num = {  1000,  900, 500, 400, 100, 90, 50, 40, 10, 9,5, 4,1};

       int target = 1994 ;

       int target1 = 0;

       ArrayList<Integer> alist = new ArrayList<>();

       for(int i=0;i<13;i++) {

             if(target>=num[i]) {

                    target1 = target-num[i];

                    alist.add(num[i]);

                    target = target1;

             }

       }

       System.out.println("The given target can be reached adding these : "+alist);

}

Output:

The given target can be reached adding these : [1000, 900, 90, 4]

When target = 2994 ;

The given target can be reached adding these : [1000, 900, 500, 400, 100, 90, 4]

 

When target = 649 ;

The given target can be reached adding these : [500, 100, 40, 9]

 

 23. //Reg_expression: 

get sum of the given values with decimal in a string with chars and superchars

       public static void regExpWithDouble() {

             String str = "asIS3.14jkK#jI@2.3Ns(1.14)jlPas.6jf";

             String str1 = str.replaceAll("[^0-9.]", " ");

             String[] strArr = str1.split(" ");

             System.out.println("The array is : "+Arrays.toString(strArr));

             int len = strArr.length;

             Double sum = 0.0;

             Double arrElement = 0.0;

            

             for(int i=0;i<len;i++) {

                    if(!(strArr[i].equals(""))) {

                           arrElement = Double.parseDouble(strArr[i]);

                           sum = sum+arrElement;

                    }

             }

             System.out.println("The sum is : "+sum);

             //to get the exact output value

             System.out.printf("The exact output is : %.2f", sum);

             //OR

             //You can use BigDecimal

             //OR

             //You can use math.round

       }

Output:

The array is : [, , , , 3.14, , , , , , , 2.3, , , 1.14, , , , , , .6]

The sum is : 7.179999999999999

The exact output is :7.18

 

 

 24. //Reg_expression: 

get sum of the given values with decimal in a string with chars and superchars using ArrayList<Double>

       public static void regExpWithDouble() {

             String str = "asIS3.14jkK#jI@2.3Ns(1.14)jlPas.6jf";

             String str1 = str.replaceAll("[^0-9.]", " ");

             String[] strArr = str1.split(" ");

             System.out.println("The array is : "+Arrays.toString(strArr));

             int len = strArr.length;

             double sum = 0.0;

             double arrElement = 0.0;

             ArrayList<Double> alist = new ArrayList<>();

             for(int i=0;i<len;i++) {

                    if(!(strArr[i].equals(""))) {

                           arrElement = Double.parseDouble(strArr[i]);

                           //sum = sum+arrElement;

                           alist.add(arrElement);

                    }

             }

             System.out.println("The arraylist is : "+alist);

             for(int i=0;i<alist.size();i++) {

                    sum = sum + alist.get(i);

             }

             //to get the exact output value

             System.out.printf("The sum is : "+sum);

Output:

The array is : [, , , , 3.14, , , , , , , 2.3, , , 1.14, , , , , , .6]

The arraylist is : [3.14, 2.3, 1.14, 0.6]

The sum is : 7.179999999999999

 

25. //rotate chars in a given string.

//Applied below concept, but did not give the exact result. Need to investigate...

       public static void rotateChars() {

             String str = "universe";

             int len = str.length();

             String firstChar = "";

             String withOutFirstChar = "";

             System.out.println("Rotation 0 = "+str);

             for(int i=0;i<len;i++) {

                    firstChar = Character.toString(str.charAt(i));

                    //Get the string without first char

                    for(int j=1;j<len;j++) {

                           withOutFirstChar = withOutFirstChar+Character.toString(str.charAt(j));

                    }

                    //Get the String without first char + first char at the end

                    String rotated = withOutFirstChar+firstChar;

                    System.out.println("Rotation "+(i+1)+" = "+rotated);

                   

                    str = rotated;

                    firstChar = "";

                    withOutFirstChar = "";

             }

       }

 

Output:

Rotation 0 = universe

Rotation 1 = niverseu

Rotation 2 = iverseui

Rotation 3 = verseuie

Rotation 4 = erseuies

Rotation 5 = rseuiesu

Rotation 6 = seuiesue

Rotation 7 = euiesueu

Rotation 8 = uiesueuu

 

No comments:

Post a Comment