Python3 "The Sword" problem: "encountered odd move to the front, encountered even move to the last"

Source: Internet
Author: User

[This article is from the Sky Cloud-owned blog Park]

I've done two solutions, one using Python built-in functions, and the other for customizing numbers-related classes and functions.

" "Solution 1: Take advantage of the list-related built-in functions in Python" "defMove_numbers (Numbers:list, new_numbers=[]):     whileLen (Numbers) >0:ifNumbers[0]% 2 = =0:number=numbers.pop (0) new_numbers.append (number)Else: number=numbers.pop (0) new_numbers.insert (0, number)Print(new_numbers)" "Solution 2: Customizing classes and functions related to numbers" "classNumbers:header=None Tail=None Numbers=NoneclassNumber :def __init__(self, value): Self.value=value Self.next=Nonedef __init__(Self, *args): Self.numbers= Tuple ([Numbers.number (ARG) forArginchargs]) Self.install (self.numbers)defInstall (self, args:tuple): forIinchRange (len (args)): number=Args[i]ifi = = 0 andLen (args) > 1: Number.next= args[i + 1] Self.header= NumberifI > 0 andI < Len (args)-1: Number.next= args[i + 1]            ifi = = Len (args)-1 andLen (args) > 1: Self.tail= NumberdefShow (self): Subs=[] Index=Self.header whileindex:subs.append (index.value) Index=Index.nextPrint(Subs)defMove (self): index=Self.header New_header=None forIinchRange (len (self.numbers)): _next=Index.nextifIndex.value% 2 = =0:index.next=None Self.tail.next=Index Self.tail=IndexifIndex.value% 2! = 0 andI! =0:ifNew_header:index.next=New_header New_header=IndexElse: New_header=Index New_header.next=Self.header Self.header=New_header Index=_next" "title: Encountered odd move to most front encountered even move to last face" "if __name__=='__main__':    #Solution 1numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] move_numbers (numbers)#Solution 2Numbers = Numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 0) numbers.move () numbers.show ( )

Python3 "The Sword" problem: "encountered odd move to the front, encountered even move to the last"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.