4 ms·
corpus = """ yellow tooth """ for line in corpus.splitlines(): for letter in line: if line.count(letter) == 1: print letter
by phektus 15y ago
corpus = """
yellow
tooth
"""
for line in corpus.splitlines():
for letter in line:
if line.count(letter) == 1:
print letter
break
- achompas 15y agoMy first pass (assumes input file is an arg): import sys def first_unique(word): if len(word) == 0: return "EOF" for i in range(0, len(word)-1): return word[i] if word[i] != word[i+1] return "NA" def main(): with open(sys.argv[1],'r') as f: print first_unique(f.readline())
- achompas 15y agoTrying functional: import sys with open(sys.argv[1], 'r') as f: word = f.readline() try: print filter(lambda x: word.count(x) == 1, word)[0] except IndexError: print ""
- epicureanideal 15y agoThis seems pretty inefficient, as it seems that line.count would be checking the whole line for how many times the letter appears. However, if you're solving in Python I guess efficiency is not really top priority anyway, and they also didn't specify a language or efficiency.
- amikazmi 15y agoIO.readlines(file).each do |w| puts w.split(//).find { |c| w.count(c) == 1 } end