Skip to content

Commit 49bbb89

Browse files
committed
Small follow-up fixes after 1.4 and added tests for using the project as a library as opposed to from the CLI
1 parent 64f877a commit 49bbb89

File tree

4 files changed

+370
-9
lines changed

4 files changed

+370
-9
lines changed

BirthdayProblem.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,13 @@ def solveForP(dOrDLog, nOrNLog, isBinary, isCombinations, method):
262262
dOrDLog is the base-2 logarithm of the number of members, s, of a set from which we should generate the sample set d ((2^s)!).
263263
'''
264264
@staticmethod
265-
def solveForN(dOrDLog, p, isBinary, isCombinations):
265+
def solveForN(dOrDLog, p, isBinary, isCombinations, method):
266266
_DecimalContext.reset() # reset to initial context precision
267267
_BirthdayProblemInputHandler.sanitize(dOrDLog, None, p, isBinary, isCombinations, False, False, False, False)
268268
d, dLog, _, _, p = _BirthdayProblemInputHandler.setup(dOrDLog, None, p, isBinary, isCombinations)
269269
if(dLog is None):
270270
raise SolverException(SolverErrorCode.DLOG_NOT_CALCULATED)
271-
return _BirthdayProblemSolverChecked.birthdayProblemInv(d, dLog, p, isBinary)
271+
return _BirthdayProblemSolverChecked.birthdayProblemInv(d, dLog, p, method, isBinary)
272272

273273

274274
class _BirthdayProblemSolverChecked:
@@ -550,7 +550,7 @@ def __birthdayProblemInvExact(d, p):
550550
pInv = _DecimalContext.ctx.subtract(_DecimalFns.ONE, p)
551551
n = _DecimalFns.ONE
552552
currentPInv = _DecimalContext.ctx.divide(_DecimalContext.ctx.subtract(d, n), d)
553-
while currentPInv > pInv:
553+
while _DecimalFns.isGreaterThan(currentPInv, pInv):
554554
n = _DecimalContext.ctx.add(n, _DecimalFns.ONE)
555555
currentPInv = _DecimalContext.ctx.multiply(
556556
currentPInv,
@@ -950,7 +950,7 @@ def parse(args = None):
950950

951951
parser.add_argument('d', metavar=('D'), type=str, nargs=1, help='Input number D, the total number of unique items, or a number from which the total number of unique items can be derived, in the set we are sampling from.')
952952
parser.add_argument('-n', '--samples', metavar=('N'), type=str, help='Input number N, the number of samples, or a number from which the number of samples can be derived from, taken from the full set of D items. When present the probability P of at least one non-unique item among the samples will be calculated. Requires one of flags -e, -s, -t or -a to determine the desired precision(s) of the calculation.')
953-
parser.add_argument('-p', '--probability', metavar=('P'), type=str, help='Input number P in [0.0, 1.0], the the probability of at least one non-unique item among the samples. When present the needed number of samples N will be calculated. Requires one of flags -e, -t or -a to determine the desired precision(s) of the calculation.')
953+
parser.add_argument('-p', '--probability', metavar=('P'), type=str, help='Input number P in [0.0, 1.0], the probability of at least one non-unique item among the samples. When present the needed number of samples N will be calculated. Requires one of flags -e, -t or -a to determine the desired precision(s) of the calculation.')
954954

955955
parser.add_argument('-b', '--binary', dest='binary', action='store_const', const=True, default=False, help='Inputs D and N are seen as exponents with base 2')
956956
parser.add_argument('-c', '--combinations', dest='combinations', action='store_const', const=True, default=False, help="Input D is seen as a number of unique items in a set from which we can yield N! (factorial) different members for the resulting set of unique items from which we sample. The calculation of D! uses Stirling's approximation which might introduce a small error responsible for the difference in results with the same input with and without -c flag.")
@@ -1094,12 +1094,13 @@ def solveText(d, dLog, n, nLog, p, pPercent, isBinary, isStirling, isTaylor, isE
10941094
def solveJson(d, dLog, n, nLog, p, pPercent, isBinary, isStirling, isTaylor, isExact, isAll, prec, isMainProgram):
10951095
res = {'results': {}}
10961096

1097+
lastMethodUsed = None
1098+
10971099
# do the calculations based on mode
10981100
if p is not None:
10991101
dText, pText = _BirthdayProblemTextFormatter.headerTextBirthdayProblemInvNumbers(dLog if isBinary else d, p, pPercent, isBinary, prec)
11001102
res['d'] = dText
11011103
res['p'] = pText
1102-
lastMethodUsed = None
11031104
for (method, included) in [(_BirthdayProblemSolver.CalcPrecision.EXACT, isExact), (_BirthdayProblemSolver.CalcPrecision.TAYLOR_APPROX, isTaylor)]:
11041105
if (included or isAll) and lastMethodUsed != _BirthdayProblemSolver.CalcPrecision.TRIVIAL:
11051106
try:
@@ -1122,7 +1123,6 @@ def solveJson(d, dLog, n, nLog, p, pPercent, isBinary, isStirling, isTaylor, isE
11221123
dText, nText = _BirthdayProblemTextFormatter.headerTextBirthdayProblemNumbers(dLog if isBinary else d, nLog if isBinary else n, isBinary, prec)
11231124
res['d'] = dText
11241125
res['n'] = nText
1125-
lastMethodUsed = None
11261126
for (method, included) in [(_BirthdayProblemSolver.CalcPrecision.EXACT, isExact), (_BirthdayProblemSolver.CalcPrecision.STIRLING_APPROX, isStirling), (_BirthdayProblemSolver.CalcPrecision.TAYLOR_APPROX, isTaylor)]:
11271127
if (included or isAll) and lastMethodUsed != _BirthdayProblemSolver.CalcPrecision.TRIVIAL:
11281128
try:

0 commit comments

Comments
 (0)