简体   繁体   中英

What Can I do for sorting algorithm with my number language?

What Can I do for sorting algorithm for my number language?

khmer_number = [០, ១, ២,​ ៣​, ៤, ៥, ៦, ៧, ៨, ៩] # unicode utf-8 for khmer language number

internatonal_number = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

And I have a function sorting by using Naive sort,Quick sort,Bubble sort ,

Now for example with:

# -*- coding: utf-8 -*-
naive_sort(ls):
    while ls:
       e = min(ls)
       ls.remove(e)
       yield e

If I have a list

ls = [4, 2, 5, 3, 7, 0]
naive_sort[ls]
output
[0,2,3,4,5,7]

Anybody have an idea example if I have a list but in my number (khmer language)

ls = [៤, ៤, ៦, ៥, ៧, ០]​  #= here is correspod this list ls = [4, 2, 5, 3, 7, 0]
naive_sort[ls]
output
[០,២,៣,៤,៥,៦] //<=>[0,2,3,4,5,7]

How Can I implement the algorithm,so I can applied for my number language as well,?

My number unicode (khmer language ) start from 17E0 - 17E9 in unicode chat

(http://unicode.org/charts/PDF/U1780.pdf)

thanks

Looks like you're holding your number values in a string. In that case I would suggest that you think about converting those strings to numbers, sorting, and then converting back to string representation.

numberList = map(khmer_to_int, myList)
numberList.sort()
myList = map(int_to_khmer, numberList)

where youre two functions "int_to_khmer" and "khmer_to_int" are defined by you. I wish I could help more but I don't know khmer language spec in reguards to utf-8.

If you represent your chars as unicode just using sorted() will sort them the correct way (that is as long as you want them sorted after their unicode values):

list = ['\u17E2', '\u17E4', '\u17E0']
print(list)
print(sorted(list))       # sorted

If you want a more specific implementation just provide your own key function that takes one value and returns a key that is used to sort:

def sortop(val):
    ''' Implement however you want the values sorted.'''
    dict = {'\u17E0' : 0, '\u17E1' : 1, '\u17E2' : 2, '\u17E3' : 3, '\u17E4' : 4}
    return dict[val]

if __name__ == '__main__':
    list = ['\u17E2', '\u17E4', '\u17E0']
    print(list)
    print(sorted(list, key=sortop)) 

I don't know about Python so I can't give you a code sample.

In case the ordering of unicode values matches the desired ordering, you can simply use this value to do the sorting.

If this isn't the case you can use a map which maps your numbers to the matching desired order, and use this map in your sorting algorithm.

To make the sorting algorithm generic you could choose to pass a custom comparer which can compare two values with eachother.

UPDATE:

Perhaps this HowTo can give you some ideas. (Or is simply the solution you are looking for. ;p)

I take it that you have a good reason to implement your own sort algorithm, rather than using the .sort() method or sorted() builtin? If that's the case, then you could do the following:

def naive_sort(ls, key):
    ls = [(key(i), i) for i in ls]
    while ls:
       e = min(ls)
       ls.remove(e)
       yield e[1]

Now you can use naive_sort like this:

khmer_number = [u'\u17e0', u'\u17e1', u'\u17e2', u'\u17e3', u'\u17e4', u'\u17e5', u'\u17e6', u'\u17e7', u'\u17e8', u'\u17e9']
international_number = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
number_map = dict(zip(khmer_number, international_number))

ls = [khmer_number[n] for n in [4, 2, 5, 3, 7, 0]]
print list(naive_sort(ls, key=lambda x: number_map[x]))

Update

I'm not sure if this is what you're asking, but if you have the number in a string, you can make a list of it like so:

>>> list(u'\u17E0\u17E2\u17E3\u17E4\u17E5\u17E7')
[u'\u17e0', u'\u17e2', u'\u17e3', u'\u17e4', u'\u17e5', u'\u17e7']

Given a such a list, you can convert it back into a string with

s = ''.join([u'\u17e0', u'\u17e2', u'\u17e3', u'\u17e4', u'\u17e5', u'\u17e7'])

If print s gives you a bunch of escape values, then you might need to do something like print s.encode('utf-8') , replacing utf-8 with whatever charset your locale uses.

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