In [ ]:
from __future__ import absolute_import, division, print_function
In [ ]:
%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 [ ]:
# plot residuals
In [ ]:
from itertools import groupby # NOT REGULAR GROUPBY
from itertools import product, cycle, izip
import re # regular expressions
In [ ]:
test_string = """de3456yghj87654edfghuio908ujhgyuY^YHJUi8ytgh gtyujnh y7"""
In [ ]:
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 [ ]:
test_string
In [ ]:
groups = []
uniquekeys = []
for k, g in groupby(test_string, lambda x: x.isdigit()):
groups.append(list(g))
uniquekeys.append(k)
In [ ]:
print(groups)
print(uniquekeys)
In [ ]:
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 [ ]:
# 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 [ ]:
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)
In [ ]:
def ex1(num):
"""A stupid example generator to prove a point."""
while num > 1:
num += 1
yield num
In [ ]:
hey = ex1(5)
In [ ]:
hey.next()
In [ ]:
hey.next()
In [ ]:
even_better_name = 5
In [ ]:
even_better_name = 5
In [ ]:
even_better_name = 5
In [ ]:
even_better_name = 5
In [ ]:
even_better_name = 5
In [ ]:
even_better_name = 5
In [ ]: