Skip to content

Instantly share code, notes, and snippets.

@Surgo
Created February 1, 2011 13:16
Show Gist options
  • Save Surgo/805830 to your computer and use it in GitHub Desktop.
Save Surgo/805830 to your computer and use it in GitHub Desktop.
SRM489 - div2 - level one
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""SRM489 - div2 - level one
Little Teddy and Little Tracy are now learning how to speak words.
Their mother, of course, doesn't want them to speak bad words.
According to her definition, a word W is bad if at least one of
the following conditions hold (see the notes section for definitions):
* W contains the string badPrefix as a prefix.
* W contains the string badSuffix as a suffix.
* W contains the string badSubstring as a contiguous substring
that is neither a prefix nor a suffix of W.
Definition:
Class: BadVocabulary
Method: count
Parameters: String, String, String, String[]
Returns: int
Method signature: int count(String badPrefix, String badSuffix,
String badSubstring, String[] vocabulary)
Notes
- A prefix of a string is obtained by removing zero or more
contiguous characters from the end of the string.
- A suffix of a string is obtained by removing zero or more
contiguous characters from the beginning of the string.
"""
class BadVocabulary(object):
"""Time Travelling Cellar
Constraints:
- badPrefix, badSuffix, and badSubstring will each contain
between 1 and 50 characters, inclusive.
- vocabulary will contain between 1 and 50 elements, inclusive.
- Each element vocabulary will contain between 1 and 50 characters,
inclusive.
- Each character of badPrefix, badSuffix, and badSubstring
will be between 'a' and 'z', inclusive.
- Each character in vocabulary will be between 'a' and 'z', inclusive.
- All elements of vocabulary will be distinct.
>>> vocabluraly = BadVocabulary()
>>> print vocabluraly.count("bug", "bug", "bug",
... ["buggy", "debugger", "debug", ])
3
>>> print vocabluraly.count("a", "b", "c",
... ["a", "b", "tco", ])
3
>>> print vocabluraly.count("cut", "sore", "scar",
... ["scary", "oscar", ])
0
>>> print vocabluraly.count("bar", "else", "foo",
... ["foofoofoo", "foobar", "elsewhere", ])
1
>>> print vocabluraly.count("pre", "s", "all",
... ["all", "coders", "be", "prepared", "for", "the", "challenge", "phase", ])
3
"""
def __init__(self):
pass
def count(self, badPrefix, badSuffix, badSubstring, vocabulary):
c = 0
for word in vocabulary:
if word[:len(badPrefix)] == badPrefix \
or word[-(len(badSuffix)):] == badSuffix \
or badSubstring in word[1:-1]:
c += 1
return c
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