diff --git a/examples/python/hidato_table.py b/examples/python/hidato_table.py index 0356230c5a..ebc617d51e 100644 --- a/examples/python/hidato_table.py +++ b/examples/python/hidato_table.py @@ -52,7 +52,7 @@ def BuildPairs(rows, cols): def main(unused_argv): for model in range(1, 7): print - print '----- Solving problem %i -----' % model + print('----- Solving problem %i -----' % model) print Solve(model) @@ -123,7 +123,7 @@ def Solve(model): r = len(puzzle) c = len(puzzle[0]) - print 'Initial game (%i x %i)' % (r, c) + print('Initial game (%i x %i)' % (r, c)) PrintMatrix(puzzle) # @@ -168,15 +168,15 @@ def Solve(model): solver.EndSearch() - print 'num_solutions:', num_solutions - print 'failures:', solver.Failures() - print 'branches:', solver.Branches() - print 'wall time:', solver.WallTime() + print('num_solutions:', num_solutions) + print('failures:', solver.Failures()) + print('branches:', solver.Branches()) + print('wall time:', solver.WallTime()) def PrintOneSolution(positions, rows, cols, num_solution): """Print a current solution.""" - print 'Solution %i:' % num_solution + print('Solution %i:' % num_solution) # Create empty board. board = [] for unused_i in range(rows): @@ -184,7 +184,7 @@ def PrintOneSolution(positions, rows, cols, num_solution): # Fill board with solution value. for k in range(rows * cols): position = positions[k].Value() - board[position / cols][position % cols] = k + 1 + board[position // cols][position % cols] = k + 1 # Print the board. PrintMatrix(board) @@ -194,12 +194,13 @@ def PrintMatrix(game): rows = len(game) cols = len(game[0]) for i in range(rows): + line = "" for j in range(cols): if game[i][j] == 0: - print ' .', + line += ' .' else: - print '% 2s' % game[i][j], - print + line += '% 3s' % game[i][j] + print(line) print diff --git a/examples/python/integer_programming.py b/examples/python/integer_programming.py index b9c2312251..8299c54dfe 100644 --- a/examples/python/integer_programming.py +++ b/examples/python/integer_programming.py @@ -58,25 +58,25 @@ def RunIntegerExampleCppStyleAPI(optimization_problem_type): def SolveAndPrint(solver, variable_list): """Solve the problem and print the solution.""" - print 'Number of variables = %d' % solver.NumVariables() - print 'Number of constraints = %d' % solver.NumConstraints() + print('Number of variables = %d' % solver.NumVariables()) + print('Number of constraints = %d' % solver.NumConstraints()) result_status = solver.Solve() # The problem has an optimal solution. assert result_status == pywraplp.Solver.OPTIMAL - print 'Problem solved in %f milliseconds' % solver.WallTime() + print('Problem solved in %f milliseconds' % solver.WallTime()) # The objective value of the solution. - print 'Optimal objective value = %f' % solver.ObjectiveValue() + print('Optimal objective value = %f' % solver.ObjectiveValue()) # The value of each variable in the solution. for variable in variable_list: - print '%s = %f' % (variable.name(), variable.SolutionValue()) + print('%s = %f' % (variable.name(), variable.SolutionValue())) - print 'Advanced usage:' - print 'Problem solved in %d branch-and-bound nodes' % solver.Nodes() + print('Advanced usage:') + print('Problem solved in %d branch-and-bound nodes' % solver.Nodes()) def Announce(solver, api_type): diff --git a/examples/python/knapsack.py b/examples/python/knapsack.py index 43137da0aa..929be3ddf1 100644 --- a/examples/python/knapsack.py +++ b/examples/python/knapsack.py @@ -46,7 +46,7 @@ def main(unused_argv): solver.Init(profits, weights, capacities) computed_profit = solver.Solve() - print 'optimal profit = ' + str(computed_profit) + '/' + str(optimal_profit) + print('optimal profit = ' + str(computed_profit) + '/' + str(optimal_profit)) if __name__ == '__main__': diff --git a/examples/python/linear_programming.py b/examples/python/linear_programming.py index f6c3c0dcb1..b63830d771 100644 --- a/examples/python/linear_programming.py +++ b/examples/python/linear_programming.py @@ -78,27 +78,27 @@ def RunLinearExampleCppStyleAPI(optimization_problem_type): def SolveAndPrint(solver, variable_list, constraint_list): """Solve the problem and print the solution.""" - print 'Number of variables = %d' % solver.NumVariables() - print 'Number of constraints = %d' % solver.NumConstraints() + print('Number of variables = %d' % solver.NumVariables()) + print('Number of constraints = %d' % solver.NumConstraints()) result_status = solver.Solve() # The problem has an optimal solution. assert result_status == pywraplp.Solver.OPTIMAL - print 'Problem solved in %f milliseconds' % solver.WallTime() + print('Problem solved in %f milliseconds' % solver.WallTime()) # The objective value of the solution. - print 'Optimal objective value = %f' % solver.ObjectiveValue() + print('Optimal objective value = %f' % solver.ObjectiveValue()) # The value of each variable in the solution. for variable in variable_list: - print '%s = %f' % (variable.name(), variable.SolutionValue()) + print('%s = %f' % (variable.name(), variable.SolutionValue())) - print 'Advanced usage:' - print 'Problem solved in %d iterations' % solver.Iterations() + print('Advanced usage:') + print('Problem solved in %d iterations' % solver.Iterations()) for variable in variable_list: - print '%s: reduced cost = %f' % (variable.name(), variable.ReducedCost()) + print('%s: reduced cost = %f' % (variable.name(), variable.ReducedCost())) for i, constraint in enumerate(constraint_list): print ('constraint %d: dual value = %f\n' ' activity = %f' % diff --git a/examples/python/pyflow_example.py b/examples/python/pyflow_example.py index c44bfcd5a8..b9ac7f3f73 100644 --- a/examples/python/pyflow_example.py +++ b/examples/python/pyflow_example.py @@ -21,7 +21,7 @@ from ortools.graph import pywrapgraph def MaxFlow(): """MaxFlow simple interface example.""" - print 'MaxFlow on a simple network.' + print('MaxFlow on a simple network.') tails = [0, 0, 0, 0, 1, 2, 3, 3, 4] heads = [1, 2, 3, 4, 3, 4, 4, 5, 5] capacities = [5, 8, 5, 3, 4, 5, 6, 6, 4] @@ -30,17 +30,17 @@ def MaxFlow(): for i in range(0, len(tails)): max_flow.AddArcWithCapacity(tails[i], heads[i], capacities[i]) if max_flow.Solve(0, 5) == max_flow.OPTIMAL: - print 'Total flow', max_flow.OptimalFlow(), '/', expected_total_flow + print('Total flow', max_flow.OptimalFlow(), '/', expected_total_flow) for i in range(max_flow.NumArcs()): - print 'From source %d to target %d: %d / %d' % ( + print('From source %d to target %d: %d / %d' % ( max_flow.Tail(i), max_flow.Head(i), max_flow.Flow(i), - max_flow.Capacity(i)) - print 'Source side min-cut:', max_flow.GetSourceSideMinCut() - print 'Sink side min-cut:', max_flow.GetSinkSideMinCut() + max_flow.Capacity(i))) + print('Source side min-cut:', max_flow.GetSourceSideMinCut()) + print('Sink side min-cut:', max_flow.GetSinkSideMinCut()) else: - print 'There was an issue with the max flow input.' + print('There was an issue with the max flow input.') def MinCostFlow(): @@ -49,7 +49,7 @@ def MinCostFlow(): Note that this example is actually a linear sum assignment example and will be more efficiently solved with the pywrapgraph.LinearSumAssignement class. """ - print 'MinCostFlow on 4x4 matrix.' + print('MinCostFlow on 4x4 matrix.') num_sources = 4 num_targets = 4 costs = [[90, 75, 75, 80], @@ -67,15 +67,15 @@ def MinCostFlow(): min_cost_flow.SetNodeSupply(num_sources + node, -1) status = min_cost_flow.Solve() if status == min_cost_flow.OPTIMAL: - print 'Total flow', min_cost_flow.OptimalCost(), '/', expected_cost + print('Total flow', min_cost_flow.OptimalCost(), '/', expected_cost) for i in range(0, min_cost_flow.NumArcs()): if min_cost_flow.Flow(i) > 0: - print 'From source %d to target %d: cost %d' % ( + print('From source %d to target %d: cost %d' % ( min_cost_flow.Tail(i), min_cost_flow.Head(i) - num_sources, - min_cost_flow.UnitCost(i)) + min_cost_flow.UnitCost(i))) else: - print 'There was an issue with the min cost flow input.' + print('There was an issue with the min cost flow input.') def main(unused_argv): diff --git a/examples/python/tsp.py b/examples/python/tsp.py index f9134bb179..ad2c74b0a6 100644 --- a/examples/python/tsp.py +++ b/examples/python/tsp.py @@ -62,9 +62,9 @@ class RandomMatrix(object): rand.seed(FLAGS.tsp_random_seed) distance_max = 100 self.matrix = {} - for from_node in xrange(size): + for from_node in range(size): self.matrix[from_node] = {} - for to_node in xrange(size): + for to_node in range(size): if from_node == to_node: self.matrix[from_node][to_node] = 0 else: @@ -107,7 +107,7 @@ def main(_): from_node = rand.randrange(FLAGS.tsp_size - 1) to_node = rand.randrange(FLAGS.tsp_size - 1) + 1 if routing.NextVar(from_node).Contains(to_node): - print 'Forbidding connection ' + str(from_node) + ' -> ' + str(to_node) + print('Forbidding connection ' + str(from_node) + ' -> ' + str(to_node)) routing.NextVar(from_node).RemoveValue(to_node) forbidden_connections += 1 @@ -115,7 +115,7 @@ def main(_): assignment = routing.SolveWithParameters(parameters, None) if assignment: # Solution cost. - print assignment.ObjectiveValue() + print(assignment.ObjectiveValue()) # Inspect solution. # Only one route here; otherwise iterate from 0 to routing.vehicles() - 1 route_number = 0 @@ -125,11 +125,11 @@ def main(_): route += str(node) + ' -> ' node = assignment.Value(routing.NextVar(node)) route += '0' - print route + print(route) else: - print 'No solution found.' + print('No solution found.') else: - print 'Specify an instance greater than 0.' + print('Specify an instance greater than 0.') if __name__ == '__main__': app.run() diff --git a/examples/tests/test_cp_api.py b/examples/tests/test_cp_api.py index 347c085faf..bd3adb8d54 100644 --- a/examples/tests/test_cp_api.py +++ b/examples/tests/test_cp_api.py @@ -6,19 +6,19 @@ def test_member(): solver = pywrapcp.Solver('test member') x = solver.IntVar(1, 10, 'x') ct = x.Member([1, 2, 3, 5]) - print ct + print(ct) def test_sparse_var(): solver = pywrapcp.Solver('test sparse') x = solver.IntVar([1, 3, 5], 'x') - print x + print(x) def test_modulo(): solver = pywrapcp.Solver('test modulo') x = solver.IntVar(0, 10, 'x') y = solver.IntVar(2, 4, 'y') - print x % 3 - print x % y + print(x % 3) + print(x % y) def main(): diff --git a/examples/tests/test_lp_api.py b/examples/tests/test_lp_api.py index ef3ab4af6d..0f972ea8c7 100644 --- a/examples/tests/test_lp_api.py +++ b/examples/tests/test_lp_api.py @@ -7,7 +7,7 @@ def Sum(arg): sum = 0; for i in arg: sum += i - print arg, sum + print(arg, sum) def test_sum_no_brackets(): Sum(x for x in range(10) if x % 2 == 0)