Recursive Selection Sort python -


there recursive selection sort in upcoming question has done.

def selsort(l):     """     sorts l in-place.     pre: l list.     post: l sorted list same elements; no return value.     """                      l1 = list("sloppy joe's hamburger place") vl1 = l1  print l1    # should be: """['s', 'l', 'o', 'p', 'p', 'y', ' ', 'j', 'o', 'e', "'", 's', ' ', 'h', 'a', 'm', 'b', 'u', 'r', 'g', 'e', 'r', ' ', 'p', 'l', 'a', 'c', 'e']"""  ret = selsort(l1)  print l1    # should """[' ', ' ', ' ', "'", 'a', 'a', 'b', 'c', 'e', 'e', 'e', 'g', 'h', 'j', 'l', 'l', 'm', 'o', 'o', 'p', 'p', 'p', 'r', 'r', 's', 's', 'u', 'y']""" print vl1   # should """[' ', ' ', ' ', "'", 'a', 'a', 'b', 'c', 'e', 'e', 'e', 'g', 'h', 'j', 'l', 'l', 'm', 'o', 'o', 'p', 'p', 'p', 'r', 'r', 's', 's', 'u', 'y']"""  print ret   # should "none" 

i know how using key → l.sort(key=str.lower). question wants me extract maximum element, instead of minimum, .append(...) on recursively sorted sublist.

if appreciate it.

so. understand problem?

let's @ asked do:

extract maximum element, instead of minimum, .append(...) on recursively sorted sublist.

so, following things:

1) extract maximum element. understand "extract" means here? know how find maximum element?

2) recursively sort sublist. here, "the sublist" consists of else after extract maximum element. know how recursion works? call sort function again sublist, relying on sorting. after all, purpose of function sort lists, supposed work, right? :)

3) .append() maximum element onto result of sorting sublist. should not require explanation.

of course, need base case recursion. when have base case? when can't follow steps written. when happen? well, why happen? answer: can't extract maximum element if there no elements, because there no maximum element extract.

thus, @ beginning of function check if passed empty list. if were, return empty list, because sorting empty list results in empty list. (do see why?) otherwise, go through other steps.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -