Twain
SRM 169 · 2003-10-28 · by huntergt
Problem Statement
Year 1:
- - If a word starts with "x", replace the "x" with a "z".
- - Change all remaining "x"s to "ks"s.
- - Change all "y"s to "i"s.
- - If a "c" is directly followed by an "e" or "i", change the "c" to an "s".
- - If a "c" is directly followed by a "k", remove the "c". Keep applying this rule as necessary (Example: "cck" becomes "k".)
- - If a word starts with "sch", change the "sch" to a "sk".
- - If a "ch" is directly followed by an "r", change the "ch" to a "k".
- - After applying the above rules, change all "c"s that are not directly followed by an "h", to a "k". (This includes all "c"s that are the last letter of a word.)
- - If a word starts with "kn" change the "kn" to an "n".
- - Change all double consonants of the same letter to a single consonant. A consonant is any letter that is not one of "a, e, i, o, u." (Example: "apple" becomes "aple"). Keep applying this rule as necessary (Example: "zzz" becomes "z".)
The plan requires that rules for each year are followed in the order they are presented, and changes for each year occur after all the changes from previous years.
Write a class Twain, which contains a method getNewSpelling. getNewSpelling takes as parameters an
Constraints
- year will be between 0 and 7, inclusive
- phrase will be between 0 and 50 characters, inclusive
- phrase will contain only lowercase letters ('a'-'z') and spaces (' ').
- phrase will not contain three or more of the same consonant in a row
1 "i fixed the chrome xerox by the cyclical church" Returns: "i fiksed the chrome zeroks by the cyclical church"
In year 1, the first "x" in "xerox" is changed to a "z". Then, the "x"s in "fixed" and "zerox" are changed to "ks"s.
2 "i fixed the chrome xerox by the cyclical church" Returns: "i fiksed the chrome zeroks bi the ciclical church"
In year 2, the "y"s in "by" and "cyclical" are changed to "i"s.
0 "this is unchanged" Returns: "this is unchanged"
Since the year is 0, no changes occur.
7 "sch kn x xschrx cknnchc cyck xxceci" Returns: "sk n z zskrks nchk sik zksesi"
In year 1, the phrase becomes "sch kn z zschrks cknnchc cyck zksceci" due to rules concerning the letter "x". In year 2, all "y"s are changed to "i"s yielding "sch kn z zschrks cknnchc cick zksceci". In year 3, "ce" and "ci" are changed to "se" and "si" yielding "sch kn z zschrks cknnchc sick zkssesi". In year 4, "ck" is changed to "k" and the phrase becomes "sch kn z zschrks knnchc sik zkssesi". In year 5, words that begin with "sch" are made to begin with "sk", "chr" is changed to "kr", and all "c"s not followed by an "h" are changed to "k" yielding "sk kn z zskrks knnchk sik zkssesi". In year 6, words that start with "kn" now start with "n" and the phrase becomes "sk n z zskrks nnchk sik zkssesi". Finally, in year 7, double consonants are removed yielding the final result.
7 " concoction convalescence cyclical cello " Returns: " konkoktion konvalesense siklikal selo "
Beware of extra spaces.
7 "" Returns: ""
Don't forget the empty case.
Submissions are judged against all 53 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Twain with a public method string getNewSpelling(int year, string phrase) · 53 test cases · 2 s / 256 MB per case