I'm trying to simulate a simple evolution thing using binary code. Each individual in the "gene pool" has their own unique string in binary code.
In order to have them mate, I need to take half of one string, and the other half of another and merge them. The problem is, I don't know how to split a string in two equal halves, and they are totally random.
Help would be appreciated, thanks!
I'm trying to simulate a simple evolution thing using binary code. Each individual in the "gene pool" has their own unique string in binary code.
In order to have them mate, I need to take half of one string, and the other half of another and merge them. The problem is, I don't know how to split a string in two equal halves, and they are totally random.
Help would be appreciated, thanks!
Share Improve this question asked Jul 7, 2015 at 22:15 XeliphoXelipho 471 silver badge5 bronze badges 5-
1
find the length of the string. Then calc the middle
num = length/2
. Then find the substring from 0 to num and from num to len(str). – Avinash Raj Commented Jul 7, 2015 at 22:17 - Substring could help you. – Alp Commented Jul 7, 2015 at 22:17
- Oh, I was not aware that that is how sub-strings work. – Xelipho Commented Jul 7, 2015 at 22:18
- Can the length be an odd number? That is the only special case. – user3735633 Commented Jul 7, 2015 at 22:18
- How would you guarantee equal halves though, say the length was even – Oliver Barnwell Commented Jul 7, 2015 at 22:18
2 Answers
Reset to default 4Well I would get the total length of the string and get the first half by doing
myString.slice(firstIndex, secondIndex)
I would make the firstIndex
be 0
and the secondIndex
equal to myString.length / 2
. That will return the first half of the string.
The second half of the string would be
myString.slice(myString.length / 2, myString.length)
So, all together:
const partOne = myString.slice(0, myString.length / 2)
const partTwo = myString.slice(myString.length / 2, myString.length)
string half1 = value.Substring(0, value.length/2);
string half2 = value.Substring(value.length/2);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744708274a4589186.html
评论列表(0条)