4 Public Domain, WTFNMFPL Public Licence
6 from pyscipopt
import Model
7 from pprint
import pformat
as pfmt
28 sdic = {0:
"even", 1:
"odd"}
32 Prints if a value is even/odd/neither per each value in a example list
34 This example is made for newcomers and motivated by:
35 - modulus is unsupported for pyscipopt.scip.Variable and int
36 - variables are non-integer by default
37 Based on this: #172#issuecomment-394644046
40 number: value which parity is checked
43 sval: 1 if number is odd, 0 if number is even, -1 if neither
49 assert number == int(round(number))
58 x = m.addVar(
"x", vtype=
"I", lb=
None, ub=
None)
59 n = m.addVar(
"n", vtype=
"I", lb=
None)
60 s = m.addVar(
"s", vtype=
"B")
73 assert m.getStatus() ==
"optimal"
74 boolmod = m.getVal(s) == m.getVal(x)%2
77 print(
"%*s: %d" % (fmtlen, v,m.getVal(v)))
78 print(
"%*d%%2 == %d?" % (fmtlen, m.getVal(x), m.getVal(s)))
79 print(
"%*s" % (fmtlen, boolmod))
84 print(
"%*d is %s" % (fmtlen, xval, sstr))
85 except (AssertionError, TypeError):
86 print(
"%*s is neither even nor odd!" % (fmtlen, number.__repr__()))
93 if __name__ ==
"__main__":
95 If positional arguments are given:
96 the parity check is performed on each of them
98 the parity check is performed on each of the default example values
101 from ast
import literal_eval
as leval
105 values = sys.argv[1:]
108 values = example_values
110 fmtlen = max([len(fmt)
for fmt
in pfmt(values,width=1).split(
'\n')])
114 except (ValueError, SyntaxError):