Print Phrases with Prime size from a Sentence

[ad_1]

View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given a string S, the duty is to print all phrases with prime size within the given string.

Examples:

Enter: S = “It is a python programming language”
Output: is
programming
Rationalization: Size of is is 2 and size of programming is 11 each are primes

Enter: S = “You’re utilizing geeksforgeeks”
Output: You
are
utilizing
geeksforgeeks

Strategy: To resolve the issue comply with the under steps:

  • First cut up the given string to get an array of house separated strings.
  • Begin traversing the phrases from left to proper.
    • Calculate the size of every phrase.
    • If the size is prime, then print the phrase.

Under is the implementation of the above method:

Python3

  

from math import sqrt

  

def isPrime(x):

    prime = 0

    if(x > 1):

        for i in vary(2, int(sqrt(x)) + 1):

            if (x % i == 0):

                prime = 1

                break

        if (prime == 0):

            return True

        else:

            return False

    else:

        return False

          

def printPrimeLenWords(s):

    

    

    s = s.cut up()

    for i in s:

        

        

        

        if isPrime(len(i)):

            print(i)

  

if __name__ == '__main__':

    st = "It is a python programming language"

      

    

    printPrimeLenWords(st)

Time complexity: O(n * sqrt(x)), the place n is the variety of phrases within the given sentence and x be the size of the most important string.
Auxiliary Area: O(1)

[ad_2]

Leave a Reply