Git Product home page Git Product logo

permutations's Introduction

Permutations

Author: Eric Latham

Email: [email protected]

Description

This repo holds my solution to the problem of generating string permutations in Python without the help of the itertools library.

The goal is to take a string as input and output a set of permutations equivalent to the following:

from typing import Set
from itertools import permutations

def perms(s: str) -> Set[str]:
  return set("".join(x) for x in permutations(s))

Note that the above code is included here.

Brainstorming Process

  1. Solve the trivial case (n<2).
  2. Solving the simplest non-trivial case (n=2).
  3. Keep stepping up (by incrementing n) until the pattern becomes clear.

In this exercise, I discovered that the natural solution is recursive.

Example Thought Process

Let s = abc.
- n = 3
- output length = 3! = 3*2*1 = 6

Group permutations based on starting character:
- Permutations starting with a:
  - a + x for x in the permutations of the other characters in s (bc)
    - a + bc = abc
    - a + cb = acb
- Perms starting with b:
  - b + x for x in the permutations of the other characters in s (ac)
    - b + ac = bac
    - b + ca = bca
- Perms starting with c:
  - c + x for x in the permutations of the other characters in s (ab)
    - c + ab = cab
    - c + ba = cba

With all groups combined into one set, you have the answer:
{abc, acb, bac, bca, cab, cba}

General Solution

This was surprisingly difficult to come up with prior to going through the brainstorming process, but after the brainstorming process, it was very easy.

This solution is also extremely slow, but that's the nature of the algorithm (factorial time!).

Nonetheless, permutations seem to pop up in many places.

I wonder what methods exist for improving it...

permutations's People

Contributors

eolatham avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.