Write A Function Transform Which Takes A Single Argument Word In The Form Of A Non Empty String Consisting Of Lowercas 1 (144.59 KiB) Viewed 19 times
Write A Function Transform Which Takes A Single Argument Word In The Form Of A Non Empty String Consisting Of Lowercas 2 (89.53 KiB) Viewed 19 times
Write a function transform() which takes a single argument word in the form of a non-empty string consisting of lowercase alphabetical symbols only, and returns its 4-character encoded form. This encoded form retains the first character of word and transforms the rest of the string according to the following rules: 1. All vowels and the consonants 'w', 'h', and 'y' are replaced with the number 'o'. All other consonants are grouped based on their phonetic similarity and each group is assigned to a numeric code. The following CODES constant is a list that provides these groups. The number for each group is the corresponding index in the list: CODES = ['a, e, i, o, u, y, h, w', 'b, f, p, v', 'c, g, j, k, q, s, x, Z', 'd, t', 'l', 'm, n', 'r'] 2. Duplicates are removed. All adjacent instances of the same number are replaced with a single instance of that number). 3. All zeroes (¹0¹) are removed. 4. The resulting string is truncated to 4 characters. One or more trailing zeros are added if the string is shorter than 4 characters.
For example, 'alice' would first be transformed into 'a4020' (step 1); there are no duplicates to remove (step 2); after removing zeros it becomes 'a42' (step 3); as the string is shorter than 4 characters, we append 'o' to get 4 characters exactly (step 4) and the final form 'a420' is returned. Example calls to the function are: >>> transform("robert") 'r163' >>> transform("ruppert") 'r163' >>> transform("roubart") 'r163' >>> transform("hobart") 'h163' >>> transform ("people") 'p140' >>> transform("peeeeeeeeeeooopppppplee") 'p140'
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!