{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### ■二分探索" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n", "nil\n" ] } ], "source": [ "array = [1, 3, 5, 7, 9]\n", "\n", "p array.bsearch {|n| n > 6 }\n", "p array.bsearch {|n| n > 10}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil\n" ] } ], "source": [ "p array.bsearch {|n| n < 4 }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ■要素の連結" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"241365\"\n" ] }, { "data": { "text/plain": [ "\"24-1-365\"" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [24, 1, 365]\n", "p array.join\n", "array.join('-')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Python\n", "#'-'.join(list)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"24-1-365\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array*'-'" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[24, 1, 365, 24, 1, 365, 24, 1, 365]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array * 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-5-3 Hash" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "656715179053826656\n" ] }, { "data": { "text/plain": [ "2020695210800531113" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [1, 2]\n", "\n", "p array.hash\n", "array.reverse!\n", "array.hash" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "9605" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ ":foo.hash" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "9605" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ ":foo.hash" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1214599741921765595\n" ] }, { "data": { "text/plain": [ "-1214599741921765595" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p 'foo'.hash\n", "'foo'.hash" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### ■ 繰り返し処理" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one: 1\n", "two: 2\n", "one\n", "two\n", "1\n", "2\n" ] }, { "data": { "text/plain": [ "{:one=>1, :two=>2}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {one: 1, two: 2}\n", "\n", "hash.each do |k, v|\n", " puts \"#{k}: #{v}\"\n", "end\n", "\n", "hash.each_key do |k|\n", " puts k\n", "end\n", "\n", "hash.each_value do |v|\n", " puts v\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### ■値の更新/追加/削除" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{:foo=>\"bar\"}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {}\n", "hash[:foo] = 'bar'\n", "\n", "hash" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {foo: 'bar'}\n", "\n", "hash.delete(:foo)\n", "hash" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{:foo=>1, :buzz=>3}\n" ] }, { "data": { "text/plain": [ "{:bar=>2}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {foo: 1, bar: 2, buzz: 3}\n", "\n", "hash.select! {|k,v| v.odd?}\n", "p hash\n", "\n", "hash = {foo: 1, bar: 2, buzz: 3}\n", "\n", "hash.reject!{|k, v| v.odd?}\n", "hash" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{:foo=>1, :bar=>2, :buzz=>3}\n", "{:foo=>1, :bar=>2, :buzz=>3}\n" ] }, { "data": { "text/plain": [ "{:foo=>1, :bar=>2, :buzz=>3}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {foo: 1, bar: 2, buzz: 3}\n", "\n", "p hash.keep_if {|k, v| true}\n", "hash.delete_if {|k, v| false}" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil\n", "nil\n" ] } ], "source": [ "hash = {foo: 1, bar: 2, buzz: 3}\n", "p hash.select! {|k, v| true}\n", "\n", "hash = {foo: 1, bar: 2, buzz: 3}\n", "p hash.reject! {|k, v| false}" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{:foo=>1, :buzz=>3}\n", "{:bar=>2}\n" ] }, { "data": { "text/plain": [ "{:foo=>1, :bar=>2, :buzz=>3}" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {foo: 1, bar: 2, buzz: 3}\n", "\n", "p hash.select {|k,v| v.odd?}\n", "p hash.reject {|k,v| v.odd?}\n", "hash" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "次回は5-5-3Hash ■マージから" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{:a=>\"Alice\", :b=>\"Bob\", :c=>\"Chalie\"}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "names = {a: 'alice', b: 'bob', c: 'chalie'}\n", "\n", "names.transform_values {|name| name.capitalize}" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NoMethodError", "evalue": "undefined method `compact' for {:a=>\"alice\", :b=>\"bob\", :c=>nil}:Hash", "output_type": "error", "traceback": [ "\u001b[31mNoMethodError\u001b[0m: undefined method `compact' for {:a=>\"alice\", :b=>\"bob\", :c=>nil}:Hash", "\u001b[37m(pry):95:in `
'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/backend.rb:65:in `eval'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/bin/iruby:5:in `'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/bin/iruby:22:in `load'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/bin/iruby:22:in `
'\u001b[0m" ] } ], "source": [ "names = {a: 'alice', b: 'bob', c: nil}\n", "names.compact" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.4.0", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.4.0" } }, "nbformat": 4, "nbformat_minor": 1 }