Simple String Compression
Function Name: compressString
Description
You are tasked with writing a function that applies a basic form of string compression. The compression algorithm you should implement will take a string consisting solely of alphabetical characters and output a compressed version of the string. In the compressed version, sequences of repeated characters should be replaced by that character followed by an integer indicating how many times the character repeats consecutively. For instance, 'aaabcc' would be compressed to 'a3b1c2'. The function should only compress the string if it results in a smaller string than the input.
Requirements
- The function must be named 'compressString'.
- The function must take a single parameter - the input string to compress.
- The input string will only contain uppercase and lowercase alphabetical characters (A-Z, a-z).
- If the compressed string is not shorter than the original string, the function should return the original string.
- Sequences with only a single character should not have the number '1' added after them in the compressed version.
Examples
Example 1: compressString('aabccc') returns 'a2bc3'.Example 2: compressString('abc') returns 'abc' (since compressing does not reduce the size of the string).Example 3: compressString('zzzzzzzz') returns 'z8'.Example 4: compressString('aabba') returns 'a2b2a'.
Links
Hailstone Sequence Length
Sun Aug 11 2024
In mathematics, the hailstone sequence (or Collatz sequence) is generated by the following process: begin with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The sequence will eventually reach the number 1, regardless of which positive integer is chosen initially. Your task is to create a function that computes the length of the hailstone sequence starting with a given positive integer.
Subsequence Validator
Tue Aug 13 2024
Write a function that takes two strings as arguments and returns a boolean indicating whether the second string is a subsequence of the first one.A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters.For example, 'ace' is a subsequence of 'abcde', but 'aec' is not. The function should be case-sensitive, meaning that 'ace' is not a subsequence of 'ABCDE'.