Skip to content

Instantly share code, notes, and snippets.

@Surgo
Created February 4, 2011 02:50
Show Gist options
  • Save Surgo/810665 to your computer and use it in GitHub Desktop.
Save Surgo/810665 to your computer and use it in GitHub Desktop.
SRM486 - div2 - level one
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""SRM486 - div2 - level one
Strange abbreviations are often used to write text messages
on uncomfortable mobile devices. One particular strategy for
encoding texts composed of alphabetic characters and spaces
is the following:
* Spaces are maintained, and each word is encoded
individually. A word is a consecutive string of
alphabetic characters.
* If the word is composed only of vowels, it is written
exactly as in the original message.
* If the word has at least one consonant, write only
the consonants that do not have another consonant
immediately before them. Do not write any vowels.
* The letters considered vowels in these rules are
'a', 'e', 'i', 'o' and 'u'. All other letters are
considered consonants.
For instance, "ps i love u" would be abbreviated as "p i lv u"
while "please please me" would be abbreviated as "ps ps m".
You will be given the original message in the String original.
Return a String with the message abbreviated using
the described strategy.
Definition:
Class: TxMsg
Method: getMessage
Parameters: String
Returns: String
Method signature: String getMessage(String original)
"""
import re
class TxMsg(object):
"""Text message
Constraints:
- original will contain between 1 and 50 characters,
inclusive.
- Each character of original will be a lowercase
letter ('a'-'z'), or a space.
- There will not be two consecutive spaces in original,
nor will the first or last character be a space.
>>> msg = TxMsg()
>>> print msg.getMessage("text message")
tx msg
>>> print msg.getMessage("ps i love u")
p i lv u
>>> print msg.getMessage("please please me")
ps ps m
>>> print msg.getMessage("back to the ussr")
bc t t s
>>> print msg.getMessage("aeiou bcdfghjklmnpqrstvwxyz")
aeiou b
"""
def __init__(self):
pass
def getMessage(self, original):
def encode(word):
if re.compile(r'[^a^e^i^o^u]+').findall(word):
# not only vowels
return ''.join([w[0] for w in \
re.compile(r'[^a^e^i^o^u]+').findall(word) if w])
else:
# only vowels
return word
return ' '.join([encode(w) for w in \
original.split(' ') if w])
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment