0% found this document useful (0 votes)
3 views

STRINGS

The document contains a collection of Python functions that perform various tasks such as checking if a chemical is a hydroxide, parsing product codes, evaluating password strength, checking for palindromes, counting digits and special characters, disemvoweling strings, calculating total digits, finding string distance, and evaluating arithmetic expressions. Each function is well-documented with parameters and return values specified. The functions demonstrate basic string manipulation, arithmetic operations, and conditional logic.

Uploaded by

ann cherian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

STRINGS

The document contains a collection of Python functions that perform various tasks such as checking if a chemical is a hydroxide, parsing product codes, evaluating password strength, checking for palindromes, counting digits and special characters, disemvoweling strings, calculating total digits, finding string distance, and evaluating arithmetic expressions. Each function is well-documented with parameters and return values specified. The functions demonstrate basic string manipulation, arithmetic operations, and conditional logic.

Uploaded by

ann cherian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

STRINGS

def is_hydroxide(chemical):
"""
-------------------------------------------------------
Determines if a chemical formula is a hydroxide.
Use: hydroxide = is_hydroxide(chemical)
-------------------------------------------------------
Parameters:
chemical - a chemical formula (str)
Returns:
hydroxide - True if chemical is a hydroxide (ends in 'OH'),
False otherwise (boolean)
-------------------------------------------------------
"""
if(chemical.count("OH") != 0):
result = True
else:
result = False
return result

def parse_code(product_code):
"""
-------------------------------------------------------
Parses a given product code. A product code has three parts:
The first three letters describe the product category
The next four digits are the product ID
The remaining characters describe the product's qualifiers
Use: pc, pi, pq = parse_code(product_code)
-------------------------------------------------------
Parameters:
product_code - a valid product code (str)
Returns:
pc - the category part of product_code (str)
pi - the id part of product_code (str)
pq - the qualifier part of product_code (str)
-------------------------------------------------------
"""
pc = product_code[0:3]
# use slicing to get next four letters of code
pi = product_code[3:7]
# similarly using slicing get all remaining letters of code
pq = product_code[7:]
return pc, pi, pq

def password_strength(password):
"""
-------------------------------------------------------
Checks the strength of a given password. A password is
strong if it contains at least eight characters, has a
combination of upper-case and lower-case letters, and
includes digits. Prints one or more of:
not long enough - if password less than 8 characters
no digits - if password has no digits
no upper case - if password has no upper case letters
no lower case - if password has no lower case letters
Use: password_strength(password)
-------------------------------------------------------
Parameters:
password - the password to be checked (str)
Returns:
None
------------------------------------------------------
"""
if len(password) < 8:
print("not long enough")
if not password.isdigit():
print("no digits")
if not password.isupper():
print("no upper case")
if not password.islower():
print("no lower case")
else:
print("Good password")
return

def is_palindrome(s):
"""
-----------------------------------------------------------------
Checks whether the given string is palindrome or not. A palindrome is
a string the reads the same forwards as backwards. Case is ignored.
Use: palindrome = is_palindrome(s)
-----------------------------------------------------------------
Parameters:
s - a string to be checked (str)
Returns:
palindrome - True if s is a palindrome, False otherwise (boolean)
-----------------------------------------------------------------
"""
palindrome = True
s = s.lower()
for i in range(len(s)):
if s[i] != s[len(s) - i - 1]:
palindrome = False

return palindrome

def digit_count(s):
"""
-------------------------------------------------------
Counts the number of digits in a string.
Use: count = digit_count(s)
-------------------------------------------------------
Parameters:
s - a string (str)
Returns:
count - the number of digits in s (int)
-------------------------------------------------------
"""
count = 0
for x in s:
if x.isdigit():
count += 1
return count

def count_special_chars(s):
"""
-------------------------------------------------------
Counts the number of special characters in s.
The special characters are: "#", "@", "$", "%", "&", "!".
Use: count = count_special_chars(s)
-------------------------------------------------------
Parameters:
s - a string (str)
Returns:
count - number of special characters in s (int)
------------------------------------------------------
"""
count = 0
count = s.count("#") + s.count("@") + s.count("$") + s.count("%") +
s.count("&") + \
s.count("!")
return count

def dsmvwl(string):
"""
-------------------------------------------------------
Disemvowels a string. Returns a copy of s with all the vowels
removed. Y is not treated as a vowel. Preserves case.
Use: out = dsmvwl(sstring)
-------------------------------------------------------
Parameters:
string - a string (str)
Returns:
out - string with its vowels removed (str)
-------------------------------------------------------
"""
s = string.lower()
out = ""
for char in s:
if char not in "aeiou":
out += char
return out

def total_digits(string):
"""
-------------------------------------------------------
Extracts and calculates the total of all digits in string.
Use: total = total_digits(string)
-------------------------------------------------------
Parameters:
s - a string (str)
Returns:
total - total of all the digits in string (int)
------------------------------------------------------
"""
total = 0

for i in string:
if i.isdigit() == True:
i = int(i)
total = total + i
return total

def str_distance(s1, s2):


"""
-------------------------------------------------------
Finds the distance between the s1 and s2. The distance between two
strings of the same length is the number of positions in the strings
at which their characters are different. If two strings are not the
same length, the distance is unknown (-1). If the distance is zero,
then two strings are equal.
Use: d = str_distance(s1, s2)
-------------------------------------------------------
Parameters:
s1 - first string (str)
s2 - second string (str)
Returns:
d - the distance between s1 and s2 (int)
------------------------------------------------------
"""

d = 0
count = 0
if(len(s1) != len(s2)):
d = -1
if(len(s1) == len(s2)):
for x in range(0, len(s1)):
if(s1[x] == s2[x]):
count = count
else:
count += 1
d = count
return d

def calculate(expr):
"""
-----------------------------------------------------------------
Treats expr as a math expression and evaluates it.
expr must have the following format:
operand1 operator operand2
operators are: +, -, *, /, %
operands are one-digit integer numbers
Return None if second operand is zero for division.
Use: result = calculate(expr)
-----------------------------------------------------------------
Parameters:
expr - an arithmetic expression to be calculated (str)
Returns:
result - The result of arithmetic expression (float)
-----------------------------------------------------------------
"""
result = 0
num1 = expr[0]
num2 = expr[4]
oper = expr[2]
if(oper == "+"):
result = float(num1) + float(num2)
if(oper == "-"):
result = float(num1) - float(num2)
if(oper == "*"):
result = float(num1) * float(num2)
if(oper == "/"):
if(num2 == "0"):
result = None
else:
result = int(num1) / int(num2)
if(oper == "%"):
if(num2 == "0"):
result = None
else:
result = int(num1) % int(num2)
return result

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy