[英]What kind of algorithm would I need to compute a name that equals a number?
For instance A =1, B = 2 ... Z = 26 The name is auto generated and must equal lets say 200 with the sum of the letters.例如 A =1, B = 2 ... Z = 26 该名称是自动生成的,并且必须等于 200 加上字母的总和。 eg例如
A = 1
AB = 3 etc
What steps would be taken in creating a function to create an array of auto generated strings that has a value of 200?在创建一个函数来创建一个值为 200 的自动生成的字符串数组时,将采取哪些步骤?
How would this be done in Python?这将如何在 Python 中完成?
You want to generate a string under some constraints.您想在某些约束下生成一个字符串。 And this is kind of optimisation problem.这是一种优化问题。 However we don't need to hire any machine learning here.然而,我们不需要在这里雇佣任何机器学习。
One possible solution might look like this (sorry, no python, just pseudo code)一种可能的解决方案可能如下所示(抱歉,没有 python,只有伪代码)
name = ""
#initialize name variable name = ""
#初始化名称变量place = selectPlaceWhereToInsertNextCharacter(name)
char = selectNextRandomCharacter()
name
putting char
in place
更新name
将char
的place
Comments:注释:
selectNextRandomCharacter
must select next character which value is less than remaining space in built so far name.函数selectNextRandomCharacter
必须选择下一个字符,该字符的值小于构建的到目前为止名称中的剩余空间。 Let's say your current name is "zzz"
so the value of it is 174
.假设您当前的名称是"zzz"
因此它的值为174
。 Next letter you select can not be greater than 200 - 174
.您选择的下一个字母不能大于200 - 174
。 Othewise you will overflow.否则你会溢出。 To do this - just select next integer within appropriate range and map it to char.为此 - 只需选择适当范围内的下一个整数并将其映射到字符。selectPlaceWhereToInsertNextCharacter(name)
it just selects where you will put next character. selectPlaceWhereToInsertNextCharacter(name)
它只是选择您将放置下一个字符的位置。 For example if name is "pawel"
there are 6 plces where you could mix in new letter.例如,如果名称是"pawel"
则有 6 个可以混合新字母的位置。 This is because I assumed that name must be quite random.这是因为我认为该名称必须非常随机。sum(map(lambda x: ord(x)-64, name))
可以使用这样的 python sum(map(lambda x: ord(x)-64, name))
检查 name 的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.