final Flashcards
mylist[[1,2,3], [4,5,6], [7,8,9]]
mylist[0]
[1,2,3]
mylist[1][0]
4
mylist[mylist[0][1][2]]
9
[i for i in range(5)]
0, 1, 2, 3, 4
[[i] for i in range(5)]
[0][1][2][3][4]
[[i]*2 for i in range(5)]
[0,0], [1,1], [2,2], [3,3], [4,4]
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
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)
write function that replaces vowel of a string
def replace_vowel(aString, aChar):
result = ‘ ‘
vowels = ‘aeiouyAEIOUY’
for c in vowels:
result = result += a Char
else:
result = result += c
return result
write function minposition that, given a list of integers, returns the position of the last occurence of the smallest element
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
manipulate a dictionary ‘D’ and then iterates over its keys and values to print them in a formatted manner
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
suppose we have n by m matrices, A and B, write a function to compute A+B
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
for i in range(5):
print(i, 2**i)
0 1
1 2
2 4
3 8
4 16
what is value of 4!
factorial of 4 is 24
4.0 /10.0 + 3.5 * 2
7.4
10%4 + 6 /2
5