Instantly share code, notes, and snippets.
Last active
July 4, 2016 14:02
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save yoon/b1ae61a5ec6d2b19eb3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Menu exercise in Julia" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Requests package" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"\\$15.05\\nmixed fruit,\\$2.15\\nfrench fries,\\$2.75\\nside salad,\\$3.35\\nhot wings,\\$3.55\\nmozzarella sticks,\\$4.20\\nsampler plate,\\$5.80\\n\"" | |
] | |
}, | |
"execution_count": 66, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Pkg.add(\"Requests\")\n", | |
"using Requests\n", | |
"import Requests: get, post, put, delete, options\n", | |
"menu_url = \"https://gist.github.com/yoon/2cc42ef2e9ecfa9ec13b\"\n", | |
"menu = readall(get(menu_url))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Importing menu" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1505,Dict{Any,Any}(\"mixed fruit\"=>215,\"side salad\"=>335,\"french fries\"=>275,\"mozzarella sticks\"=>420,\"sampler plate\"=>580,\"hot wings\"=>355))" | |
] | |
}, | |
"execution_count": 61, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lines = split(menu, \"\\n\")\n", | |
"target_price = round(Int, parse(Float64, match(r\"\\d+\\.\\d{2}\", shift!(lines)).match) * 100)\n", | |
"menu_items = Dict()\n", | |
"for line in lines\n", | |
" splits = split(line, \",\")\n", | |
" price = match(r\"\\d+\\.\\d{2}\", pop!(splits))\n", | |
" item = join(splits)\n", | |
" if sizeof(price) > 0 && sizeof(item) > 0\n", | |
" menu_items[item] = round(Int, parse(Float64, string(price.match)) * 100)\n", | |
" end\n", | |
"end\n", | |
"target_price, menu_items" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Solutions functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"solutions (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 62, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function solutions(target, items)\n", | |
" items_within = within(target, items)\n", | |
" combinations = []\n", | |
" for (k,v) in items_within\n", | |
" if target == v\n", | |
" push!(combinations, Dict(k => v))\n", | |
" else\n", | |
" for combination in solutions(target - v, items_within)\n", | |
" push!(combinations, cat(1, [Dict(k => v)], combination))\n", | |
" end\n", | |
" end\n", | |
" end\n", | |
" combinations\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"within (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 63, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function within(target,items)\n", | |
" filter((k,v) -> v <= target, items)\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"13-element Array{Any,1}:\n", | |
" [Dict(\"mixed fruit\"=>215),Dict(\"mixed fruit\"=>215),Dict(\"mixed fruit\"=>215),Dict(\"mixed fruit\"=>215),Dict(\"mixed fruit\"=>215),Dict(\"mixed fruit\"=>215),Dict(\"mixed fruit\"=>215)]\n", | |
" [Dict(\"mixed fruit\"=>215),Dict(\"sampler plate\"=>580),Dict(\"hot wings\"=>355),Dict(\"hot wings\"=>355)] \n", | |
" [Dict(\"mixed fruit\"=>215),Dict(\"hot wings\"=>355),Dict(\"sampler plate\"=>580),Dict(\"hot wings\"=>355)] \n", | |
" [Dict(\"mixed fruit\"=>215),Dict(\"hot wings\"=>355),Dict(\"hot wings\"=>355),Dict(\"sampler plate\"=>580)] \n", | |
" [Dict(\"sampler plate\"=>580),Dict(\"mixed fruit\"=>215),Dict(\"hot wings\"=>355),Dict(\"hot wings\"=>355)] \n", | |
" [Dict(\"sampler plate\"=>580),Dict(\"hot wings\"=>355),Dict(\"mixed fruit\"=>215),Dict(\"hot wings\"=>355)] \n", | |
" [Dict(\"sampler plate\"=>580),Dict(\"hot wings\"=>355),Dict(\"hot wings\"=>355),Dict(\"mixed fruit\"=>215)] \n", | |
" [Dict(\"hot wings\"=>355),Dict(\"mixed fruit\"=>215),Dict(\"sampler plate\"=>580),Dict(\"hot wings\"=>355)] \n", | |
" [Dict(\"hot wings\"=>355),Dict(\"mixed fruit\"=>215),Dict(\"hot wings\"=>355),Dict(\"sampler plate\"=>580)] \n", | |
" [Dict(\"hot wings\"=>355),Dict(\"sampler plate\"=>580),Dict(\"mixed fruit\"=>215),Dict(\"hot wings\"=>355)] \n", | |
" [Dict(\"hot wings\"=>355),Dict(\"sampler plate\"=>580),Dict(\"hot wings\"=>355),Dict(\"mixed fruit\"=>215)] \n", | |
" [Dict(\"hot wings\"=>355),Dict(\"hot wings\"=>355),Dict(\"mixed fruit\"=>215),Dict(\"sampler plate\"=>580)] \n", | |
" [Dict(\"hot wings\"=>355),Dict(\"hot wings\"=>355),Dict(\"sampler plate\"=>580),Dict(\"mixed fruit\"=>215)] " | |
] | |
}, | |
"execution_count": 64, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"solutions(target_price, menu_items)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"collapsed": false | |
}, | |
"source": [ | |
"## References\n", | |
"* http://www.juliabox.org/\n", | |
"* http://learnxinyminutes.com/docs/julia/\n", | |
"* http://en.wikibooks.org/wiki/Introducing_Julia\n", | |
"* http://docs.julialang.org/en/release-0.4/\n", | |
"* http://pkg.julialang.org/" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Julia 0.4.3", | |
"language": "julia", | |
"name": "julia-0.4" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"mimetype": "application/julia", | |
"name": "julia", | |
"version": "0.4.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment