Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
Added palindromes function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZandercraftGames committed Mar 2, 2021
1 parent b86dba4 commit 22dca8a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
8 changes: 8 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ def test_palindrome():
assert functions.palindrome("Racecar", True) == (True, 'racecaR')
assert functions.palindrome("never a foot too far even", False) is True
assert functions.palindrome("potato", True) == (False, 'otatop')


# Test for the functions.palindromes() function
def test_palindromes():
assert functions.palindromes("racecar") == (1, ['racecar'])
assert functions.palindromes("Racecar") == (1, ['Racecar'])
assert functions.palindromes("I like racecar madam and a Racecar is good") == (5, ['I', 'racecar', 'madam', 'a',
'Racecar'])
2 changes: 1 addition & 1 deletion zcscommonlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from zcscommonlib.functions import month, typedinput
from zcscommonlib.functions import month, typedinput, palindrome, palindromes
from zcscommonlib.zcsmath import great_circle
23 changes: 20 additions & 3 deletions zcscommonlib/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def typedinput(typein: str, inputmsg: str, errormsg: str):
# Function logic
while True:
try:
# Grab user's input and try to convert it to the format provided.
# Grab user's input and try to convert it to the format provided
if typein == "str":
userin = str(input(inputmsg))
elif typein == "int":
Expand Down Expand Up @@ -100,12 +100,29 @@ def palindrome(inputstr: str, isword: bool) -> [bool, str]:
# Check if the string in reverse is the same as the normal string
if formattedstr[::-1].lower() == formattedstr.lower():
if isword:
return True, formattedstr[::-1]
return True, formattedstr[::-1] # TODO: Make compatible with sentences in future release.
else:
return True
# Return false if the string is not a palindrome.
else:
if isword:
return False, formattedstr[::-1]
return False, formattedstr[::-1] # TODO: Make compatible with sentences in future release.
else:
return False


def palindromes(inputstr: str) -> (int, [str]):
"""
Takes an input, in the form of a string, and checks how many palindromes are in it. Also returns an array of those
palindromes.
:param inputstr: an input to check in the form of a string
:return: (int - number of palindromes, [str, str2...] - palindromes)
"""
words = str.split(inputstr) # Split the input into an array of the words in it
palindrome_array = []
# Cycle through all words provided
for i in words:
if i[::-1].lower() == i.lower():
palindrome_array.append(i) # Add palindromes to the array
return len(palindrome_array), palindrome_array

0 comments on commit 22dca8a

Please sign in to comment.