DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Pyrex

Snippets Manager user avatar by
Snippets Manager
·
Aug. 01, 06 · · Code Snippet
Like (0)
Save
Tweet
499 Views

Join the DZone community and get the full member experience.

Join For Free
// description of your code here
Permute a list


cdef class Permute2:

    cdef int n, first
    cdef object lst

    def __init__(self, lst):
        self.lst = lst
        self.first = 0
        self.n = len(lst) - 1

    def __iter__(self):
        return self

    def __next__(self):
        cdef int j, l, k, x, y, z
        if self.first == 0:
            self.first = 1
            return self.lst
        if self.n == 1:
            return [self.lst[1], self.lst[0]]
        while 1:
            if self.lst[-2] < self.lst[-1]:
                self.lst[-2], self.lst[-1] = self.lst[-1], self.lst[-2]
            elif self.lst[-3] < self.lst[-2]:
                if self.lst[-3] < self.lst[-1]:
                    self.lst[-3], self.lst[-2], self.lst[-1] = self.lst[-1], self.lst[-3], self.lst[-2]
                else:
                    self.lst[-3], self.lst[-2], self.lst[-1] = self.lst[-2], self.lst[-1], self.lst[-3]
            else:
                j = self.n - 3
                if j < 0: raise StopIteration
                y = self.lst[j]
                x = self.lst[-3]
                z = self.lst[-1]
                while y >= x:
                    j = j - 1
                    if j < 0: raise StopIteration
                    x = y
                    y = self.lst[j]
                if y < z:
                    self.lst[j] = z
                    self.lst[j+1] = y
                    self.lst[self.n] = x
                else:
                    l = self.n - 1
                    while y >= self.lst[l]:
                        l = l - 1
                    self.lst[j], self.lst[l] = self.lst[l], y
                    self.lst[self.n], self.lst[j+1] = self.lst[j+1], self.lst[self.n]
                k = j + 2
                l = self.n - 1
                while k < l:
                    self.lst[k], self.lst[l] = self.lst[l], self.lst[k]
                    k = k + 1
                    l = l - 1
            return self.lst

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Pre-Commit Hooks DevOps Engineer Should Know To Control Kubernetes
  • What SREs Can Learn From the Atlassian Nightmare Outage of 2022
  • How Does the Database Understand and Execute Your Query?
  • Upload Files to AWS S3 in JMeter Using Groovy

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo