On Mon, Jan 4, 2016 at 12:34 PM, Pooja Bhalode <poojabhalod...@gmail.com> wrote:
> Hi, I wanted to check if this program can be used to merge the lists > together and sort them. This seems to work, but i wanted to check if there > are drawbacks in writing it in this manner. > > > My solution: > > > def linear_merge(list1, list2): > > for num in list2: > > list1.append(num) > > > > list1.sort() > > This could be more easily done with list1.extend(list2) list1.sort() > > > # +++your code here+++ > > return list1 > > > > Whereas, their code is a bit different, I have posted it here. > > > def linear_merge(list1, list2): > > > > result = [] > > # Look at the two lists so long as both are non-empty. > > # Take whichever element [0] is smaller. > > while len(list1) and len(list2): > > if list1[0] < list2[0]: > > result.append(list1.pop(0)) > > else: > > result.append(list2.pop(0)) > > > # Now tack on what's left > > result.extend(list1) > > result.extend(list2) > > return result > > > I don't think this code will work if the original lists are unsorted to begin with. You should give some sample data and your results for both methods > > Can you please tell me if there is a problem in the first code? > > Thank you. > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com/stats/birthdays _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor