Skip to main content

Posts

Showing posts with the label numbers

Python - Parsing or Regular Expression or Regex or RE (search, split, findall)

🐍  Parsing is way too simple in python. Basic parsing is done using the library re (Regular Expression). TRY ON JUPYTER NOTEBOOK Following should be the first line to start using functionalities of Regular Expressions: import re re.search(pattern, text) import re text = 'This is a string with term1, but it does not have the other term.' pattern = 'term1' print(re.search(pattern, text)) This will search 'term1' in the text and return Details, since it found it in the text. import re text = 'This is a string with term1, but it does not have the other term.' pattern = 'term2' print(re.search(pattern, text)) This will search 'term2' in the text and return 'None', since it didn't find it. re.search(pattern, text).start() It return the position of the first character of the pattern you are searching in the text. import re text = 'This is a string with term1, but it does not have the other term...

Python - Get the Next Prime

🐍  Have the program in Python to find prime numbers until the user chooses to stop asking for the next one. Solution import math def getprimes():     primes = [2]     num = 3          while True:         isprime = True         numsqrt = int(math.sqrt(num)+1)         for i in primes:             if i<=numsqrt:                 if (num%i==0):                     isprime = False                     break             else:                 break         if isprime:             primes.append(num)             yield num         num += 2 if __name__==...