GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Wiki
  • PSF Wiki
  • Jython Wiki
  • Python Wiki
  • Archive

This view

  • Original page
  • New address

GeezerVison is showing wiki.python.org — Easy As Py

View as Markdown

Easy As Py¶

Legacy Wiki Page

This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org.

This probably shouldn’t be the first example, but it’s a nice one:


My requirement was

I want a list of all ordered permutations of a given length of a set of tokens. Each token is a single character, and for convenience, they are passed as a string in ascending ASCII order.

For example

permute(“abc”,2)

should return  ["aa","ab","ac","ba","bb","bc","ca","cb","cc"] 

and permute(“13579”,3) should return a list of 125 elements

 ["111","113", ... ,"997","999"] 


Ask the experienced coder to code this up in the language of his or her choice. Then show them this (contributed by castironpi):

 def p(a,b):
        if not b: return ['']
        return [i+j for i in a for j in p(a,b-1)]

CategoryAdvocacy

Previous
Early 2to 3 Migrations
Next
Edit Python Code Chinese
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.