import pandas as pd
import numpy as np
phrases = pd.read_csv("H:/python_jiggering/logs/aoc_2017/d4_input.txt", header = None, sep = '\t')
phrases.head()
phrases = phrases[0]
# now write a function to make a judgement:
def all_unique(x):
x = np.array(x.split())
if(len(x) == len(np.unique(x))):
return 1
else:
return 0
uniques = phrases.apply(all_unique)
uniques.sum()
Now we basically need to do the same thing except we will test for anagrams of the words within phrases as well. This seems like it will be pretty easy to solve if I can figure out a way to sort the letters of a string and then recombine.
Let's try this function to standardize a single word:
def std_word(x):
letter_list = []
for let in x:
letter_list.append(let)
letter_list.sort()
out = ''
for let in letter_list:
out += let
return out
Now a function to do this to a whole passphrase
def std_pass(x):
x = np.array(x.split())
return ' '.join([std_word(xi) for xi in x])
Finally modify the all_unique
function to include this
def all_unique2(x):
x = np.array(std_pass(x).split())
if(len(x) == len(np.unique(x))):
return 1
else:
return 0
uniques2 = phrases.apply(all_unique2)
uniques2.sum()