homeworkhelp111

Code

Nov 16th, 2021 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. def CheckHash(text, n):
  2.     '''
  3.    This function returns True if every n-th character in the
  4.    string text is the character #; False otherwise.
  5.    If n is less than 1 then return False, as well if text is an empty string OR
  6.    it has less than n characters then also return False.
  7.    
  8.    For example, if text = "ab#cd#ef#" and n = 3 then the function
  9.    will return True.
  10.    If text="ab#cd#ef" and n = 3 then the function returns True.
  11.    If text="ab#cd$ef#" and n = 3 then the function returns False.
  12.    More test cases listed below in main function along with
  13.    their expected output (inside comments)
  14.    '''
  15.     match = True
  16.  
  17.     if(n == 0 or n > len(text) or len(text)==0):
  18.         match = False
  19.     else:
  20.         for i in range(0,len(text)):
  21.             if((i+1)%n == 0 and not text[i] == '#'):
  22.                 match = False
  23.                 break
  24.     return match
  25.  
  26. def main():
  27.     print(CheckHash("ab#cd#ef#", 3)) ## True
  28.     print(CheckHash("ab#cd#ef", 3)) ## True
  29.     print(CheckHash("ab#cd$ef#", 3)) ## False
  30.     print(CheckHash("a#b#c#d", 2)) ## True
  31.     print(CheckHash("a#b#c#d", 1)) ## False
  32.     print(CheckHash("a#b#c#d", 0)) ## False
  33.     print(CheckHash("", 10)) ## False
  34.     print(CheckHash("a#b#c#d", 3)) ## False
  35.     print(CheckHash("a#b#c#d", 10)) ## False
  36.     print(CheckHash("##$###$#", 2)) ## True
  37.  
  38. main()
  39.  
Add Comment
Please, Sign In to add comment