{ "metadata": { "language": "Julia", "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#4. PLEAC Julia: Arrays\n", "\n", "[PLEAC = Language Examples Alike Cookbook](http://pleac.sourceforge.net/)\n", "PLEAC examples are drawn from the \"Perl Cookbook\" by Tom Christiansen & Nathan Torkington, published by O'Reilly. They provide a nice range of examples oriented toward data munging, the type of work I tend to want to do first when learning a new language.\n", "\n", "\n", "The Julia examples below are principally translations from the [Python version](http://pleac.sourceforge.net/pleac_python/)\n", "\n", "###Caution\n", "I'm learning as I go, so the code below probably doesn't represent best practice. Your suggestions are welcome! \n", "Please file an issue or make a pull request to the [github repo](https://github.com/catawbasam/IJulia_PLEAC/).\n", "\n", "The examples are not complete. Missing items are generally noted in comments. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.1: Introduction\n", "\n", "Julia provides 2 major types of Arrays, typed arrays and arrays of Any (aka cell arrays). Cell arrays can contain arbitrary types, including nested arrays. Typed arrays are automatically flattened." ] }, { "cell_type": "code", "collapsed": false, "input": [ "non_nested = [\"this\", \"that\", \"the\", \"other\"]'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "1x4 Array{ASCIIString,2}:\n", " \"this\" \"that\" \"the\" \"other\"" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "nested_typed = [\"this\", \"that\", [\"the\", \"other\"]]'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "1x4 Array{ASCIIString,2}:\n", " \"this\" \"that\" \"the\" \"other\"" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "nested_cell = {\"this\", \"that\", [\"the\", \"other\"]}'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "1x3 Array{Any,2}:\n", " \"this\" \"that\" [\"the\",\"other\"]" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "tune = [\"The\", \"Star-Spangled\", \"Banner\"]'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "1x3 Array{ASCIIString,2}:\n", " \"The\" \"Star-Spangled\" \"Banner\"" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "Vector[1,4,6]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "WARNING: " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "ename": "LoadError", "evalue": "no method convert(Type{Array{T,1}},Int64)\nat In[5]:1", "output_type": "pyerr", "traceback": [ "no method convert(Type{Array{T,1}},Int64)\nat In[5]:1", " in setindex! at array.jl:412", " in getindex at array.jl:153" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.2 Specifying an array in your program" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [\"quick\", \"brown\", \"fox\"]'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "backtraces on your platform are often misleading or partially incorrect\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "1x3 Array{ASCIIString,2}:\n", " \"quick\" \"brown\" \"fox\"" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "a = split(\"Why are you teasing me?\")'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "1x5 Array{String,2}:\n", " \"Why\" \"are\" \"you\" \"teasing\" \"me?\"" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "text = \"\"\"\n", " The boy stood on the burning deck,\n", " It was as hot as glass.\n", "\"\"\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "\" The boy stood on the burning deck,\\n It was as hot as glass.\\n\"" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "lines = [strip(l) for l in split(strip(text),'\\n')]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "2-element Array{Any,1}:\n", " \"The boy stood on the burning deck,\"\n", " \"It was as hot as glass.\" " ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "sio = IOBuffer(text) # using iobuffer rather than file here" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "IOBuffer([0x20,0x20,0x20,0x20,0x54,0x68,0x65,0x20,0x62,0x6f \u2026 0x61,0x73,0x20,0x67,0x6c,0x61,0x73,0x73,0x2e,0x0a],true,false,true,false,67,9223372036854775807,1)" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "tl=readlines(sio)\n", "sl=[strip(l) for l in tl]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "2-element Array{Any,1}:\n", " \"The boy stood on the burning deck,\"\n", " \"It was as hot as glass.\" " ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "close(sio)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "banner = \"The Mines of Moria\"\n", "#no single quoted strings in Julia\n", "name = \"Gandalf\"\n", "banner = \"Speak,$name and enter!\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "\"Speak,Gandalf and enter!\"" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "@sprintf(\"Speak, %s, and welcome!\", name)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "\"Speak, Gandalf, and welcome!\"" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "his_host = \"www.julialang.org\"\n", "getaddrinfo(his_host)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "ip\"204.232.175.78\"" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "julia_info=getpid() # Julia's process id" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "8872" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "#shell_info\n", ";tasklist " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Image Name PID Session Name Session# Mem Usage\r\n", "========================= ======== ================ =========== ============\r\n", "System Idle Process 0 Services 0 24 K\r\n", "System 4 Services 0 6,280 K\r\n", "smss.exe 388 Services 0 712 K\r\n", "csrss.exe 488 Services 0 4,000 K\r\n", "wininit.exe 572 Services 0 1,396 K\r\n", "csrss.exe 584 Console 1 61,912 K\r\n", "winlogon.exe 636 Console 1 3,344 K\r\n", "services.exe 656 Services 0 14,328 K\r\n", "lsass.exe 684 Services 0 20,524 K\r\n", "lsm.exe 692 Services 0 4,432 K\r\n", "svchost.exe 788 Services 0 7,176 K\r\n", "svchost.exe 864 Services 0 14,076 K\r\n", "svchost.exe 948 Services 0 20,464 K\r\n", "svchost.exe 980 Services 0 18,604 K\r\n", "svchost.exe 1012 Services 0 25,360 K\r\n", "svchost.exe 156 Services 0 48,920 K\r\n", "stacsv64.exe 496 Services 0 5,956 K\r\n", "svchost.exe 1092 Services 0 8,680 K\r\n", "Prot_srv.exe 1236 Services 0 17,756 K\r\n", "vpnagent.exe 1388 Services 0 9,880 K\r\n", "svchost.exe 1432 Services 0 27,468 K\r\n", "EvtEng.exe 1524 Services 0 5,824 K\r\n", "ZCfgSvc7.exe 1628 Services 0 5,064 K\r\n", "spoolsv.exe 1704 Services 0 29,176 K\r\n", "ac.sharedstore.exe 1732 Services 0 3,580 K\r\n", "HostControlService.exe 1768 Services 0 2,380 K\r\n", "acevents.exe 1776 Services 0 4,480 K\r\n", "HostStorageService.exe 1800 Services 0 2,208 K\r\n", "svchost.exe 1824 Services 0 8,472 K\r\n", "svchost.exe 1864 Services 0 10,972 K\r\n", "armsvc.exe 1968 Services 0 1,900 K\r\n", "AESTSr64.exe 1992 Services 0 1,384 K\r\n", "AeXNSAgent.exe 2020 Services 0 9,188 K\r\n", "AgentService.exe 1964 Services 0 27,192 K\r\n", "unsecapp.exe 2084 Services 0 2,176 K\r\n", "WmiPrvSE.exe 2160 Services 0 5,884 K\r\n", "svchost.exe 2264 Services 0 5,972 K\r\n", "svchost.exe 2296 Services 0 3,040 K\r\n", "LGVL600SVC.exe 2428 Services 0 2,148 K\r\n", "schsvc.exe 2560 Services 0 51,532 K\r\n", "sqlservr.exe 2672 Services 0 35,500 K\r\n", "o2flash.exe 2724 Services 0 2,752 K\r\n", "srvany.exe 2768 Services 0 1,324 K\r\n", "SDIOAssist.exe 2844 Services 0 3,008 K\r\n", "PDFProFiltSrvPP.exe 2964 Services 0 1,724 K\r\n", "pstartSr.exe 3028 Services 0 2,052 K\r\n", "pg_ctl.exe 3068 Services 0 1,900 K\r\n", "radexecd.exe 1280 Services 0 2,920 K\r\n", "radstgms.exe 2580 Services 0 2,600 K\r\n", "RegSrvc.exe 3080 Services 0 2,420 K\r\n", "postgres.exe 3088 Services 0 36,292 K\r\n", "conhost.exe 3096 Services 0 1,116 K\r\n", "upeksvr.exe 3148 Console 1 7,112 K\r\n", "ccSvcHst.exe 3156 Services 0 20,884 K\r\n", "sqlwriter.exe 3240 Services 0 8,616 K\r\n", "postgres.exe 3272 Services 0 1,848 K\r\n", "svchost.exe 3280 Services 0 19,088 K\r\n", "tvnserver.exe 3412 Services 0 2,908 K\r\n", "svchost.exe 3452 Services 0 4,444 K\r\n", "postgres.exe 3464 Services 0 1,752 K\r\n", "postgres.exe 3472 Services 0 3,292 K\r\n", "postgres.exe 3488 Services 0 1,676 K\r\n", "postgres.exe 3500 Services 0 2,552 K\r\n", "postgres.exe 3516 Services 0 1,940 K\r\n", "WLIDSVC.EXE 3540 Services 0 5,852 K\r\n", "WLIDSVCM.EXE 3816 Services 0 1,292 K\r\n", "taskhost.exe 4128 Console 1 7,996 K\r\n", "ccSvcHst.exe 4148 Console 1 5,256 K\r\n", "dwm.exe 4436 Console 1 3,764 K\r\n", "explorer.exe 4508 Console 1 109,804 K\r\n", "acevents.exe 4864 Console 1 5,556 K\r\n", "accrdsub.exe 4984 Console 1 7,324 K\r\n", "Smc.exe 5016 Services 0 20,564 K\r\n", "Apoint.exe 5088 Console 1 5,280 K\r\n", "iFrmewrk.exe 5140 Console 1 7,316 K\r\n", "sttray64.exe 5148 Console 1 5,648 K\r\n", "FF_Protection.exe 5256 Console 1 3,104 K\r\n", "tvnserver.exe 5268 Console 1 2,464 K\r\n", "MSOSYNC.EXE 5320 Console 1 7,928 K\r\n", "ISUSPM.exe 5352 Console 1 8,496 K\r\n", "acsagent.exe 5400 Console 1 5,944 K\r\n", "ONENOTEM.EXE 5428 Console 1 1,120 K\r\n", "RoxioBurnLauncher.exe 5588 Console 1 4,944 K\r\n", "PDVD9Serv.exe 5612 Console 1 2,224 K\r\n", "svchost.exe 5700 Services 0 3,244 K\r\n", "AeXNSAgentHostSurrogate32 5740 Services 0 12,108 K\r\n", "IAStorIcon.exe 5924 Console 1 11,648 K\r\n", "WUDFHost.exe 6084 Services 0 2,288 K\r\n", "schTray.exe 5296 Console 1 21,084 K\r\n", "cptray.exe 5640 Console 1 2,608 K\r\n", "ApMsgFwd.exe 6244 Console 1 1,852 K\r\n", "ApntEx.exe 6436 Console 1 3,332 K\r\n", "hidfind.exe 6448 Console 1 2,156 K\r\n", "conhost.exe 6480 Console 1 2,196 K\r\n", "P95tray.exe 6536 Console 1 3,828 K\r\n", "unsecapp.exe 6580 Console 1 3,272 K\r\n", "Agent.exe 6716 Console 1 270,748 K\r\n", "AeXAgentUIHost.exe 6852 Console 1 4,596 K\r\n", "pptd40nt.exe 6956 Console 1 2,456 K\r\n", "pdfPro5Hook.exe 6972 Console 1 2,120 K\r\n", "SearchIndexer.exe 1312 Services 0 75,824 K\r\n", "BrStMonW.exe 6048 Console 1 231,044 K\r\n", "BrCtrlCntr.exe 6732 Console 1 1,944 K\r\n", "vpnui.exe 6676 Console 1 16,980 K\r\n", "BrYNSvc.exe 6684 Services 0 5,012 K\r\n", "communicator.exe 7028 Console 1 87,976 K\r\n", "EMET_Agent.exe 7044 Console 1 17,692 K\r\n", "BrCcUxSys.exe 7328 Console 1 2,200 K\r\n", "PrivacyIconClient.exe 8584 Console 1 24,968 K\r\n", "LMS.exe 8620 Services 0 3,792 K\r\n", "UNS.exe 8644 Services 0 9,708 K\r\n", "UcMapi.exe 8844 Console 1 41,736 K\r\n", "IAStorDataMgrSvc.exe 4924 Services 0 9,396 K\r\n", "OSPPSVC.EXE 2660 Services 0 10,696 K\r\n", "wmpnetwk.exe 11340 Services 0 11,668 K\r\n", "ielowutil.exe 1040 Console 1 1,256 K\r\n", "TGitCache.exe 5408 Console 1 15,512 K\r\n", "chrome.exe 11416 Console 1 765,144 K\r\n", "chrome.exe 9876 Console 1 106,112 K\r\n", "splwow64.exe 9096 Console 1 19,604 K\r\n", "JuniperSetupClient.exe 13044 Console 1 16,564 K\r\n", "svchost.exe 1820 Services 0 5,336 K\r\n", "notepad++.exe 10576 Console 1 39,184 K\r\n", "WmiPrvSE.exe 1556 Services 0 32,232 K\r\n", "chrome.exe 5912 Console 1 11,268 K\r\n", "ssh-agent.exe 7040 Console 1 4,876 K\r\n", "SearchProtocolHost.exe 12760 Services 0 11,216 K\r\n", "WINWORD.EXE 14156 Console 1 139,064 K\r\n", "chrome.exe 6904 Console 1 106,924 K\r\n", "chrome.exe 13828 Console 1 43,144 K\r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "chrome.exe 9188 Console 1 46,628 K\r\n", "chrome.exe 12196 Console 1 51,828 K\r\n", "chrome.exe 10204 Console 1 44,560 K\r\n", "chrome.exe 1960 Console 1 64,488 K\r\n", "WmiPrvSE.exe 10480 Services 0 21,892 K\r\n", "svchost.exe 5764 Services 0 5,940 K\r\n", "chrome.exe 7944 Console 1 56,768 K\r\n", "OUTLOOK.EXE 8352 Console 1 193,008 K\r\n", "python.exe 9048 Console 1 64,496 K\r\n", "conhost.exe 10516 Console 1 8,844 K\r\n", "chrome.exe 8100 Console 1 78,688 K\r\n", "cmd.exe 7372 Console 1 3,616 K\r\n", "julia-readline.exe 8516 Console 1 287,812 K\r\n", "cmd.exe 12060 Console 1 3,616 K\r\n", "julia-readline.exe 8272 Console 1 150,012 K\r\n", "cmd.exe 12936 Console 1 3,448 K\r\n", "conhost.exe 5728 Console 1 8,840 K\r\n", "w3wp.exe 4496 Services 0 13,312 K\r\n", "taskeng.exe 12728 Services 0 5,724 K\r\n", "taskeng.exe 8224 Console 1 7,284 K\r\n", "cmd.exe 11232 Console 1 3,656 K\r\n", "julia-readline.exe 8872 Console 1 284,336 K\r\n", "chrome.exe 12464 Console 1 56,476 K\r\n", "radsched.exe 8148 Services 0 5,512 K\r\n", "SearchFilterHost.exe 4516 Services 0 7,292 K\r\n", "tasklist.exe 7496 Console 1 6,704 K\r\n" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "banner = [\"Costs\", \"only\", \"$4.95\"]' # $ stripped due to substitution" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "1x3 Array{ASCIIString,2}:\n", " \"Costs\" \"only\" \"4.95\"" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "banner = [\"Costs\", \"only\", \"\\$4.95\"]'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "1x3 Array{ASCIIString,2}:\n", " \"Costs\" \"only\" \"\\$4.95\"" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "print(banner[3])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "$4.95" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "banner = split(\"Costs only \\$4.95\")'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "1x3 Array{String,2}:\n", " \"Costs\" \"only\" \"\\$4.95\"" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ " brax=split(\"\"\" ' \" ( ) < > { } [ ] \"\"\")'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "1x10 Array{String,2}:\n", " \"'\" \"\\\"\" \"(\" \")\" \"<\" \">\" \"{\" \"}\" \"[\" \"]\"" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "# no ''' in Julia" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "tags = split(\"LI TABLE TR TD A IMG H1 P\")'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "1x8 Array{String,2}:\n", " \"LI\" \"TABLE\" \"TR\" \"TD\" \"A\" \"IMG\" \"H1\" \"P\"" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "sample = split(\"The backslash (\\\\) is often used in regular expressions.\")'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "1x9 Array{String,2}:\n", " \"The\" \"backslash\" \"(\\\\)\" \"is\" \u2026 \"in\" \"regular\" \"expressions.\"" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "#Julia uses Unicode by default, so no string qualifier is required\n", "ships = \"Ni\u00f1a Pinta Santa Mar\u00eda\"\n", "split(ships)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "1x4 Array{String,2}:\n", " \"Ni\u00f1a\" \"Pinta\" \"Santa\" \"Mar\u00eda\"" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "ships = [\"Ni\u00f1a\", \"Pinta\", \"Santa Mar\u00eda\"]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": [ "3-element Array{UTF8String,1}:\n", " \"Ni\u00f1a\" \n", " \"Pinta\" \n", " \"Santa Mar\u00eda\"" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.3: Printing a list with commas" ] }, { "cell_type": "code", "collapsed": false, "input": [ "js=join(ships,\", \")\n", "js= length(ships)>1? replace(js,\", \"*ships[end], \" and \"*ships[end]) : js" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "\"Ni\u00f1a, Pinta and Santa Mar\u00eda\"" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "function commify_series(arr::Array)\n", " js=join(arr,\", \")\n", " js=length(arr)>1? replace(js,\", \"*arr[end], \" and \"*arr[end]) : js\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 29, "text": [ "commify_series (generic function with 1 method)" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "commify_series([])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "\"\"" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "commify_series([\"red\"])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "\"red\"" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "commify_series([\"red\",\"yellow\"]) " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": [ "\"red and yellow\"" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "commify_series([\"red\",\"yellow\",\"green\"])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "\"red, yellow and green\"" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "myarray = [\"red\",\"yellow\",\"green\"]'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "1x3 Array{ASCIIString,2}:\n", " \"red\" \"yellow\" \"green\"" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "println( \"I have [$(join(myarray,\", \"))] marbles.\" )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I have [red, yellow, green] marbles." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "println( \"I have \" * join(myarray,' ') * \" marbles.\" )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I have red yellow green marbles." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "# commify_series - show proper comma insertion in list output\n", "data = {\n", " ( \"just one thing\", ),\n", " ( split(\"Mutt Jeff\") ),\n", " ( split(\"Peter Paul Mary\") ),\n", " ( \"To our parents\", \"Mother Theresa\", \"God\" ),\n", " ( \"pastrami\", \"ham and cheese\", \"peanut butter and jelly\", \"tuna\" ),\n", " ( \"recycle tired, old phrases\", \"ponder big, happy thoughts\" ),\n", " ( \"recycle tired, old phrases\", \"ponder big, happy thoughts\", \"sleep and dream peacefully\" ),\n", " }" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 37, "text": [ "7-element Array{Any,1}:\n", " (\"just one thing\",) \n", " [\"Mutt\",\"Jeff\"] \n", " [\"Peter\",\"Paul\",\"Mary\"] \n", " (\"To our parents\",\"Mother Theresa\",\"God\") \n", " (\"pastrami\",\"ham and cheese\",\"peanut butter and jelly\",\"tuna\") \n", " (\"recycle tired, old phrases\",\"ponder big, happy thoughts\") \n", " (\"recycle tired, old phrases\",\"ponder big, happy thoughts\",\"sleep and dream peacefully\")" ] } ], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "typeof(data)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "Array{Any,1}" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "function commify_sep(terms::Array)\n", " sep=\", \"\n", " for term in terms\n", " if \",\" in term\n", " sep = \"; \"\n", " end\n", " end \n", " return sep\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "commify_sep (generic function with 1 method)" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "for item in data\n", " #println(item)\n", " println(\"$(commify_sep(item)).\")\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "ename": "LoadError", "evalue": "no method commify_sep((ASCIIString,),)\nat In[40]:4", "output_type": "pyerr", "traceback": [ "no method commify_sep((ASCIIString,),)\nat In[40]:4", " in anonymous at no file:3" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "function commify_series3(terms)\n", " sep = commify_sep(terms)\n", " n=length(terms)\n", " if n==0\n", " return \"\"\n", " elseif n==1\n", " return terms[1]\n", " elseif n==2\n", " return \"$(terms[1]) and $(terms[2])\"\n", " else\n", " return \"$(join(terms[1:end-1],sep))$(sep)and $(terms[end])\"\n", " end\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 41, "text": [ "commify_series3 (generic function with 1 method)" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "for item in data\n", " println(\"The list is: $(commify_series3(item)).\")\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "ename": "LoadError", "evalue": "no method commify_sep((ASCIIString,),)\nat In[42]:3", "output_type": "pyerr", "traceback": [ "no method commify_sep((ASCIIString,),)\nat In[42]:3", " in commify_series3 at In[41]:2" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 42 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.4: Changing Array Size" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = Array(Int,5)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 43, "text": [ "1x5 Array{Int64,2}:\n", " 57451264 62701552 74991088 67816400 74991280" ] } ], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "#resize via assignment\n", "mysize=3\n", "a = a[1:mysize]\n", "a'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 44, "text": [ "1x3 Array{Int64,2}:\n", " 57451264 62701552 74991088" ] } ], "prompt_number": 44 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####methods that change an object in-place usually end with !" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# To add an element to the end of a list, use the push! method:\n", "push!(a,4)\n", "a'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "1x4 Array{Int64,2}:\n", " 57451264 62701552 74991088 4" ] } ], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "# To insert an element, use the insert! method:\n", "insert!(a,1,10)\n", "a'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 46, "text": [ "1x5 Array{Int64,2}:\n", " 10 57451264 62701552 74991088 4" ] } ], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "# To append one list with the contents of another, use the append! method\n", "a2 = [ 1,2,3 ]\n", "append!(a,a2)\n", "a'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": [ "1x8 Array{Int64,2}:\n", " 10 57451264 62701552 74991088 4 1 2 3" ] } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "# To insert the contents of one list into another, overwriting zero or \n", "# more elements, specify a slice\n", "a[2:4] = a2\n", "a'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "1x8 Array{Int64,2}:\n", " 10 1 2 3 4 1 2 3" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "a[1]=0\n", "a[2]=8\n", "a'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 49, "text": [ "1x8 Array{Int64,2}:\n", " 0 8 2 3 4 1 2 3" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "# To remove one element from the middle of a list\n", "splice!(a,2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 50, "text": [ "8" ] } ], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "a'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 51, "text": [ "1x7 Array{Int64,2}:\n", " 0 2 3 4 1 2 3" ] } ], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ "# To remove elements from the middle of a list:\n", "splice!(a,3:5)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 52, "text": [ "1x3 Array{Int64,2}:\n", " 3 4 1" ] } ], "prompt_number": 52 }, { "cell_type": "code", "collapsed": false, "input": [ "a'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 53, "text": [ "1x4 Array{Int64,2}:\n", " 0 2 2 3" ] } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ "function what_about_that_array(terms)\n", " println( \"The list now has $(length(terms)) elements.\")\n", " println( \"The index of the last element is $(length(terms))\" )\n", " println( \"Element #3 is $(terms[3])\" )\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 54, "text": [ "what_about_that_array (generic function with 1 method)" ] } ], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "what_about_that_array(a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The list now has 4 elements." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The index of the last element is 4\n", "Element #3 is 2\n" ] } ], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [ "terms=a\n", "println( \"The index of the last element is $(length(terms))\" )\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The index of the last element is 4" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "people = split(\"Crosby Stills Nash Young\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 57, "text": [ "4-element Array{String,1}:\n", " \"Crosby\"\n", " \"Stills\"\n", " \"Nash\" \n", " \"Young\" " ] } ], "prompt_number": 57 }, { "cell_type": "code", "collapsed": false, "input": [ "what_about_that_array(people)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The list now has 4 elements." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The index of the last element is 4\n", "Element #3 is Nash\n" ] } ], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "pop!(people)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 59, "text": [ "\"Young\"" ] } ], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [ "what_about_that_array(people)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The list now has 3 elements." ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The index of the last element is 3\n", "Element #3 is Nash\n" ] } ], "prompt_number": 60 }, { "cell_type": "code", "collapsed": false, "input": [ "sz=10000-length(people)\n", "xtra=[None for i in 1:sz ];" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 60 }, { "cell_type": "code", "collapsed": false, "input": [ "append!(people, xtra);" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "ename": "LoadError", "evalue": "no method convert(Type{String},UnionType)\nat In[60]:1", "output_type": "pyerr", "traceback": [ "no method convert(Type{String},UnionType)\nat In[60]:1", " in copy! at abstractarray.jl:230" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 60 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.5 Doing something with every element in an array" ] }, { "cell_type": "code", "collapsed": false, "input": [ "myarray" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 61, "text": [ "1x3 Array{ASCIIString,2}:\n", " \"red\" \"yellow\" \"green\"" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "for item in myarray\n", " pass\n", "end" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 62 }, { "cell_type": "code", "collapsed": false, "input": [ "for (k,v) = ENV\n", " print( k, v[1:min(length(v),' ',6)], ' ' ) \n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "C" ] }, { "output_type": "stream", "stream": "stdout", "text": [ ":=c:\\ COLUMNS80 JL_PRIVATE_LIBDIRbin JULIAC:\\Tem JULIA_EDITORstart JULIA_EXEjulia- JULIA_HOMEC:\\Tem LINES30 PROMPT$P$G SYS_PATHc:\\pyt TMPC:\\Use COMPUTERNAMEMM1911 USERDOMAINMITRE BURN_AUTOPLAYC:\\Pro EMC_AUTOPLAYC:\\Pro PSMODULEPATHC:\\Win COMMONPROGRAMFILESC:\\Pro PROCESSOR_IDENTIFIERIntel6 PROGRAMFILESC:\\Pro PROCESSOR_REVISION2a07 GPHOME_CLIENTSC:\\Pro PATHC:\\Tem NUMBER_OF_PROCESSORS8 PROGRAMFILES(X86)C:\\Pro WINDOWS_TRACING_FLAGS3 SSH_AGENT_PID1056 TK_LIBRARYC:\\Pyt PRGC:\\Pro GIT_SSHC:\\Pro TEMPC:\\Use ORACLE_HOMEC:\\Ora COMMONPROGRAMFILES(X86)C:\\Pro PROCESSOR_ARCHITECTUREAMD64 TIX_LIBRARYC:\\Pyt ALLUSERSPROFILEC:\\Pro TNS_ADMINC:\\Ora LOCALAPPDATAC:\\Use HOMEPATH\\Users USERDOMAIN_ROAMINGPROFILEMITRE PROGRAMW6432C:\\Pro USERNAMEKEITHC FP_NO_HOST_CHECKNO LOGONSERVER\\\\MBDC SYSTEMROOTC:\\Win SESSIONNAMEConsol PROGRAMDATAC:\\Pro PYTHONPATHC:\\Pyt SSH_AUTH_SOCK/tmp/s TCL_LIBRARYC:\\Pyt USERDNSDOMAINMITRE. R_HOMEC:\\Pro PATHEXT.COM;. GDAL_DATAC:\\Pro WINDIRC:\\Win WINDOWS_TRACING_LOGFILEC:\\BVT HOMEDRIVEC: RCAUTOPLAYC:\\Pro SYSTEMDRIVEC: P86C:\\Pro COMSPECC:\\Win SVN_SSHC:\\Pro APPDATAC:\\Use PROCESSOR_LEVEL6 COMMONPROGRAMW6432C:\\Pro OSWindow PUBLICC:\\Use VTK_DATA_ROOTC:\\Pyt USERPROFILEC:\\Use " ] } ], "prompt_number": 63 }, { "cell_type": "code", "collapsed": false, "input": [ "# skipped examples with get_usage() and who" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 64 }, { "cell_type": "code", "collapsed": false, "input": [ "mytext=\"\"\"a frumpily godgered\n", " skillufous banger.\n", " nasbue u oiper \n", " han yty poy\"\"\"\n", "fakefile = IOBuffer(mytext)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 65, "text": [ "IOBuffer([0x61,0x20,0x66,0x72,0x75,0x6d,0x70,0x69,0x6c,0x79 \u2026 0x61,0x6e,0x20,0x79,0x74,0x79,0x20,0x70,0x6f,0x79],true,false,true,false,66,9223372036854775807,1)" ] } ], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "while ~eof(fakefile)\n", " ln=readline(fakefile)\n", " words = split(ln)\n", " for w in words\n", " print(reverse(w)*\" \")\n", " end \n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "ylipmurf deregdog suofulliks .regnab eubsan u repio nah yty yop " ] } ], "prompt_number": 66 }, { "cell_type": "code", "collapsed": false, "input": [ "data = [1, 2, 3]'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 67, "text": [ "1x3 Array{Int64,2}:\n", " 1 2 3" ] } ], "prompt_number": 67 }, { "cell_type": "code", "collapsed": false, "input": [ "data = [i-1 for i in data]'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 68, "text": [ "1x3 Array{Any,2}:\n", " 0 1 2" ] } ], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "# skipped final example" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 69 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### PLEAC 4.6: Iterating over an array by reference" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fruits = [\"Apple\", \"Blackberry\"]\n", "for fruit in fruits\n", " println( fruit, \" tastes good in a pie.\")\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Apple" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " tastes good in a pie.\n", "Blackberry tastes good in a pie.\n" ] } ], "prompt_number": 70 }, { "cell_type": "code", "collapsed": false, "input": [ "# If you must explicitly index, use enumerate():\n", "for (i, fruit) in enumerate(fruits)\n", " println( i,' ', fruit, \" tastes good in a pie.\")\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Apple tastes good in a pie.\n", "2 Blackberry tastes good in a pie.\n" ] } ], "prompt_number": 71 }, { "cell_type": "code", "collapsed": false, "input": [ "rogue_cats = [\"Morris\", \"Felix\"]\n", "namedict = { \"felines\"=> rogue_cats }" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 72, "text": [ "{\"felines\"=>[\"Morris\",\"Felix\"]}" ] } ], "prompt_number": 72 }, { "cell_type": "code", "collapsed": false, "input": [ "for cat in namedict[\"felines\"]\n", " println( cat, \"purrs hypnotically.\")\n", "end\n", "println( \"--More-- \\nYou are controlled.\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Morris" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "purrs hypnotically.\n", "Felixpurrs hypnotically.\n", "--More-- \n", "You are controlled.\n" ] } ], "prompt_number": 73 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.7: Extracting unique elements from a list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ar=[1,2,2,4,5,6,6]\n", "unique(ar)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 74, "text": [ "1x5 Array{Int64,2}:\n", " 1 2 4 5 6" ] } ], "prompt_number": 74 }, { "cell_type": "code", "collapsed": false, "input": [ "myset=Set(ar...)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 75, "text": [ "Set{Int64}(5,4,6,2,1)" ] } ], "prompt_number": 75 }, { "cell_type": "code", "collapsed": false, "input": [ "seen=Dict()\n", "for item in ar\n", " seen[item] = get(seen,item, 0) + 1\n", "end\n", "seen" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 76, "text": [ "{5=>1,4=>1,6=>2,2=>2,1=>1}" ] } ], "prompt_number": 76 }, { "cell_type": "code", "collapsed": false, "input": [ "uniq=[k for k in keys(seen)]\n", "uniq'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 77, "text": [ "1x5 Array{Any,2}:\n", " 5 4 6 2 1" ] } ], "prompt_number": 77 }, { "cell_type": "code", "collapsed": false, "input": [ "# generate a list of users logged in, removing duplicates\n", "# SKIP" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 78 }, { "cell_type": "code", "collapsed": false, "input": [ "sort(int(uniq))'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 79, "text": [ "1x5 Array{Int64,2}:\n", " 1 2 4 5 6" ] } ], "prompt_number": 79 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.8: Finding elements in one array but not another" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a_array = [1,2,4,6,8,10]\n", "b_array = [2,5,6,8,11];" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 79 }, { "cell_type": "code", "collapsed": false, "input": [ "Set(b_array...)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 80, "text": [ "Set{Int64}(5,6,2,11,8)" ] } ], "prompt_number": 80 }, { "cell_type": "code", "collapsed": false, "input": [ "setdiff( Set(a_array...), Set(b_array...))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 81, "text": [ "Set{Int64}(4,10,1)" ] } ], "prompt_number": 81 }, { "cell_type": "code", "collapsed": false, "input": [ "setdiff(a_array, b_array)' #arrays get handled automatically; nice!" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 82, "text": [ "1x3 Array{Int64,2}:\n", " 1 4 10" ] } ], "prompt_number": 82 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.9: Computing union, intersection or difference of unique arrays (sets)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = Set(1, 3, 5, 6, 7, 8)\n", "b = Set(2, 3, 5, 7, 9)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 83, "text": [ "Set{Int64}(5,7,2,9,3)" ] } ], "prompt_number": 83 }, { "cell_type": "code", "collapsed": false, "input": [ "union(a,b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 84, "text": [ "Set{Int64}(5,6,7,2,9,8,3,1)" ] } ], "prompt_number": 84 }, { "cell_type": "code", "collapsed": false, "input": [ "intersect(a,b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 85, "text": [ "Set{Int64}(5,7,3)" ] } ], "prompt_number": 85 }, { "cell_type": "code", "collapsed": false, "input": [ "symdiff(a,b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 86, "text": [ "Set{Int64}(6,2,9,8,1)" ] } ], "prompt_number": 86 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.10: Appending one array to another" ] }, { "cell_type": "code", "collapsed": false, "input": [ "push!(ar,9)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 87, "text": [ "1x8 Array{Int64,2}:\n", " 1 2 2 4 5 6 6 9" ] } ], "prompt_number": 87 }, { "cell_type": "code", "collapsed": false, "input": [ "append!(ar,[3,4])'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 88, "text": [ "1x10 Array{Int64,2}:\n", " 1 2 2 4 5 6 6 9 3 4" ] } ], "prompt_number": 88 }, { "cell_type": "code", "collapsed": false, "input": [ "members = [\"Time\", \"Flies\"]\n", "initiates = [\"An\", \"Arrow\"]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 89, "text": [ "2-element Array{ASCIIString,1}:\n", " \"An\" \n", " \"Arrow\"" ] } ], "prompt_number": 89 }, { "cell_type": "code", "collapsed": false, "input": [ "append!(members,initiates)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 90, "text": [ "1x4 Array{ASCIIString,2}:\n", " \"Time\" \"Flies\" \"An\" \"Arrow\"" ] } ], "prompt_number": 90 }, { "cell_type": "code", "collapsed": false, "input": [ "insert!(members,3,\"Like\")'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 91, "text": [ "1x5 Array{ASCIIString,2}:\n", " \"Time\" \"Flies\" \"Like\" \"An\" \"Arrow\"" ] } ], "prompt_number": 91 }, { "cell_type": "code", "collapsed": false, "input": [ "members[1]=\"Fruit\"\n", "members[4:5]=[\"A\",\"Banana\"]\n", "members'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 92, "text": [ "1x5 Array{ASCIIString,2}:\n", " \"Fruit\" \"Flies\" \"Like\" \"A\" \"Banana\"" ] } ], "prompt_number": 92 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.11: Reversing an array" ] }, { "cell_type": "code", "collapsed": false, "input": [ "reverse!(members)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 93, "text": [ "1x5 Array{ASCIIString,2}:\n", " \"Banana\" \"A\" \"Like\" \"Flies\" \"Fruit\"" ] } ], "prompt_number": 93 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.12: Processing multiple elements of an array" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ar = [i for i in 1:8]\n", "# remove n elements from front of mylist, saving them into fron\n", "front = splice!(ar,1:3)\n", "front'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 94, "text": [ "1x3 Array{Int64,2}:\n", " 1 2 3" ] } ], "prompt_number": 94 }, { "cell_type": "code", "collapsed": false, "input": [ "ar'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 95, "text": [ "1x5 Array{Int64,2}:\n", " 4 5 6 7 8" ] } ], "prompt_number": 95 }, { "cell_type": "code", "collapsed": false, "input": [ "# remove 1 element from the front , saving it in front\n", "front = splice!(ar,1) " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 96, "text": [ "4" ] } ], "prompt_number": 96 }, { "cell_type": "code", "collapsed": false, "input": [ "ar'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 97, "text": [ "1x4 Array{Int64,2}:\n", " 5 6 7 8" ] } ], "prompt_number": 97 }, { "cell_type": "code", "collapsed": false, "input": [ "# remove n elements from the end\n", "splice!(ar,endof(ar)-1:endof(ar))'\n", "ar" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 98, "text": [ "2-element Array{Int64,1}:\n", " 5\n", " 6" ] } ], "prompt_number": 98 }, { "cell_type": "code", "collapsed": false, "input": [ "# remove n elements from the end, saving them in end\n", "myend = splice!(ar,endof(ar)-1:endof(ar))'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 99, "text": [ "1x2 Array{Int64,2}:\n", " 5 6" ] } ], "prompt_number": 99 }, { "cell_type": "code", "collapsed": false, "input": [ "ar = [i for i in 1:8]\n", "myend=pop!(ar)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 100, "text": [ "8" ] } ], "prompt_number": 100 }, { "cell_type": "code", "collapsed": false, "input": [ "function shift2(ar)\n", " front = splice!(ar,1:2)\n", " return front\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 101, "text": [ "shift2 (generic function with 1 method)" ] } ], "prompt_number": 101 }, { "cell_type": "code", "collapsed": false, "input": [ "friends = split(\"Peter Paul Mary Jim Tim\")\n", "this, that = shift2(friends)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 102, "text": [ "2-element Array{String,1}:\n", " \"Peter\"\n", " \"Paul\" " ] } ], "prompt_number": 102 }, { "cell_type": "code", "collapsed": false, "input": [ "this, that, friends" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 103, "text": [ "(\"Peter\",\"Paul\",[\"Mary\",\"Jim\",\"Tim\"])" ] } ], "prompt_number": 103 }, { "cell_type": "code", "collapsed": false, "input": [ "function pop2(ar)\n", " back = splice!(ar,endof(ar)-1:endof(ar))\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 104, "text": [ "pop2 (generic function with 1 method)" ] } ], "prompt_number": 104 }, { "cell_type": "code", "collapsed": false, "input": [ "beverages = split( \"Dew Jolt Cola Sprite Fresca\")\n", "pair = pop2(beverages)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 105, "text": [ "2-element Array{String,1}:\n", " \"Sprite\"\n", " \"Fresca\"" ] } ], "prompt_number": 105 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### PLEAC 4.13: Finding the First Array Element That Passes a Test" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ar'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 106, "text": [ "1x7 Array{Int64,2}:\n", " 1 2 3 4 5 6 7" ] } ], "prompt_number": 106 }, { "cell_type": "code", "collapsed": false, "input": [ "criterion(n) = return (n>2) #short form function declaration\n", "criterion(2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 107, "text": [ "false" ] } ], "prompt_number": 107 }, { "cell_type": "code", "collapsed": false, "input": [ "filter( n -> (n>2), ar)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 108, "text": [ "1x5 Array{Int64,2}:\n", " 3 4 5 6 7" ] } ], "prompt_number": 108 }, { "cell_type": "code", "collapsed": false, "input": [ "employees = [(\"eng\",54,\"joe\"),(\"law\",65,\"billy\"),(\"eng\",72,\"bob\")]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 109, "text": [ "3-element Array{(ASCIIString,Int64,ASCIIString),1}:\n", " (\"eng\",54,\"joe\") \n", " (\"law\",65,\"billy\")\n", " (\"eng\",72,\"bob\") " ] } ], "prompt_number": 109 }, { "cell_type": "code", "collapsed": false, "input": [ "#use an anonymous function\n", "eng =filter( emp->(emp[1]==\"eng\"), employees)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 110, "text": [ "2-element Array{(ASCIIString,Int64,ASCIIString),1}:\n", " (\"eng\",54,\"joe\")\n", " (\"eng\",72,\"bob\")" ] } ], "prompt_number": 110 }, { "cell_type": "code", "collapsed": false, "input": [ "sal, idx =findmax([e[2] for e in eng])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 111, "text": [ "(72,2)" ] } ], "prompt_number": 111 }, { "cell_type": "code", "collapsed": false, "input": [ "println(\"Highest paid engineer is: $(eng[idx][3])\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Highest paid engineer is: bob" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 112 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.14: Finding All Elements in an Array Matching Certain Criteria" ] }, { "cell_type": "code", "collapsed": false, "input": [ "matching = filter( emp->(emp[1]==\"eng\"), employees)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 113, "text": [ "2-element Array{(ASCIIString,Int64,ASCIIString),1}:\n", " (\"eng\",54,\"joe\")\n", " (\"eng\",72,\"bob\")" ] } ], "prompt_number": 113 }, { "cell_type": "code", "collapsed": false, "input": [ "nums=999997:1000005\n", "bigs = filter( n -> n>1000000, nums)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 114, "text": [ "1x5 Array{Int64,2}:\n", " 1000001 1000002 1000003 1000004 1000005" ] } ], "prompt_number": 114 }, { "cell_type": "code", "collapsed": false, "input": [ "#SKIP pigs : as it is for Dicts\n", "#SKIP os.popen(\"who\") " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 115 }, { "cell_type": "code", "collapsed": false, "input": [ "type Empl\n", " category::String\n", " salary::Int64\n", " name::String\n", "end" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 116 }, { "cell_type": "code", "collapsed": false, "input": [ "e1=Empl(\"eng\",45,\"Kyle\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 117, "text": [ "Empl(\"eng\",45,\"Kyle\")" ] } ], "prompt_number": 117 }, { "cell_type": "code", "collapsed": false, "input": [ "ear =[ Empl(\"eng\",47,\"Jo\"), Empl(\"cat\",45,\"Kyle\"), Empl(\"eng\",56,\"Jim\")]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 118, "text": [ "3-element Array{Empl,1}:\n", " Empl(\"eng\",47,\"Jo\") \n", " Empl(\"cat\",45,\"Kyle\")\n", " Empl(\"eng\",56,\"Jim\") " ] } ], "prompt_number": 118 }, { "cell_type": "code", "collapsed": false, "input": [ "eng = filter( e-> e.category==\"eng\", ear)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 119, "text": [ "2-element Array{Empl,1}:\n", " Empl(\"eng\",47,\"Jo\") \n", " Empl(\"eng\",56,\"Jim\")" ] } ], "prompt_number": 119 }, { "cell_type": "code", "collapsed": false, "input": [ "type Appl\n", " name::String\n", " income::Int64\n", "end" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 120 }, { "cell_type": "code", "collapsed": false, "input": [ "appls = [ Appl(\"Biff\",6), Appl(\"Buff\",6), Appl(\"Boff\",6)]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 121, "text": [ "3-element Array{Appl,1}:\n", " Appl(\"Biff\",6)\n", " Appl(\"Buff\",6)\n", " Appl(\"Boff\",6)" ] } ], "prompt_number": 121 }, { "cell_type": "code", "collapsed": false, "input": [ "secondary_assistance = filter( appl-> appl.income<30000, appls)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 122, "text": [ "3-element Array{Appl,1}:\n", " Appl(\"Biff\",6)\n", " Appl(\"Buff\",6)\n", " Appl(\"Boff\",6)" ] } ], "prompt_number": 122 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.16: Sorting an Array Numerically" ] }, { "cell_type": "code", "collapsed": false, "input": [ "narr = [1,5,3,6,8,3,2]\n", "narr'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 123, "text": [ "1x7 Array{Int64,2}:\n", " 1 5 3 6 8 3 2" ] } ], "prompt_number": 123 }, { "cell_type": "code", "collapsed": false, "input": [ "sort(narr)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 124, "text": [ "1x7 Array{Int64,2}:\n", " 1 2 3 3 5 6 8" ] } ], "prompt_number": 124 }, { "cell_type": "code", "collapsed": false, "input": [ "reverse(sort(narr))'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 125, "text": [ "1x7 Array{Int64,2}:\n", " 8 6 5 3 3 2 1" ] } ], "prompt_number": 125 }, { "cell_type": "code", "collapsed": false, "input": [ "sort(narr, rev=true)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 126, "text": [ "1x7 Array{Int64,2}:\n", " 8 6 5 3 3 2 1" ] } ], "prompt_number": 126 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.17: Sorting an array by a computable field" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ordered = sort(narr)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 127, "text": [ "1x7 Array{Int64,2}:\n", " 1 2 3 3 5 6 8" ] } ], "prompt_number": 127 }, { "cell_type": "code", "collapsed": false, "input": [ "# computed field for ordering via by fn\n", "ordered = sort(narr, by=(x-> -1x))' " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 128, "text": [ "1x7 Array{Int64,2}:\n", " 8 6 5 3 3 2 1" ] } ], "prompt_number": 128 }, { "cell_type": "code", "collapsed": false, "input": [ "# skip sort of /etc/passwd" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 129 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.18: Implementing a circular array" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# there has to be a better way to do this\n", "type Circlei\n", " index::Int64\n", "end\n", "\n", "Circlei() = Circlei(1)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 130, "text": [ "Circlei (constructor with 2 methods)" ] } ], "prompt_number": 130 }, { "cell_type": "code", "collapsed": false, "input": [ "circ = Circlei()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 131, "text": [ "Circlei(1)" ] } ], "prompt_number": 131 }, { "cell_type": "code", "collapsed": false, "input": [ "import Base.next\n", "function next(c::Circlei)\n", " c.index = ((c.index+1) % 5)\n", " c.index = c.index==0 ? 5 : c.index\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 132, "text": [ "next (generic function with 45 methods)" ] } ], "prompt_number": 132 }, { "cell_type": "code", "collapsed": false, "input": [ "next(circ)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 133, "text": [ "2" ] } ], "prompt_number": 133 }, { "cell_type": "code", "collapsed": false, "input": [ "abort_counter = 0\n", "while abort_counter<7\n", " next(circ)\n", " println(\"Handling process $(circ.index)\")\n", " abort_counter+=1\n", "end" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Handling process 3" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Handling process 4\n", "Handling process 5\n", "Handling process 1\n", "Handling process 2\n", "Handling process 3\n", "Handling process 4\n" ] } ], "prompt_number": 134 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.19: Randomizing an array" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ar'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 135, "text": [ "1x7 Array{Int64,2}:\n", " 1 2 3 4 5 6 7" ] } ], "prompt_number": 135 }, { "cell_type": "code", "collapsed": false, "input": [ "shuffle(ar)'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 136, "text": [ "1x7 Array{Int64,2}:\n", " 1 2 4 6 3 5 7" ] } ], "prompt_number": 136 }, { "cell_type": "code", "collapsed": false, "input": [ "shuffle!(ar) #in place\n", "ar'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 137, "text": [ "1x7 Array{Int64,2}:\n", " 7 6 1 4 3 5 2" ] } ], "prompt_number": 137 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.20: Program words" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SKIP" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###PLEAC 4.21: Program permute" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SKIP" ] }, { "cell_type": "code", "collapsed": false, "input": [ ";ipython nbconvert 4_pleac_arrays.ipynb" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "[NbConvertApp] Using existing profile dir: u'C:\\\\Users\\\\keithc\\\\.ipython\\\\profile_default'\r\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "[NbConvertApp] Converting notebook 4_pleac_arrays.ipynb to html\r\n", "[NbConvertApp] Support files will be in 4_pleac_arrays_files\\\r\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "[NbConvertApp] Loaded template html_full.tpl\r\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "[NbConvertApp] Writing 334838 bytes to 4_pleac_arrays.html\r\n" ] } ], "prompt_number": 140 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 139 } ], "metadata": {} } ] }