{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import itertools\n", "import string" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def increment(password):\n", " if password:\n", " if password[-1] < 'z':\n", " return password[:-1] + chr(ord(password[-1]) + 1)\n", " else:\n", " return increment(password[:-1]) + 'a'\n", " else:\n", " raise StopIterations\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'aca'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "increment('abz')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'aabaaaaa'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "increment('aaazzzzz')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def valid(password):\n", " if 'i' in password:\n", " return False\n", " if 'o' in password:\n", " return False\n", " if 'l' in password:\n", " return False\n", " if not has_run(password):\n", " return False\n", " if len(doubles(password)) < 2:\n", " return False\n", " return True" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def has_run(password):\n", " for i in range(len(password) - 3):\n", " if ord(password[i]) + 1 == ord(password[i+1]) and ord(password[i+1]) + 1 == ord(password[i+2]):\n", " return True\n", " return False" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has_run('hiajkblammnpo')" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def doubles(password):\n", " ds = []\n", " for i in range(len(password)-1):\n", " if password[i] == password[i+1]:\n", " ds += [password[i]]\n", " return set(ds)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'k', 'm'}" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "doubles('hiajkkblammmnpo')" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "valid('abbcegjk')" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def next_valid(password):\n", " p = increment(password)\n", " while not valid(p):\n", " p = increment(p)\n", " return p" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'abcdffaa'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next_valid('abcdefgh')" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'ghjaabcc'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next_valid('ghijklmn')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hxbxxyzz'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next_valid('hxbxwxba')" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hxcaabcc'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next_valid('hxbxxyzz')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }