Last active
November 8, 2017 06:21
-
-
Save Slater-Victoroff/679aeaf8a73208a3efc6149f8cb215e3 to your computer and use it in GitHub Desktop.
Firstletter oneliner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def firstNonRepeated(s): | |
return ([letter for i, letter in enumerate(s) if letter not in (s[:i] + s[i+1:])] + [""])[0] | |
def first_non_repeated1(s): | |
for i, letter in enumerate(s): | |
if letter not in (s[:i] + s[i+1:]): | |
return letter | |
return "" | |
def first_non_repeated1(s): | |
for i, letter in enumerate(s): | |
if letter not in (s[:i] + s[i+1:]): | |
return letter | |
return "" | |
def first_non_repeated2(s): | |
d = {} | |
for letter in s: | |
if letter not in d: d[letter] = 1 | |
else: d[letter]+=1 | |
for letter in s: | |
if d[letter]==1: return letter | |
return "" | |
def first_non_repeated3(s): | |
d = defaultdict(int) | |
for letter in s: | |
d[letter]+=1 | |
for letter in s: | |
if d[letter]==1: return letter | |
return "" | |
if __name__ == "__main__": | |
import timeit | |
print(timeit.timeit('first_non_repeated1("asdhfas;kjdfhlakj")', setup='from __main__ import first_non_repeated1', number=100000)) | |
print(timeit.timeit('first_non_repeated2("asdhfas;kjdfhlakj")', setup='from __main__ import first_non_repeated2', number=100000)) | |
print(timeit.timeit('first_non_repeated3("asdhfas;kjdfhlakj")', setup='from __main__ import first_non_repeated3', number=100000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment