#๐Ÿ”’ Make the code run faster

210 messages ยท Page 1 of 1 (latest)

elfin breach
#

I have this code who work well but is there a way to make my code go faster ? ```py
def count_number_of_letter_in_a_word(word):
return sum(letter.isalpha() for letter in word.lower())

def count_occurrences_in_text(word, text):
SplitString = text.lower().replace("."," ").split()
WordOccurences = 0
if " " in word and len(word) > 1 :
return(text.lower().count(word.lower()))

for words in SplitString:
    if " " not in word:
        if word.lower() in words and len(word) > 1 and count_number_of_letter_in_a_word(word) == count_number_of_letter_in_a_word(words) and "'" not in words:
            WordOccurences+=1
        elif word.lower() in words and len(word) > 1 and count_number_of_letter_in_a_word(word) == count_number_of_letter_in_a_word(words) and text[0] == "'" and text[-1] == "'":
            WordOccurences+=1
        elif word.lower() == words:
            WordOccurences+=1

return WordOccurences
wary hearthBOT
#

@elfin breach

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quasi skiff
#

Your variable names leave a lot to be desired

#

But I highly suspect this is suboptimal

#

Also, it's not clear what the intent is

quick sequoia
#

count_occurrences_in_text could just be text.count(word)

quasi skiff
#

And then later they're comparing the length of the alpha chars

elfin breach
#

Let me send yout the whole code

#

here it is

quasi skiff
#

That... Doesn't help at all.

#

Can you explain the goal of the program?

#

The assignment perhaps?

elfin breach
#

Ok I need to pass all the unit test

#

the goal is just to count the number of time a word is on a string

#

but since there is different case I can't just do a .count()

#

it won't work

#

and I need to solve all the test as fast as possible

dusty wolf
quasi skiff
#

I don't think your code currently accomplishes that

elfin breach
#

it should be even faster

#

count function won't work well for some cases

quasi skiff
elfin breach
elfin breach
dusty wolf
elfin breach
#

and there is several of them where it doesn't work with just a "count"

#

I know that the part that goes slow is this py for words in SplitString: if " " not in word: if word.lower() in words and len(word) > 1 and count_number_of_letter_in_a_word(word) == count_number_of_letter_in_a_word(words) and "'" not in words: WordOccurences+=1 elif word.lower() in words and len(word) > 1 and count_number_of_letter_in_a_word(word) == count_number_of_letter_in_a_word(words) and text[0] == "'" and text[-1] == "'": WordOccurences+=1 elif word.lower() == words: WordOccurences+=1 but I don't know how to do the same withouth using list looping

dusty wolf
# elfin breach yeah

idk what you're on because it works for me

>>> def cnt(word, text):
...     return text.lower().count(word.lower())
...
>>> cnt('I', 'Georges is my name and I like python. Oh ! your name is georges? And you like Python!\nYes is is true, I like PYTHON\nand my name is GEORGES')
10
>>>
#

or do you mean it should be 2

elfin breach
#

it should be 2

dusty wolf
#

ok, reason about why it should be 2

elfin breach
#

I guess that we should only look for the word "I" and not word that have I like "is or like" etc

#

And I think the count function search for the letter I in any word that got it

dusty wolf
#

o I c

#

what about, you split the sentence into words, then just check each word to see if it's an exact match

elfin breach
#

Like there there is only 1 I

#

but for python there is 3

dusty wolf
#

or if a word is separated by spaces, instead of matching "I", why not match " I "

elfin breach
#

Was going to say that

#

let me try it

dusty wolf
#

and cover some edge cases like "I like apples." by adding a ' ' to the beginning & end of the string

elfin breach
#

I need now to replace every special character with " "

dusty wolf
#

if so, you can use a loop to change the original string until all special characters have become ' '

elfin breach
#

Only said that ...

#

Like there you see that maley is not inside the string

dusty wolf
#

well just don't replace "'" with ' '

elfin breach
#

because it's in a word

#

Well the count technique won't work

#

because I'm "hardcoding" result for each case that doesn't work

#

it's even worse than the one I did at first

dusty wolf
elfin breach
#

first result

#

then know I will have other problem with the rest

dusty wolf
#

what's your code right now

elfin breach
#

it doesn't even work lol

dusty wolf
# elfin breach

that's not hardcoding it, that's just removing punctuations

dusty wolf
elfin breach
#

What do you mean by edgecase?

dusty wolf
dusty wolf
dusty wolf
# elfin breach yeah

and I proposed a fix already, see above

by adding a ' ' to the beginning & end of the string

#

and another edge case might be something like

>>> ' I I I '.count(' I ')
2
# .count() doesn't count overlaps, so
# ' I I I '
#  ^^^
#      ^^^
# those are the 2 it counts
```to fix that you can replace every `' '` with `'  '`
elfin breach
#

code py def count_occurrences_in_text(word, text): text = text.lower() punctuations = [".", ",", ";", ":", "!", "?", "...", "ยซ", "ยป", "โ€œ", "โ€", "(", ")", "[", "]", "{", "}", "'", "-", "-", "*", "#", "/", "\\", "&", "@"] for char in punctuations: text = text.replace(char, '') if "george" in text: print("New string after removing punctiation " , text) return (text.lower().count(word.lower() + " "))

elfin breach
#

oh

#

that ? text = " " + text + " "

#

Fixed

#

like this

#
def count_occurrences_in_text(word, text):
    text = text.lower()
    text = " " + text + " "
    punctuations = [".", ",", ";", ":", "!", "?", "...", "ยซ", "ยป", "โ€œ", "โ€", "(", ")", "[", "]", "{", "}", "'", "-", "-", "*", "#", "/", "\\", "&", "@", "\n"]
    for char in punctuations:
        text = text.replace(char, ' ')
    return (text.lower().count(word.lower() + " "))
    ```
#

but new problem

dusty wolf
#

word.lower() + " "

elfin breach
elfin breach
#

Did it

#

now fails here:

dusty wolf
elfin breach
#

yeah fixed it

#

I know the problem

#

it's because it's the first word

dusty wolf
#

you didn't include " in your special characters

elfin breach
#

and we are doing " " + text + " "

dusty wolf
#

you included these 2 โ€œ โ€ which idk what those are tbh

elfin breach
#

We should do like if it's first word then just text + " " else " " + text + " "

elfin breach
dusty wolf
elfin breach
#

passed way more test

#

with your technique

dusty wolf
elfin breach
dusty wolf
#

ah, it's getting into the annoying af territory I see
why do I feel like I've seen this exact task before actually (I specifically rmb seeing "Reflexion Mirror" somewhere)

elfin breach
#

yep

#

it's a "." problem

#

but if I remove "." from the list of char the first test failed ...

dusty wolf
#

I'm thinking if there's a way so you don't have to rewrite again due to stupid test cases

elfin breach
#

I may have found something

#

maybe we should also remove special char from the word too

#

let me try

dusty wolf
elfin breach
#

well passed 1 test

dusty wolf
#

" # regard ' as text:"
nice one, that was a fucking lie

elfin breach
#

๐Ÿ˜‚ ๐Ÿ˜‚ ๐Ÿ˜‚

dusty wolf
#

and also the parameter word is also a lie at this point because you're given entire sentences now

elfin breach
#

yep

#

looking into a solution for linguist

dusty wolf
#

I'm assuming you can't import anything?

#

if you can, this task's a job for re

dusty wolf
elfin breach
#

that's all they said

dusty wolf
# elfin breach

if I were you, I'd ignore that and just get something working first

elfin breach
#

Find a solution

dusty wolf
# elfin breach

and also this statement might be completely false if you only need a few regexes to solve it, but a lot of native str operations if you didn't re

elfin breach
#

punctuations = [".", ",", ";", ":", "!", "?", "...", "ยซ", "ยป", "โ€œ", "โ€", "(", ")", "[", "]", "{", "}", "*", "#", "/", "\", "&", "@", "\n" ,'"',"'''"]
just added ''' to the list

#

and it worked

#

New problem

dusty wolf
#

e.g.

for char in punctuations:
    text = text.replace(char, ' ')
```most likely slower than
```py
re.sub(f'[{punctuations}]', '', text)
```because you were looping a bunch of times and making a lot of strings
elfin breach
dusty wolf
elfin breach
#

I you think I wil do it

#

anything faster is better

dusty wolf
#

like the entire problem is

  • for text like apple. you should ignore .
  • for text like bapple, that's not an apple
  • for text like my c.v. to, you should not ignore . (because it's part of the word)
#

so my idea is if you just cooked up a regex that's
<detect start of word><the word you're supposed to match><any remaining special characters>then everything's solved

elfin breach
#

yes

#

Like this idea

dusty wolf
#

well let's get regex-ing then

  • start of word is probably just ' ' (again you can add a ' ' to the text for edge cases like I like apples.)
  • any remaining special characters that can be done easily with character sets [] and *
    e.g. [abc] is a regex that matches either a b or c; a* is a regex that matches a 0 or 1 time
elfin breach
#

Wait

#

All the test work with this ```py

def count_occurrences_in_text(word, text):
text = text.lower()
text = " " + text + " "
punctuations = [".", ",", ";", ":", "!", "?", "...", "ยซ", "ยป", "โ€œ", "โ€", "(", ")", "[", "]", "{", "}", "*", "#", "/", "\", "&", "@", "\n" ,'"',"'''","''","__"]
for char in punctuations:
text = text.replace(char, ' ')
word = word.replace(char, ' ')
if word.lower() == "linguist":
print("sentences: " , text)
return (text.lower().count(" " + word.lower() + " "))
```

#

Now will just transform all of this into a regex

dusty wolf
elfin breach
#

But isn't what we have done to pass the code similar to "hardcoding"?

elfin breach
dusty wolf
#

well well but the regex will actually work w/o hardcoding

#

hopefully

elfin breach
#

I hope

elfin breach
dusty wolf
#

like what if you just counted the regex fr' {word}[{punctuations}]*'

elfin breach
#

you can't have a generic function

#

but I hope the regex will do the work

elfin breach
#

forgot how to regex don't do it that often

dusty wolf
#

well that wouldn't pass for '''linguist ig

elfin breach
#

thanks

dusty wolf
#

right, ignorecase

#

re.findall(..., flags=re.IGNORECASE)

elfin breach
#

Dumb

#

forgot one thing

#

didn't redeclare the ponctuation list

dusty wolf
#

hm

elfin breach
#

I think my new code is good

#

it go 10 times faster than the previous one

#

Thanks for you help

dusty wolf
# elfin breach Thanks for you help

maybe this regex works?

import re, string
from string import punctuation
def cnt(word, text):
    matches = re.findall(fr"[{punctuation} ]{word}[{punctuation} ]", ' ' + text.replace(' ', '  ') + ' ', re.IGNORECASE)
    return len(matches)
elfin breach
#

well

#

yeah

#

you texted me 2 minutes after I sended the code

#

but I will send it

dusty wolf
#

oh

elfin breach
#

there is no limitation

dusty wolf
elfin breach
#

it does even faster than my code:

dusty wolf
elfin breach
#

so it's the same speed ?

dusty wolf
elfin breach
#

let me try a larger groupe of test

dusty wolf
#

re.sub(f'[{punctuations}]', '', text) removes all punc characters at once, probably faster than the loop

elfin breach
#

it's the same speed

#

because It loop trough a list of 9 elem

#

so it's not that big

dusty wolf
elfin breach
#

thanks

#

let's hope they like this code better

dusty wolf
#

I mean you can just tell them that you tried both (re and no re) and the former was faster, despite the task saying otherwise

elfin breach
#

yeah

#

Will say find another way to make the code

#

and they will have 2 code to see

#

maybe they will be like he tried even after something faster

dusty wolf
#

gl to you

elfin breach
#

thanks

elfin breach
#

Worked wellll

dusty wolf
#

ey yo nice
good luck to you

elfin breach
#

thanks

wary hearthBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.