Skip to content

CodeQuiz 5

In this code note, we will explore the sample answers to the seven methods implementation task from codquiz project. Make sure to watch and try out the code for each of the challenges.

List of challenge methods:

  • safeBridge
  • countWords
  • replaceSpace
  • extractTarget
  • capsPart



› Solution

Please watch the following explainer video for each method's implementation detail:

Please expand to see instructor's implementation of above methods:

Expand to see the code
import org.junit.Test;
import stayaway.verify.easy.LevelThreeTests;
import stayaway.verify.strings.StringPart1Test;
import java.util.Arrays;

public class StringPractice {
    // Run main method to verify your
    // challenge method implementations
    public static void main(String[] args) {
        StringPart1Test str1Test = new StringPart1Test();
        // Comment out to isolate the method run
        str1Test.safeBridge();
        str1Test.countWords();
        str1Test.replaceSpace();
        str1Test.extractTarget();
        str1Test.capsPart();
    }

    /**
     * INSTRUCTIONS: #ready #actualInterview #Flywheel
     * -------------
     * Given a string that represents a bridge "|====|", determine that
     * the bridge is safe for crossing or not.  A bride is said to be unsafe
     * if it contains a space character.
     *
     * MORE INFO:
     * -------------
     * "|=======|"  ←  safe
     * "|== ====|"  ←  unsafe
     * "|=|"        ←  safe
     *
     * EXAMPLE:
     * -------------
     * safeBridge("|====|")  ➞ true
     * safeBridge("|== ==|") ➞ false
     * safeBridge("|== == ==|") ➞ false
     *
     * RULE:
     * -------------
     *  > Do not use sout
     */
    public boolean safeBridge(String bridgePic) {
        boolean isSafe = bridgePic.contains(" ");
        return !isSafe;
    }

    /**
     * INSTRUCTIONS: #ready
     * -------------
     * Given a text, count the number of words it contains
     * and return it to the user.
     *
     *
     * EXAMPLE:
     * -------------
     * countWords("hello there students")  ➞ 3
     * countWords("today is the best day") ➞ 5
     * countWords("planet earth")          ➞ 2
     *
     * RULE:
     * -------------
     *  > Do not use sout
     */
    public int countWords(String text) {
        int count = text.split(" ").length;
        return count;
    }

    /**
     * INSTRUCTIONS: #ready
     * -------------
     * Given a text, replace every space characters it contains with '*' character and
     * return the resulting text.
     *
     *
     * EXAMPLE:
     * -------------
     * replaceSpace("hello there students")  ➞ "hello*there*students"
     * replaceSpace("today is the best day") ➞ "today*is*the*best*day"
     * replaceSpace("planet earth")          ➞ "planet*earth"
     *
     * RULE:
     * -------------
     *  > Do not use sout
     */
    public String replaceSpace(String text) {
        String result = text.replace(" ", "*");
        return result;
    }

    /**
     * INSTRUCTIONS: #ready
     * -------------
     * Given a text and target String, extract the sub part of a text that
     * starts with the target string.  If the target string does not exist in the text
     * and extraction is not possible, return "ERROR" instead.
     *
     *
     * EXAMPLE:
     * -------------
     * replaceSpace("hello there students",  "there") ➞ "there students"
     * replaceSpace("today is the best day", "is"   ) ➞ "is the best day"
     * replaceSpace("planet earth is blue",  "x"    ) ➞ "ERROR"
     *
     * RULE:
     * -------------
     *  > Do not use sout
     */
    public String extractTarget(String text, String target) {
        if(!text.contains(target)) return "ERROR";
        int startIdx = text.indexOf(target);
        return text.substring(startIdx);
    }

    /**
     * INSTRUCTIONS: #ready #actualInterview #AWS
     * -------------
     * Given a text and a target String, identify the target part of the string from the text
     * and return a new String where the target portion is all uppercase.
     * Please see EXAMPLE section for more details. Also, use charAt() method to accomplish this
     * but do not use replace() method.
     *
     *
     * EXAMPLE:
     * -------------
     * capsPart("hello there students", "there") ➞ "hello THERE students"
     * capsPart("today is the best day", "is"  ) ➞ "today IS the best day"
     * capsPart("planet earth is blue",  "blue") ➞ "planet earth is BLUE"
     *
     * HINT:
     * -------------
     * > In ASCII table char `a`  ➞ 97
     * > In ASCII Table char `A`  ➞ 65
     * > Skip the uppercase of space ` ` character
     *
     * RULE:
     * -------------
     *  > Do not use replace() method
     *  > Make use of charAt() method
     *  > Do not use sout
     *  > text is all lowercase guaranteed
     *  > target is non-repeating
     */
    public String  capsPart(String text, String target) {
        String result  = "";
        int start = text.indexOf(target);
        int end = start + target.length();
        for(int i = 0; i <= text.length()-1; i++) {
            char each = text.charAt(i);
            if(i >= start && i < end) {
                each -= 32;
            }
            result += each;
        }
        return result;
    }
}//end::class



greet

Given a string that represents a bridge "|====|", determine that the bridge is safe for crossing or not. A bride is said to be unsafe if it contains a space character.

Hint:

1
2
3
"|=======|"  ←  safe
"|== ====|"  ←  unsafe
"|=|"        ←  safe

EXAMPLE:

1
2
3
safeBridge("|====|")  ➞ true
safeBridge("|== ==|") ➞ false
safeBridge("|== == ==|") ➞ false

Sample Answer:

1
2
3
4
public boolean safeBridge(String bridgePic) {
    boolean isSafe = bridgePic.contains(" ");
    return !isSafe;
}



countWords

Given a text, count the number of words it contains and return it to the user.

EXAMPLE:

1
2
3
countWords("hello there students")  ➞ 3
countWords("today is the best day") ➞ 5
countWords("planet earth")          ➞ 2

Sample Answer:

1
2
3
4
public int countWords(String text) {
    int count = text.split(" ").length;
    return count;
}



replaceSpace

Given a text, replace every space characters it contains with '*' character and return the resulting text.

Example:

1
2
3
replaceSpace("hello there students")  ➞ "hello*there*students"
replaceSpace("today is the best day") ➞ "today*is*the*best*day"
replaceSpace("planet earth")          ➞ "planet*earth"

Sample Answer:

1
2
3
4
public String replaceSpace(String text) {
    String result = text.replace(" ", "*");
    return result;
}



extractTarget

Given a text and target String, extract the sub part of a text that starts with the target string. If the target string does not exist in the text and extraction is not possible, return "ERROR" instead.

Example:

1
2
3
replaceSpace("hello there students",  "there") ➞ "there students"
replaceSpace("today is the best day", "is"   ) ➞ "is the best day"
replaceSpace("planet earth is blue",  "x"    ) ➞ "ERROR"

Sample Answer:

1
2
3
4
5
public String extractTarget(String text, String target) {
    if(!text.contains(target)) return "ERROR";
    int startIdx = text.indexOf(target);
    return text.substring(startIdx);
}



capsPart

Given a text and a target String, identify the target part of the string from the text and return a new String where the target portion is all uppercase. Please see EXAMPLE section for more details. Also, use charAt() method to accomplish this but do not use replace() method. Please keep the following constraints in mind:

  • Do not use replace() method
  • Make use of charAt() method
  • text is all lowercase guaranteed
  • target is non-repeating

HINT:

1
2
3
In ASCII table char `a`  ➞ 97
In ASCII table char `A`  ➞ 65
Skip the uppercase of space ` ` character

Example:

1
2
3
capsPart("hello there students", "there") ➞ "hello THERE students"
capsPart("today is the best day", "is"  ) ➞ "today IS the best day"
capsPart("planet earth is blue",  "blue") ➞ "planet earth is BLUE"

Sample Answer:

public String  capsPart(String text, String target) {
    String result  = "";
    int start = text.indexOf(target);
    int end = start + target.length();
    for(int i = 0; i <= text.length()-1; i++) {
        char each = text.charAt(i);
        if(i >= start && i < end) {
            each -= 32;
        }
        result += each;
    }
    return result;
}