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

Functions Module

Zander edited this page Mar 15, 2021 · 14 revisions

How to Import

from zcscommonlib import functions

Month - functions.month()


Purpose

Takes an input of an integer and returns a string of the month of that number.

Args

num (int): The number of the month.

Returns

The month's name in string format.

Example

# Imports
from zcscommonlib import functions

# Main program logic
month = functions.month(5)

print(month)

# Output: May

Typed Input - functions.typed_input()


Purpose

Gets the input from a user, persisting until the input of the right type is received with an optional input message and error message.

Args

type_in (str): The type of input to require of the user (int, float, str, bool).
input_msg (str): The message to display to the user when getting input.
error_msg (str): The error to log to console when an input of the wrong type is given.

Returns

The input provided from the user in the desired format.

Example

# Imports
from zcscommonlib import functions

# Main program logic
userInput = functions.typed_input("int", "Please input an integer to multiply by 11: ", "This is not a valid integer.")
# User input: "Please input an integer to multiply by 11: 5"
print(str(userInput * 11))

# Output: 55

Palindrome - functions.palindrome()


Purpose

Takes an input, in the form of a string, and checks if it is a palindrome.

Args

input_str (str): A string you want to check.
is_word (bool): Is the value a single word?

Returns

Returns a boolean of if the word/string is a palindrome and a string of the word reversed (only if is_word is set to True) in the following format: (bool, str)

Example

# Imports
from zcscommonlib import functions

# Main program logic
inputWord = functions.typed_input("str", "Please provide a word to check if it is a palindrome: ", "This is not a valid input.")
# User input: "Please provide a word to check if it is a palindrome: Racecar"

isPalindrome, reversedStr = functions.palindrome(inputWord, True) == (True, 'racecaR')

print(inputWord + " in reverse is " + reversedStr + ".")
# Check if the word was a palindrome.
if isPalindrome:
    print("Therefore, your input is a palindrome.")
else:
    print("Therefore, your input is not a palindrome.")
# Output: Racecar in reverse is racecaR.
# Output: Therefore, your input is a palindrome.

Palindromes - functions.palindromes()


Purpose

Takes an input, in the form of a string, and checks how many palindromes are in it. Also returns an array of those palindromes.

Args

input_str (str): A string you want to check.

Returns

Returns an integer of how many palindromes are in the string and an array of all of the palindromes in the provided string in the following format: (int, [str, str2...])

Example

# Imports
from zcscommonlib import functions

# Main program logic
inputStr = functions.typed_input("str", "Please provide a sentence to check for palindromes: ", "This is not a valid input.")
# User input: "Please provide a sentence to check for palindromes: The racecar drives around madam as she talks to her mom."

palindrome_amount, palindromes = functions.palindromes(inputStr)

if palindrome_amount == 1:
    print("There is one palindrome in the sentence you provided. It is \"" + palindromes[0] + "\".")
elif palindrome_amount > 0:
    print("There are " + str(palindrome_amount) + " palindromes in the sentence you provided. They are \""
          + str(palindromes) + "\".")
else:
    print("There are no palindromes in that sentence.")

# Output: There are 3 palindromes in the sentence you provided. They are "['racecar', 'madam', 'mom']"

Random Value - functions.random_value()


Purpose

Takes an array and returns a random value from it.

Args

array (list): An array to retrieve the value from.

Returns

A random value from the array

Example

# Imports
from zcscommonlib import functions

# Main program logic
myArray = ['value1', 'value2', 'value3']
randomArrayValue = functions.random_value(myArray)

print(randomArrayValue)

# Output: value2