exam 2 Flashcards
print(f”{asd}”)
formatted strings contain replacement fields, {}
print(“/n”)
calls to print on next line
Write a Python program that asks a user to input a string. Then clear all the white spaces present in that string and output it back to the user.
user_input = input(“Enter your string: “)
result = “”
for char in user_input:
if char != ‘ ‘:
result += char
print(result)
if a string is a palindrome
user_input = input(“Enter a string: “)
if len(user_input) < 3:
print(“False”)
else:
user_input_lower = user_input.lower()
if user_input_lower == user_input_lower[::-1]: print("True") else: print("False")
write a function that removes all vowels from string
def remove_vow(word):
result = “”
vowels = ‘aeiouyAEIOUY’
for c in word:
if c not in vowels:
result += c
return result
print(remove_vow(“Hello”))
password validator. must be at least 8 characters long, one lower case, one uppercase and one number, one special character, and no space or tab
def pwdlength(password):
return len(password) >= 8
def containslower(w):
for c in w:
if ord(‘a’) <= ord(c) <= ord(‘z’):
return True
return False
def containsupper(w):
for c in w:
if ord(‘A’) <= ord(c) <= ord(‘Z’):
return True
return False
def containsnumber(w):
for c in w:
if ord(c) >= ord(‘0’) and ord(c) <= ord(‘9’):
return True
return False
def specialcharacters(w):
for c in w:
if c in “#@$”:
return True
return False
def nospace(w):
for c in w:
if c in “ “:
return True
return False
def secrequirement(password):
return containslower(password) and containsupper(password) and containsnumber(password)
def valid(password):
return pwdlength(password) and secrequirement(password) and specialcharacters(password) and nospace(password)
def get_password():
password = “”
while not valid(password):
password = input(“Enter a password”)
return password
print(get_password())
password validator. must be one special character, and no space or tab
def specialcharacters(w):
for c in w:
if c in “#@$”:
return True
return False
def nospace(w):
for c in w:
if c in “ “:
return False
return True
def valid(password):
return specialcharacters(password) and nospace(password)
def get_password():
password = “”
while not valid(password):
password = input(“Enter a password”)
return password
print(get_password())
password validator. must be one uppercase and one number
def containsupper(w):
for c in w:
if ord(‘A’) <= ord(c) <= ord(‘Z’):
return True
return False
def containsnumber(w):
for c in w:
if ord(c) >= ord(‘0’) and ord(c) <= ord(‘9’):
return True
return False
def secrequirement(password):
return containsupper(password) and containsnumber(password)
def valid(password):
return secrequirement(password)
def get_password():
password = “”
while not valid(password):
password = input(“Enter a password”)
return password
print(get_password())
password validator. must be at least 8 characters long and have one lower case
def pwdlength(password):
return len(password) >= 8
def containslower(w):
for c in w:
if ord(‘a’) <= ord(c) <= ord(‘z’):
return True
return False
def secrequirement(password):
return containslower(password)
def valid(password):
return pwdlength(password) and secrequirement(password)
def get_password():
password = “”
while not valid(password):
password = input(“Enter a password”)
return password
print(get_password())
use ASCII to check without using a string
def containsupper(w):
for char in w:
if ord(char) >= ord(‘A’) and ord(char) <= ord(‘Z’):
return True
return False
print(containsupper(“ere”))
write a program that tests a sorting function sort(l) and returns a list
check if each item is less than or greater than the next item
def verifysorted(inputlist):
for i in range(len(inputlist)-1):
if inputlist[i] > inputlist[i+1]:
return False
return True
print(“sorted: “, verifysorted([1,2,3]))
find a way to reduce extremely out sorted list
break the list into chunks. organize those chunks
def verifysorted(inputlist):
for i in range(len(inputlist)-1):
if inputlist[i] > inputlist[i+1]:
return False
return True
def verifyrecursive(l, start, end):
if start >= end:
return True
elif start+1 == end:
return l[start] <= l[end]
return verifyrecursive(l, start, (start+end)//2 and verifyrecursive(l, start + end)//2, end)
print(“Sorted recursive:”, verifyrecursive([1,2,3,4,5], 0, 4))
find distance between two strings of same length
def ham_distances(s1, s2):
if len(s1) != len(s2):
raise valueError(“String must be equal length”)
distance = sum(ch1 != ch2 for ch1, ch2 in zip (s1,s2))
return distance
string1 = input(“enter word:”)
string2 = input(“enter word:”)
if len(string1) != len(string2):
print(“not equal”)
else:
distance = ham_distances(string1, string2)
print(distance)
compute the square root of a number using newton method
def square_root(N, epsilon=1e-10):
guess = N / 2.0
while True: new_guess = 0.5 * (guess + N / guess) if abs(new_guess - guess) < epsilon: break guess = new_guess return guess
N = float(input(“Enter a positive number to find its square root: “))
sqrt_N = square_root(N)
print(“The square root of”, N, “is approximately:”, sqrt_N)
s1 = “spam”
s2 = “ni!”
“The Knights who say, “ + s2
The Knights who say ni!