简体   繁体   中英

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. 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?

How would this be done in 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)

  1. name = "" #initialize name variable
  2. place = selectPlaceWhereToInsertNextCharacter(name)
  3. char = selectNextRandomCharacter()
  4. update name putting char in place
  5. If value of ```name`` is < 200, goto 2

Comments:

  • Function selectNextRandomCharacter must select next character which value is less than remaining space in built so far name. Let's say your current name is "zzz" so the value of it is 174 . Next letter you select can not be greater than 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. For example if name is "pawel" there are 6 plces where you could mix in new letter. This is because I assumed that name must be quite random.
  • value of name can be checked using such python sum(map(lambda x: ord(x)-64, name))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM