Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def CheckHash(text, n):
- '''
- This function returns True if every n-th character in the
- string text is the character #; False otherwise.
- If n is less than 1 then return False, as well if text is an empty string OR
- it has less than n characters then also return False.
- For example, if text = "ab#cd#ef#" and n = 3 then the function
- will return True.
- If text="ab#cd#ef" and n = 3 then the function returns True.
- If text="ab#cd$ef#" and n = 3 then the function returns False.
- More test cases listed below in main function along with
- their expected output (inside comments)
- '''
- match = True
- if(n == 0 or n > len(text) or len(text)==0):
- match = False
- else:
- for i in range(0,len(text)):
- if((i+1)%n == 0 and not text[i] == '#'):
- match = False
- break
- return match
- def main():
- print(CheckHash("ab#cd#ef#", 3)) ## True
- print(CheckHash("ab#cd#ef", 3)) ## True
- print(CheckHash("ab#cd$ef#", 3)) ## False
- print(CheckHash("a#b#c#d", 2)) ## True
- print(CheckHash("a#b#c#d", 1)) ## False
- print(CheckHash("a#b#c#d", 0)) ## False
- print(CheckHash("", 10)) ## False
- print(CheckHash("a#b#c#d", 3)) ## False
- print(CheckHash("a#b#c#d", 10)) ## False
- print(CheckHash("##$###$#", 2)) ## True
- main()
Add Comment
Please, Sign In to add comment