In [1]:
%matplotlib inline
%config InlineBackend.figure_format='retina'
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context('talk')
sns.set_style('darkgrid')
plt.rcParams['figure.figsize'] = 12, 8 # plotsize
import numpy as np
import pandas as pd
In [2]:
# plot residuals
In [3]:
from itertools import groupby # NOT REGULAR GROUPBY
from itertools import product, cycle, izip
import re # regular expressions
In [5]:
test_string = """de3456yghj87654edfghuio908ujhgyuY^YHJUi8ytgh gtyujnh y7"""
In [6]:
count = 0
digits = []
for x in test_string:
try:
int(x)
count += 1
digits.append(int(x))
except:
pass
print "Number of digits:", str(count) + ";"
print "They are:", digits
In [7]:
test_string
Out[7]:
In [8]:
groups = []
uniquekeys = []
for k, g in groupby(test_string, lambda x: x.isdigit()):
groups.append(list(g))
uniquekeys.append(k)
In [9]:
print(groups)
print(uniquekeys)
In [10]:
numbers = []
for x, y in izip(groups, uniquekeys):
if y:
numbers.append(int(''.join([j for j in x])))
print "Number:", np.sum(uniquekeys)
print "They are:", numbers
In [11]:
# In one cell
def solution_2(test_string):
groups = []
uniquekeys = []
for k, g in groupby(test_string, lambda x: x.isdigit()):
if k:
groups.append(int(''.join([j for j in g])))
return len(groups), groups
print solution_2(test_string)
In [12]:
def solution_3(test_string):
"""Regular expressions can be a very powerful and useful tool."""
groups = [int(j) for j in re.findall(r'\d+', test_string)]
return len(groups), groups
solution_3(test_string)
Out[12]:
In [13]:
def ex1(num):
"""A stupid example generator to prove a point."""
while num > 1:
num += 1
yield num
In [14]:
hey = ex1(5)
In [15]:
hey.next()
Out[15]:
In [58]:
hey.next()
Out[58]:
In [ ]: