final Flashcards

1
Q

mylist[[1,2,3], [4,5,6], [7,8,9]]
mylist[0]

A

[1,2,3]

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

mylist[1][0]

A

4

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

mylist[mylist[0][1][2]]

A

9

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

[i for i in range(5)]

A

0, 1, 2, 3, 4

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

[[i] for i in range(5)]

A

[0][1][2][3][4]

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

[[i]*2 for i in range(5)]

A

[0,0], [1,1], [2,2], [3,3], [4,4]

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

write a function that takes user input for the actual time and alarm clock time. print whether the alarm time has passed or the difference in minutes if it hasn’t

A

def main():
acutal_time = int(input(“Enter the actual time: “))
alarm_clock = int(input(“Enter alarm clock time: “))

actual_hour = acutal_time//100
actual_minute = actual_time%100
actual_total_min = actual_hour * 60 + actual_minute

alarm_hour = alarm_clock//100
alarm_minute = alarm_clock%100
alarm_total_min = alarm_hour * 60 + alarm_minute

diff = alarm_total_min - alarm_total_min

if diff <= 0:
    print(" ")
else:
    print(diff)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

write function that replaces vowel of a string

A

def replace_vowel(aString, aChar):
result = ‘ ‘
vowels = ‘aeiouyAEIOUY’
for c in vowels:
result = result += a Char
else:
result = result += c
return result

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

write function minposition that, given a list of integers, returns the position of the last occurence of the smallest element

A

def minposition(intlist):
if len(intlist) == 0:
return -1

minval = intlist[0]
minindex = 0

for i in range(len(intlist)-1, -1, -1):
    if minval >= intlist[i]:
        minval = intlist[i]
        minindex = i
return minindex
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

manipulate a dictionary ‘D’ and then iterates over its keys and values to print them in a formatted manner

A

D = {“firstname”:”John”, “lastname”:”Doe”, “address”:”unknown”, “GPA”: 3.6}
D[“active”] = 10
for c in D:
print(“{}:{}”.format(c, D[c]))

result:
firstname:john
lastname:Doe
address:unknown
GPA:3.6
active:10

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

suppose we have n by m matrices, A and B, write a function to compute A+B

A

def addmatrix(A,B):
result = []
for i in range(len(A)):
newrow = []
for j in range(len(A[0])):
newrow.append(A[i][j] + B[i][j])
print(newrow)
result.append(newrow)
return result

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

for i in range(5):
print(i, 2**i)

A

0 1
1 2
2 4
3 8
4 16

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

what is value of 4!

A

factorial of 4 is 24

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

4.0 /10.0 + 3.5 * 2

A

7.4

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

10%4 + 6 /2

A

5

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

abs(4 -20//3)**3

A

8

17
Q

sqrt(4.5 - 5.0) + 7 * 3

A

11

18
Q

range(3, 10)

A

3, 4, 5, 6, 7, 8, 9ra

19
Q

range(4, 13, 3)

A

4, 7, 10

20
Q

range(15, 5, -2)

A

15, 13, 11, 9, 7

21
Q

range(5, 3)

A

error

22
Q

for i in range(1, 11):
print(i*i)

A

1, 4, 9, 16, 25, 36, 49, 64, 81, 100

23
Q

for i in range [1, 3, 5, 7, 9]:
print(i, “:”, i**3)
print(i)

A

1:1
3:27
5:125
7:343
9:729
9

24
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")
25
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())

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

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

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

29
Q

check if each item is less than or greater than the next item

A

def verifysorted(inputlist):
for i in range(len(inputlist)-1):
if inputlist[i] > inputlist[i+1]:
return False
return True

30
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

def grade_quiz(grade):
if grade == 5:
return ‘A’
elif grade == 1 or grade == 0:
return ‘F’
else:
return ‘invalid score’
grade_input = int(input(‘enter number score’))
if 0<= grade_input <= 5:
grade = grade_quiz(grade_input)
print(f”grade is {grade}”)

31
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}”)

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

33
Q

s1 = “spam”
s2 = “ni!”

spm

A

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

34
Q

s1 = “spam”
s2 = “ni!”

NI!

A

s2[0:2].upper()

35
Q

s1 = “spam”
s2 = “ni!”

“The Knights who say, “ + s2

A

The Knights who say ni!