Wednesday, January 20, 2021

 #codingexercise

Public string[] getParts(String complex) {

       Int  plus = complex.indexOf(‘+’);

       Int minus = complex.indexOf(‘-’);

       If ( plus == -1 && minus == -1) return new String[1];

       String[] parts = new String[3];

       If (plus != -1 && minus != -1) {

           Parts[0] = “+-”;

           Parts[1] = complex.substring(0, plus);

           Parts[2] = complex.substring(minus + 1, complex.length); 

           Return parts;

       }

      If(plus !=1) {

           Parts[0] = “+”;

           Parts[1] = complex.substring(0, plus);

           Parts[2] = complex.substring(plus + 1, complex.length); 

           Return parts;

      }

      If (minus != -1) {

           Parts[0] = “-”;

           Parts[1] = complex.substring(0, minus);

           Parts[2] = complex.substring(minus + 1, complex.length); 

           Return parts;

       }

      Parts = newString[2];

       Return parts;


}

This makes a few assumptions but it merely finds the real and the complex parts. 


No comments:

Post a Comment