Worksheet Questions (for worksheet.py program) (1) Explain what every line of the program worksheet.py is doing. (2) Then answer the following questions: ---------------------------------------------------------- What is the difference between "input" and "raw_input" ? What happens when you type: gurobi> answer = input('Your answer is: ') and you repond to "Your answer is: " by typing the following: (1) 4, (2) 4.5, (3) Yes, (4) "Yes", (5) 'Yes'. What is the type of "answer"? Same question for gurobi> answer = raw_input('Your answer is: ') ---------------------------------------------------------- If you had an LP/IP/QP that involved a three dimensional array initialized via: model = Model('sudoku') vars = model.addVars(n,n,n, vtype=GRB.BINARY, name='G') what line of code would you add to impose the following (English) condition: "for all i and j (from 0 to n-1), the sum over all k (from 0 to n-1) of vars[i,j,k] equals 1" ? [Hint: look at sudoku.py] ---------------------------------------------------------- If you issue the Python (or Gurobi) command: >>> a = 'Joel, how are you today?' what are the values of: (1) a[0:9] , (2) a.split(',') , (3) a.split(' ') , (4) a.find('you') , (5) a.find('el') , (6) a.find('ow') , (7) 'are' in a , (8) a.count('o') ---------------------------------------------------------- When you type "help()", what is the difference between running in the Gurobi shell and in Python? In Python, type "help()", then "topics", then "STRINGS". What is the difference had you typed "STRINGMETHODS" ---------------------------------------------------------- What does the following Python/Gurobi program do (this assumes you have a file named "data8x4_vary.txt" in the run directory) ? file = open("data8x4_vary.txt") for i in range(4): print( 'Line number ' + str(i) + ' is: ' + file.readline() ) file.close() What do we use the function "str" in the "str(i)" above ? If you wanted to start counting lines from 1 to 4 instead of 0 to 3, how would you modify the above code (there is more than one way) ? ---------------------------------------------------------- After you issue the command import random What do the following commands do? (1) random.random() , (2) random.randint(10,19) , (3) random.uniform(-1.0,1.0) , (3) random.uniform(-1,1) , (4) random.choice( ['happy','wise','focused','kind'] ) ---------------------------------------------------------- What is the difference between the print statements in the two loops below? import random def forecast() : print('Today in Vancouver we will have ' + random.choice( ['showers.' , 'rain.' , 'rain storms.', 'rain with thunder.' , 'rain with possible flooding.' , 'rain and hail.' , 'severe rain with flood warnings.' ] ) ) for i in range(7,14) : print('November ' + str(i) + ':'), forecast() for i in range(7,14) : print('November %2g :' % i), forecast()