1

I’m making a program to try to show the genotypic ratio of a dihybrid punnet square. I’m up to the point where I can create an answer, but I need to sort it.

Example= answer_one = “aTAt”

I need to sort it so that the answer becomes “AaTt”. Basically the method of sorting needs to make the string alphabetical, and put the capital letter before the lowercase one.

When I try to sort it, it becomes aAtT, and that’s exactly what I don’t want.

Narfee
  • 21
  • 3
  • And that is exactly the default sorting method, as it uses the ASCII to compare literals. Your request seems to be a more custom sorting. – Ahmad Sattout Mar 22 '19 at 23:34

1 Answers1

1

You need to define a custom sort. Here is one way:

import functools

def cmp(a,b):
    if a.upper() < b.upper():
        return -1
    elif a.upper() == b.upper() and a < b:
        return -1
    elif a == b:
        return 0
    else:
        return 1

def punnet_sort(s):
    return ''.join(sorted(s,key=functools.cmp_to_key(cmp)))

For example,

>>> punnet_sort('aTAt')
'AaTt'
John Coleman
  • 51,337
  • 7
  • 54
  • 119