STRINGS
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
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