exam 2 Flashcards

1
Q

print(f”{asd}”)

A

formatted strings contain replacement fields, {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

print(“/n”)

A

calls to print on next line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A

user_input = input(“Enter your string: “)
result = “”
for char in user_input:
if char != ‘ ‘:
result += char
print(result)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

if a string is a palindrome

A

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")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

write a function that removes all vowels from string

A

def remove_vow(word):
result = “”
vowels = ‘aeiouyAEIOUY’
for c in word:
if c not in vowels:
result += c
return result
print(remove_vow(“Hello”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

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())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

password validator. must be one special character, and no space or tab

A

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())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

password validator. must be one uppercase and one number

A

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())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

password validator. must be at least 8 characters long and have one lower case

A

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())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

use ASCII to check without using a string

A

def containsupper(w):
for char in w:
if ord(char) >= ord(‘A’) and ord(char) <= ord(‘Z’):
return True
return False
print(containsupper(“ere”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

write a program that tests a sorting function sort(l) and returns a list

A

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]))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

find a way to reduce extremely out sorted list

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

find distance between two strings of same length

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

compute the square root of a number using newton method

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

s1 = “spam”
s2 = “ni!”

“The Knights who say, “ + s2

A

The Knights who say ni!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

s1 = “spam”
s2 = “ni!”

NI!

A

s2[0:2].upper()

17
Q

s1 = “spam”
s2 = “ni!”

spm

A

s1[0:2] + s1[-1]

18
Q

a certain CS professor gives 5 point quizzes that are graded on the scale 5-A, 4-B, 3-C, 2-D, 1-F, 0-F. Write a program that accepts a quiz score as an input and prints out the corresponding grade

A

c

19
Q

write a program that allows the user to type in a phrase and then outputs the acronym for that phrase. Note: the acronym should be all uppercase, even if the words in the phrase are not capitalized

A

def get_acronym(phrase):
words = phrase.split()
acronym = ‘‘.join(word[0].upper() for word in words)
return acronym

user_input = input(“Enter a phrase: “)

acronym = get_acronym(user_input)
print(f”The acronym for {user_input} is {acronym}”)

20
Q

write a program to print the lyrics of the song ‘old mcdonald’. your program should print the lyrics for two different animals

A

def old_mcdonald(animal, sound):
print(“Old McDonald had a farm, E-I-E-I-O”)
print(f”And on that farm he had a {animal}, E-I-E-I-O”)
print(f”With a {sound} {sound} here, and a {sound} {sound} there”)
print(f”Here a {sound}, there a {sound}, everywhere a {sound} {sound}”)
print(f”Old McDonald had a farm, E-I-E-I-O\n”)

old_mcdonald(“cow”, “moo”)
old_mcdonald(“duck”, “quack”)