In [1]:
%%file __init__.py
__version__ = '0.0.4'


Overwriting __init__.py

In [2]:
ls


LICENSE                 Untitled.ipynb          __pycache__/            stringtools.py
README.md               __init__.py             function_writing.ipynb  test_code.ipynb

In [3]:
prob = '''The two problems we face are 
['red', 'and', 'yellow', 'and', 'green',
'and', 'blue,', 'purple', 'and', 'orange!'] 
and [1231, 'hello', [1, 2, 3]].'''

In [4]:
import string

In [5]:
string.punctuation


Out[5]:
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

In [22]:
def stripper(sentence):
    '''
    takes a string as input and removes punctuation
    returns a string
    '''
    clean = ''
    for let in sentence:
        if let in string.punctuation: continue
        clean += let
    return clean

In [ ]:
stripper(prob)

In [36]:
def stripper2(sentence):
    '''
    takes a string as input and removes punctuation
    returns a string using list comprehension
    '''
    y = [x for x in sentence if x not in string.punctuation]
    z = ''
    for let in y:
        z += let
    return z

In [37]:
stripper2(prob)


Out[37]:
'The two problems we face are red and yellow and green and blue purple and orange and 1231 hello 1 2 3'

In [41]:
def stripper3(sentence):
    '''
    takes a string as input and removes punctuation
    returns a string using list comprehension
    '''
    
    res = "".join([x for x in sentence if x not in string.punctuation])
    return res

In [42]:
stripper3(prob)


Out[42]:
'The two problems we face are red and yellow and green and blue purple and orange and 1231 hello 1 2 3'

In [6]:
def stripper4(sentence):
    '''
    takes a string as input and removes punctuation
    returns a string usinf list comprehension
    '''
    
    res = "".join([x for x in sentence if x not in set(string.punctuation)])
    return res

In [7]:
stripper4(prob)


Out[7]:
'The two problems we face are \nred and yellow and green\nand blue purple and orange \nand 1231 hello 1 2 3'

Creating the library

notice the '%%file' magic

it writes the block of code to a file, allowing us to call it with an

import stringtools

In [7]:
%%file stringtools.py
#!/usr/bin/python
# Filename: stringtools.py
import string

def split_embiggen_sort(sentence):
    '''
    takes a string and removes punctuation 
    splits it into a list of individual words, 
    converts them to uppercase and 
    sorts them in descending order
    '''
    all_converted = embiggen(sentence)
    all_converted = stripper3(all_converted)
    all_converted = divide_at_spaces(all_converted)
    all_converted = big_to_little(all_converted)
    
    return all_converted

def stripper3(sentence):
    '''
    takes a string as input and removes punctuation using list comprehension
    returns a string 
    '''
    res = "".join([x for x in sentence if x not in string.punctuation])
    return res

def embiggen(sentence):
    '''
    convert a string to uppercase and 
    returns a string
    '''
    assert(type(sentence) is str)
    converted = sentence.upper()
    return converted

def divide_at_spaces(sentence):
    '''
    takes a string as input
    splits the string at space characters and 
    returns a list of strings
    '''
    assert(type(sentence) is str)
    chopped = sentence.split()
    return chopped

def big_to_little(bits):
    '''
    sorts a list of words from longest to shortest and return a list
    '''
    assert(type(bits) is list)
    bits.sort(key=len)
    return bits


Overwriting stringtools.py

In [4]:
import stringtools

In [2]:
stringtools.big_to_little('this is a test string'.split())


Out[2]:
['a', 'is', 'this', 'test', 'string']

In [44]:
big_to_little(bits = problem1)


Out[44]:
['red',
 'and',
 'and',
 'and',
 'and',
 'green',
 'blue,',
 'yellow',
 'purple',
 'orange!']

In [37]:
problem1 = 'red and yellow and green and blue, purple and orange!'.split()

In [62]:
split_embiggen_sort("The two problems we face are ['red', 'and', 'yellow', 'and', 'green', 'and', 'blue,', 'purple', 'and', 'orange!'] and [1231, 'hello', [1, 2, 3]].")


Out[62]:
['WE',
 '2,',
 'THE',
 'TWO',
 'ARE',
 'AND',
 '[1,',
 'FACE',
 '3]].',
 "'AND',",
 "'AND',",
 "'AND',",
 "'AND',",
 '[1231,',
 "['RED',",
 'PROBLEMS',
 "'GREEN',",
 "'BLUE,',",
 "'HELLO',",
 "'YELLOW',",
 "'PURPLE',",
 "'ORANGE!']"]

In [38]:
problem2 = [1231,'hello',[1,2,3]]

In [39]:
print('The two problems we face are {} and {}.'.format(problem1,problem2))


The two problems we face are ['red', 'and', 'yellow', 'and', 'green', 'and', 'blue,', 'purple', 'and', 'orange!'] and [1231, 'hello', [1, 2, 3]].

In [40]:
problem3 = 12345

In [34]:
big_to_little([1231,'hello',[1,2,3]])

In [6]:
stringtools.stripper3?

In [ ]: