Package mini1

Class LoopInMySoup


  • public class LoopInMySoup
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int countMatchingChars​(java.lang.String s, java.lang.String t)
      Counts the number of positions in a pair of strings that have matching characters.
      static int countSubstrings​(java.lang.String t, java.lang.String s, boolean allowOverlap)
      Counts the number of times that one string occurs as a substring in another, optionally allowing the occurrences to overlap.
      static java.lang.String getRun​(java.lang.String s, int start)
      Returns a run of characters in s, starting at position start and continuing as long as the characters match.
      static boolean isFibonacciTypeSequence​(java.lang.String text)
      Determines whether the given string of text represents a "Fibonacci-type" sequence, where the given text consists of integer values separated by arbitrary whitespace.
      static java.lang.String mergeWithRuns​(java.lang.String t, java.lang.String s)
      Merges two strings together, using alternating characters from each, except that runs of the same character are kept together.
      static int newtonCount​(double x, double err)
      Determines the number of iterations of Newton's method required to approximate the square root of x within the given bound.
      static java.lang.String removeRuns​(java.lang.String s)
      Returns a string similar to the given string with all runs of consecutive, repeated characters removed.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • countMatchingChars

        public static int countMatchingChars​(java.lang.String s,
                                             java.lang.String t)
        Counts the number of positions in a pair of strings that have matching characters. The strings need not be the same length. For example, countMatchingChars("abcde", "xbydzzzzz") returns 2.
        Parameters:
        s - any string
        t - any string
        Returns:
        number of positions in which the characters match in the two strings
      • newtonCount

        public static int newtonCount​(double x,
                                      double err)
        Determines the number of iterations of Newton's method required to approximate the square root of x within the given bound. Newton's method starts out by setting the initial approximate answer to x. Then in each iteration, answer is replaced by the quantity (answer + x / answer) / 2.0. The process stops when the difference between x and (answer * answer) is strictly less than the given bound err. The method returns the number of iterations required. The given value x must be non-negative.

        For example, given x = 10 the first three iterations of Newton's method produce the approximate values 5.5, 3.66, and 3.20. Those three values squared are 30.29, 13.39, and 10.21, respectively. Therefore newtonCount(10, 1.0) returns 3, since it takes 3 iterations to get the result 10.21 that is within 1.0 of x. On the other hand, newtonCount(10, 200) returns 0, since 10 * 10 = 100 is already within 200 units of x = 10.

        Parameters:
        x - value whose square root is to be approximated
        err - given bound for the approximation
        Returns:
        number of iterations required to get an approximation whose square is within the given bound of x
      • isFibonacciTypeSequence

        public static boolean isFibonacciTypeSequence​(java.lang.String text)
        Determines whether the given string of text represents a "Fibonacci-type" sequence, where the given text consists of integer values separated by arbitrary whitespace. A Fibonacci-type sequence is any sequence of numbers in which each value (other than the first and second) is the sum of the previous two values. An example would be "-2 1 -1 0 -1 -1 -2 -3 -5 -8". This method always returns true if the sequence has fewer than 3 numbers. The behavior is undefined if the provided string contains any non-numeric values.
        Parameters:
        text - string of text (possibly empty) containing numbers separated by whitespace
        Returns:
        true if the given sequence of numbers is a Fibonacci-type sequence, false otherwise
      • removeRuns

        public static java.lang.String removeRuns​(java.lang.String s)
        Returns a string similar to the given string with all runs of consecutive, repeated characters removed. For example,
        • given "apple", returns "aple"
        • given "banana", returns "banana"
        • given "baaannannnnaa", returns "banana"
        • given an empty string, returns an empty string
        Parameters:
        s - given string
        Returns:
        string created from s by removing runs of the same character
      • countSubstrings

        public static int countSubstrings​(java.lang.String t,
                                          java.lang.String s,
                                          boolean allowOverlap)
        Counts the number of times that one string occurs as a substring in another, optionally allowing the occurrences to overlap. For example:
        • countSubstrings("aa", "aaaaa", false) returns 2
        • countSubstrings("aa", "aaaaa", true) returns 4
        • countSubstrings("aa", "ababab", true) returns 0
        Parameters:
        t - string we are looking for ("target")
        s - string in which we are looking ("source")
        allowOverlap - true if occurrences of t are allowed to overlap
        Returns:
        number of times t occurs in s as a substring
      • mergeWithRuns

        public static java.lang.String mergeWithRuns​(java.lang.String t,
                                                     java.lang.String s)
        Merges two strings together, using alternating characters from each, except that runs of the same character are kept together. For example,
        • mergeWithRuns("abcde", "xyz") returns "axbyczde"
        • mergeWithRuns("abbbbcde", "xyzzz") returns "axbbbbyczzzde"
        Either or both of the strings may be empty. If the first string is nonempty, its first character will be first in the returned string.
        Parameters:
        t - first string
        s - second string
        Returns:
        string obtained by merging characters from t and s, preserving runs
      • getRun

        public static java.lang.String getRun​(java.lang.String s,
                                              int start)
        Returns a run of characters in s, starting at position start and continuing as long as the characters match. For example, getRun("abbbbbcbc", 2) returns "bbbb", and getRun("abbbbbcbc", 0) returns "a".
        Parameters:
        s - given string
        start - index at which to start looking for a run
        Returns: