简体   繁体   中英

How can i make my function run even more faster?

Hey guys I was doing a problem-solve I did a lot of changes to my first function to hit the time limit

but this is really my last idea I don't know how to make it faster than right now

from timeit import default_timer as timer
from datetime import timedelta

start = timer()

testList = ['hello']
wordList = ['hello','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe']
def words_with_given_shape(words,shape):
    itemList = 'abcdefghijklmnopqrstuvwxyz'
    def wordShape(word):
        tempS = []
        termil = len(word)-1
        
        for inx,elem in enumerate(word):
            orderAl = itemList.index(elem)
            if inx < termil:
                orderNl = itemList.find(word[inx+1])
                if orderAl > orderNl:
                    tempS.append(-1)
                if orderAl < orderNl:
                    tempS.append(1)
                if orderNl == orderAl:
                    tempS.append(0) 
        return tempS
    def checkWord(words):
        res = []
        for i in words:
            if wordShape(i)==shape:
                res.append(i)
        return res
    return checkWord(words)
print(words_with_given_shape(wordList,  [-1, 1, 0, 1,1,1,-1]))
print(words_with_given_shape(wordList,  [-1, 1, 0, 1]))
print(words_with_given_shape(wordList,  [-1, 1]))
print(words_with_given_shape(wordList,  [-1, 1, 0, 1,1, 0, 1,-1,1]))
print(words_with_given_shape(testList,  [-1, 1, 0, 1]))
end = timer()
print(timedelta(seconds=end-start))

its currently giving me this time 0:00:00.001272

but it seems the tester needs even faster than this because at test 12 it fails due to execute time limit

So basically can you guide me to make the words_with_given_shape function even more optimized?

*** EDIT ***: I forget to tell the question is it gives list of words and shape of word the shape is like [0,1,1,-1] it means 0 eq 1 character is after the current character in alpha order -1 character is before the current character in alpha order

So for Hello its [-1, 1, 0, 1]

The answer is find all words in word list which shape is same as shape arg

Try this:

def words_with_given_shape(words, shape):
    return [word for word in words
            if (len(shape) == len(word) - 1 and
                all(c1 == c2 if s == 0 else (c1 > c2 if s == -1 else c1 < c2)
                    for c1, c2, s in zip(word[:-1], word[1:], shape)))]

It's much more compact (just one line) and you don't need to generate the shape of every single word (which is useless in this case, as you can use the provided one)!

By reformating your code like shown and using List Comprehensions you're 100 ms faster on 10000 iterations:

  • init:
from timeit import default_timer as timer
from datetime import timedelta
start = timer()
testList = ['hello']
wordList = ['hello','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe']
  • v1:
def checkcond(x, i, word, itemList):
    _, __ = itemList.index(x),  itemList.find(word[i+1])
    return -1 if _ > __ else 1 if _ < __ else 0

def wordShape(word, itemList):
    return [checkcond(x, i, word, itemList) for i, x in enumerate(word[:-1])]

def words_with_given_shape(words,shape):
    itemList = 'abcdefghijklmnopqrstuvwxyz'
    return [x for x in words if wordShape(x, itemList)==shape]
  • v2: (faster on google colab)
def words_with_given_shape(words,shape):
    itemList = 'abcdefghijklmnopqrstuvwxyz'
    def checkcond(x, i, word):
        _, __ = itemList.index(x),  itemList.find(word[i+1])
        return -1 if _ > __ else 1 if _ < __ else 0
    def wordShape(word):
        return [checkcond(x, i, word) for i, x in enumerate(word[:-1])]
    return [x for x in words if wordShape(x)==shape]
  • timecheck:
def timecheck():
    for x in range(10000):
        words_with_given_shape(wordList,  [-1, 1, 0, 1,1,1,-1])
        words_with_given_shape(wordList,  [-1, 1, 0, 1])
        words_with_given_shape(wordList,  [-1, 1])
        words_with_given_shape(wordList,  [-1, 1, 0, 1,1, 0, 1,-1,1])
        words_with_given_shape(testList,  [-1, 1, 0, 1])
    return timedelta(seconds=timer()-start)
print(timecheck())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM