# [Tutor] My shot at the partitioning problem

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 18 Aug 2001 02:18:40 -0700 (PDT)

```On Sat, 18 Aug 2001, Roeland Rengelink wrote:

> One note on your code. You have:
>
> def stringCrossover(s1, s2, probability=0.5):
>     """A simple crossover operator on strings s1 and s2."""
>     for i in range(len(s1)):
>         if random.random() < probability:
>             return s1[:i] + s2[i:]
>     return s1
>
> ie.
>
> 50 % s1[:0]+s2[0:]
> 25 % s1[:1]+s2[1:]
> 12.5% s1[:2]+s2[2:]
> etc.
>
> Is that really what you want?

Oh no!  You're right: As it's written, the crossover function is biased
towards crossing string over near the beginning of the string!  That's
definitely not what I had in mind: it would be better if it could cross
over anywhere with equal probability.

> I thought more something like:
>
> def stringCrossover(s1, s2):
>     """A simple crossover operator on strings s1 and s2."""
>     i = random.randrange(len(s1))
>     return s1[:i]+s2[i:]

Yes, your version of stringCrossover makes much more sense.  I believe I
was writing the string mutating function at the same time as