<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Patterns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "using Symata; isymata()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Generate all possible traversals of a tree.\n",
    "\n",
    "`C` is a tree:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "C = [a,[[a1,[a12,b12,c12]],[b2,[a22,b22,c22]],[c3,[a32,b32,c32,d32]]]];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "begin\n",
    "trav(tree_List) := Flatten(trav([], tree), 1)\n",
    "trav(accum_List, [x_, y_List]) := Map(yy -> trav([accum, x], yy), y)\n",
    "trav(x_,y_) := Flatten([x,y])\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$  \\left[  \\left[ a,a1,a12 \\right] , \\left[ a,a1,b12 \\right] , \\left[ a,a1,c12 \\right] , \\left[ a,b2,a22 \\right] , \\left[ a,b2,b22 \\right] , \\left[ a,b2,c22 \\right] , \\left[ a,c3,a32 \\right] , \\left[ a,c3,b32 \\right] , \\left[ a,c3,c32 \\right] , \\left[ a,c3,d32 \\right]  \\right]  $$"
      ],
      "text/plain": [
       "L\"$$  \\left[  \\left[ a,a1,a12 \\right] , \\left[ a,a1,b12 \\right] , \\left[ a,a1,c12 \\right] , \\left[ a,b2,a22 \\right] , \\left[ a,b2,b22 \\right] , \\left[ a,b2,c22 \\right] , \\left[ a,c3,a32 \\right] , \\left[ a,c3,b32 \\right] , \\left[ a,c3,c32 \\right] , \\left[ a,c3,d32 \\right]  \\right]  $$\""
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trav(C)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example is found at\n",
    "http://mathematica.stackexchange.com/questions/5753/determining-all-possible-traversals-of-a-tree/5756#5756"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0-dev",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }<commit_msg>add comment<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Patterns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "using Symata; isymata()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Generate all possible traversals of a tree.\n",
    "\n",
    "`C` is a tree:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "C = [a,[[a1,[a12,b12,c12]],[b2,[a22,b22,c22]],[c3,[a32,b32,c32,d32]]]];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "begin\n",
    "trav(tree_List) := Flatten(trav([], tree), 1)\n",
    "trav(accum_List, [x_, y_List]) := Map(yy -> trav([accum, x], yy), y)\n",
    "trav(x_,y_) := Flatten([x,y])\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$  \\left[  \\left[ a,a1,a12 \\right] , \\left[ a,a1,b12 \\right] , \\left[ a,a1,c12 \\right] , \\left[ a,b2,a22 \\right] , \\left[ a,b2,b22 \\right] , \\left[ a,b2,c22 \\right] , \\left[ a,c3,a32 \\right] , \\left[ a,c3,b32 \\right] , \\left[ a,c3,c32 \\right] , \\left[ a,c3,d32 \\right]  \\right]  $$"
      ],
      "text/plain": [
       "L\"$$  \\left[  \\left[ a,a1,a12 \\right] , \\left[ a,a1,b12 \\right] , \\left[ a,a1,c12 \\right] , \\left[ a,b2,a22 \\right] , \\left[ a,b2,b22 \\right] , \\left[ a,b2,c22 \\right] , \\left[ a,c3,a32 \\right] , \\left[ a,c3,b32 \\right] , \\left[ a,c3,c32 \\right] , \\left[ a,c3,d32 \\right]  \\right]  $$\""
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trav(C)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example is from <NAME>, and is found at\n",
    "http://mathematica.stackexchange.com/questions/5753/determining-all-possible-traversals-of-a-tree/5756#5756"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0-dev",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
{'fraction_non_alphanumeric': 0.259684091763821, 'fraction_numerical': 0.040992854456562616, 'mean_word_length': 2.4183804627249357, 'pattern_counts': {'":': 114, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4590061', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2294, 'n_words': 393}