110 likes | 193 Views
SOLUTIONS TO THE QUESTIONS DISCUSSED DURING THE FIRST REVIEW SESSION. COSC 1306 Fall 2013. First question. Main memory Graphical user interface. Second and third questions. Algorithmic approach: Try all flavors in sequence. It always works. Heuristics:
E N D
SOLUTIONS TO THE QUESTIONS DISCUSSED DURINGTHE FIRST REVIEW SESSION COSC 1306Fall 2013
First question • Main memory • Graphical user interface
Second and third questions • Algorithmic approach: • Try all flavors in sequence. It always works. • Heuristics: • Picking the tub that has the least ice cream. • Trying a a few likely winners.
Fourth question • We cannot compare a string to a number
Fifth Question • It will not work • Missing column athe end of the if
Sixth question • Will loop forever • Should be:i= 5while i> 0 :i= i -1 or I -= 1 • Use for I in range(…) instead
Seventh question • a = 7.5 • b = 2 • c = 3.0 • d = False
Eighth question """ Eighth Question""" first = input("Enter your first name: ") second = input("Enter your last name: ") print("Hello " + first[0] + ". " + second + "!")
Ninth question from math import sqrt def norm(a, b) : return sqrt(a*a + b*b) print(norm(3, 4)) will print 5.0
Tenth question from math import sqrt number = int(input("Enter an integer: ")) s= "This number is prime" biggest = int(sqrt(number)) for i in range(2, biggest + 1) : if (number % i == 0) : s = "This number is not prime" print(s) This solution wastes cycles: we should exit the loop as soon as we find out that the number is not prime!
Eleventh question • Something like: s = “Cannot drink alcohol”if birthyear < 1995 : s = “ Can drink alcohol”elifbirthyear == 1995 : if birthmonth < 9 : s = “ Can drink alcohol”elifbirthmonth == 9 and birthday >= 25 : s = “ Can drink alcohol”