Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.

Please note that the string does not contain any non-printable characters.

Example:

Input: "Hello, my name is John"
Output: 5

Difficulty: introductory

Starter code:

class Solution:
    def countSegments(self, s: str) -> int:
        

Solutions:
class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         return len(s.split())

class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         count = 0
         finished = True
         for c in s:
             if finished:
                 if c is " ": continue
                 else:
                     finished = False
                     count += 1
             else:
                 if c is " ":
                     finished = True
                 else:
                     continue
         return count


class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         return len(s.split())


class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         count = 0
         finished = True
         for c in s:
             if finished:
                 if c is " ": continue
                 else:
                     finished = False
                     count += 1
             else:
                 if c is " ":
                     finished = True
                 else:
                     continue
         return count


class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         return len(s.split())
             # lmao is me


class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         '''
         counter = 0
         wasspace = 1
         for char in s:
             if char == " ":
                 wasspace = 1
                 continue
             if wasspace == 1:
                 counter += 1
                 wasspace = 0
         return counter
         '''
         return len(s.split())

class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         c=0
         l=s.split(" ")
         for i in l:
             if i!="":
                 c+=1
         return c

class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         return len(s.split())

class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         if len(s)==0:
             return 0
         seen_character = False
         count = 0
         for char in s:
             if seen_character and char == ' ':
                 count += 1
                 seen_character = False
             elif seen_character == False and char != " ":
                 seen_character = True
         if seen_character:
             count += 1
         return count


class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         return len(s.split())

class Solution:
     def countSegments(self, s):
         """
         :type s: str
         :rtype: int
         """
         mylist = s.split()
         return len(mylist)