{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def next_permutation(a):\n",
    "    i = len(a) - 1\n",
    "    while a[i-1] >= a[i]:\n",
    "        i -= 1\n",
    "\n",
    "    j = len(a)\n",
    "    while a[j-1] <= a[i-1]:\n",
    "        j -= 1\n",
    "\n",
    "    a[i-1], a[j-1] = a[j-1], a[i-1]\n",
    "\n",
    "    i += 1\n",
    "    j = len(a)\n",
    "    while i < j:\n",
    "        a[i-1], a[j-1] = a[j-1], a[i-1]\n",
    "        i += 1\n",
    "        j -= 1\n",
    "\n",
    "\n",
    "digits = list(range(0, 9+1))\n",
    "for _ in range(1_000_000-1):\n",
    "    next_permutation(digits)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "title": "Lexicographic permutations"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}