每次x更改我的sympy求和时,我如何生成另一个字符串?

问题描述 投票:0回答:1
from sympy import summation, Piecewise, symbols, Eq, Ge,solve, Le import sys import itertools import string import random sys.setrecursionlimit(300000000) x = symbols('x') # Create a dictionary to map numbers to characters def checksum1(args2): def generate_random_string(min_length, max_length): length = random.randint(min_length, max_length) characters = string.printable.strip() return ''.join(random.choices(characters, k=length)) #change this to generate a different random one for each x client = generate_random_string(60, 200) try: account = ord(client) return 1 except Exception: return 0 m = Piecewise( (checksum1(x), Eq(checksum1(x), 1)), (0, True) ) result = summation(m, (x, 1, 900)) print(result)

在代码中,我想在函数checksum1中每次使用其他随机字符串。它仅生成一个随机字符串,然后停止。 X的每个值都应具有不同的随机字符串,直到所有随机值都用尽。有人可以告诉我该怎么办吗?

具有代码的可运行版本

In [153]: # Create a dictionary to map numbers to characters
     ...: def checksum1(args2):
     ...:         def generate_random_string(min_length, max_length):
     ...:             length = random.randint(min_length, max_length)
     ...:             characters = string.printable.strip()
     ...:             str = ''.join(random.choices(characters, k=length))  #change this to generate a different random one for each x
     ...:             #print(str)
     ...:             return str
     ...:         client = generate_random_string(60, 100)
     ...:         print(client)
     ...:         try:
     ...:             account = ord(client)
     ...:             return 1
     ...:         except Exception:
     ...:             return 0
     ...:             

In [153]: 

In [154]: checksum1(x)
5G]7C(zqa<q|l2pHzOEc<ukA<sHM9R\uc+J')P'G~Hfq2F7]+/u/yLT^{]Z9<qvYL\YP
Out[154]: 0

In [155]: checksum1(x)
*IO\'37y:M*5c%,1*RN[>3Q#.#p<BsnFh\p-Y.=<AciA3cQ$U>U&2i557HhAF{SSOU(]4#/|E/rnIX]+N)V/q
Out[155]: 0

m
python random sympy
1个回答
0
投票

In [156]: m = Piecewise( ...: ...: (checksum1(x), Eq(checksum1(x), 1)), ...: (0, True) ...: ) /`yN6G#rF7hx:Kwc?V%c+/VFvh(Ib~8.8bL!R'pb{7^c`GvSOq25Fx|.YS,c[6E"a>f8:W 9(Cx*%sM&Am/|x4A(Su#4kK;z@YwV<^gnFH*uT)jqM_3Yk:5y>o6\s%YB@y&$%vLZYp In [157]: m Out[157]: 0 In [158]: type(m) Out[158]: sympy.core.numbers.Zero

summation

中不“重新恢复”,更不用说运行了

generate_random_string
函数。

summation(m, (x, 1, 3))
在您以前的问题时,我想知道您为什么要使用

sympy

没有此额外的代码,一个简单的列表理解测试多个随机字符串:
PieceWise

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.