D.V.N.Sarma wrote:
[snip recursive merge sort algorithm]
> Especially the statement
>
> v = (a[0] < b[0] and a or b).pop(0)
>
> gives a.pop(0), if a[0] < b[0] otherwise b.pop(0).
I believe this idiom was used before the ternary if statements were
introduced (in 2.5 I believe). In modern Python y
D.V.N.Sarma డి.వి.ఎన్.శర్మ, 31.08.2013 18:30:
> I have been searching for mergesort implimentations in python and came
> across this.
In case this isn't just for education and you actually want to use it, the
built-in sorting algorithm in Python (used by list.sort() and sorted()) is
a very fast me
On 2013-08-31 22:00, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> def merge(a, b):
> if len(a)*len(b) == 0:
> return a+b
Indentation in Python matters; if you're going to post code, you should
probably keep it.
> We have to look at the statement as
>
> v = ((a[0] < b[0] and a) or b).pop(0)
This is short
I have been searching for mergesort implimentations in python and came
across
this.
def merge(a, b):
if len(a)*len(b) == 0:
return a+b
v = (a[0] < b[0] and a or b).pop(0)
return [v] + merge(a, b)
def mergesort(lst):
if len(lst) < 2:
return lst
m = len(lst)/2
return merge(mergesort(lst[:m]), mer