pythonpython-3.xoptimizationcode-organization

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


Solution

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

    from timeit import default_timer as timer
    from datetime import timedelta
    start = timer()
    testList = ['hello']
    wordList = ['hello','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe']
    
    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]
    
    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]
    
    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())