catal
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 問題解決力を鍛える!アルゴリズムとデータ構造 ## 日程 2023/01/06 ## 第4.4章〜 - 任意参加 - ファシリテーター: Chen ### 話す・書く順番決めルーレット in Ruby ```ruby ['hsu', 'chen', 'teruya', 'kanazawa', 'okura', 'tsujita'].shuffle.each { |v| puts ">[name=#{v}]\n\n" } ``` in JavaScript ```javascript const l = ['hsu', 'chen', 'teruya', 'kanazawa', 'okura', 'tsujita'] l[Math.floor(Math.random()*l.length)] ``` ### ファシリテーター決め方 - `%w(hsu chen teruya kanazawa okura kanno)`の順番でやっていく ### 参加者がやること - 事前に決められた章を読む - 学んだことやわからなかったことを書き込む(任意) 当日 - 最初に感想や質問を書く時間を設ける(10分) - 時間枠 60分 - 延長枠 30分 ### 勉強会時にやること 各自学びや気付き、疑問を発表していく ファシリテーターがよしなに議論を進める 終了前に勉強会を振り返る?(KPTなどで)← 60分のときに仮締め ## 各自が書き込む場所 >[name=teruya] 4.4 メモ化 この部分は前回(トリボナッチだけど)メモ化版を作ったので再掲 ```ruby= def memo_trib(num) return 0 if num == 0 return 0 if num == 1 return 1 if num == 2 return $memo[num] if $memo[num] return $memo[num] = memo_trib(num - 1) + memo_trib(num - 2) + memo_trib(num - 3) end num = gets.to_i $memo = Array.new(num + 1) # $memo[0] = 0 # $memo[1] = 0 # $memo[2] = 1 puts memo_trib(num) p $memo ``` 4.5.1 部分和問題 説明を読むと難しく感じるが……再帰でひたすら分岐(数を選ぶ or 選ばない)して行って、 最終的に0を作れるかどうかという問題になる。 4.9 ```ruby= def func(checked_index, rest, nums) if checked_index == 0 return true if rest == 0 return false end # nums[checked_index - 1] を選ばない場合 return true if func(checked_index - 1, rest, nums) # nums[checked_index - 1] を選ばない場合 return true if func(checked_index - 1, rest - nums[checked_index - 1], nums) return false end nums = [3, 2, 6, 5] rest = 14 checked_index = nums.size # [3, 2, 6, 5] の1つ外側のindex puts func(checked_index, rest, nums) ``` 章末問題4.6 ```ruby= nums = [3, 2, 6, 5] rest = 14 # memo[いくつ目のnumまで調べたか][indexの数値を作れたか] memo = Array.new(nums.size + 1) { Array.new(rest + 1) {false} } memo[0][0] = true puts '実行前' memo.each { |row| p row.map { |bool| bool ? '⭕️' : '❎'} } puts nums.each_with_index do |num, index| memo[index].each_with_index do |bool, sum| next unless bool # num を足さない場合 memo[index + 1][sum] = true # num を足す場合 memo[index + 1][sum + num] = true if (sum + num) <= rest end end puts '実行後' memo.each { |row| p row.map { |bool| bool ? '⭕️' : '❎'} } puts puts memo.last.last ``` ![](https://i.imgur.com/Xyg2cTT.jpg) >[name=tsujita] > 4.4 メモ化 いわゆるキャッシュのこと、計算量が減る 再帰の中でうまく使うのがポイントというのはなんとなく理解しました > 4.6 分割統治法 確かに無意識に使っていた・・!と思ったけど、これをアルゴリズムと認識してロジックに落とし込める自信はないです >[name=okura] ### 4.4 メモ化して動的計画法へ キャッシュは効くときと効かないときの差が激しい気がしている。普通にRailsアプリを書いているとあまり意味がないことも多い。 ```ruby= # resultが複数回呼ばれないと効果がない def result @result ||= 'result' end # 引数があるのでこれはバグ def result2(arg) @result ||= "result#{arg}" end ``` ### 4.5 再帰の例(3):再帰関数を用いる全探索 こっちのほうがビット演算より可読性も高いと思う。「数を選ぶか選ばないかの2通り」をビット演算にエンコードするのは脳の負荷が高い… ### 4.6 分割統治法 分割統治法自体はとても汎用的な解法というか考え方だとは思うので、アルゴリズムが全面に出ない場合でも役に立ちそう。 ### 章末問題 メソッドではなくてprocを使うことで変数の数を減らす工夫をしています。procを実行するときは`foo.()`みたいにドットが必要になる。 #### 4.5 うまくいかなかったバージョン。 ```ruby= n = gets.chomp.to_i digit = n.digits.size base = [3, 5, 7] answers = [] 0.upto(digit - 2) do |i| [3, 5, 7].repeated_permutation(i) do |a| result = a.empty? ? base : base + a result.permutation do |r| answers << r if r.join.to_i <= n end end end answers.uniq! puts answers.size ``` ここでの敗因は`permutation`系のメソッドが実は結構重いことなんじゃないかと思っている。`repeated_permutation`を含めたループが3重になっているのがつらい。 うまくいったバージョン。 ```ruby= n = gets.chomp.to_i digit = n.digits.size result = 0 check = ->(num) { break 0 if num.join.to_i > n if num.include?(3) && num.include?(5) && num.include?(7) result += 1 end [3, 5, 7].each do |i| check.(num + [i]) end } [3, 5, 7].repeated_permutation(3) do |i| check.(i) end puts result ``` #### 4.6 ```ruby= a = gets.chomp.split(" ").map(&:to_i) w = 10000000 cache = {} check = ->(i, rest) { h = [i, rest].hash return cache[h] unless cache[h].nil? if i.zero? rest.zero? else if check.(i - 1, rest) cache[h] = true elsif check.(i - 1 , rest - a[i - 1]) cache[h] = true else cache[h] = false end end } puts check.(a.size, w) ``` ## 振り返り >[name=teruya] 二次元DPは図で描けるけど、三次元以上になると描けないので頭がぶっ壊れます >[name=tsujita] だんだんちゃんと難しくなってきた・・ > [name=okura] キャッシュは難しい。 これはあるあるだと思うけど、書いたコードが大規模なデータに耐えられるのかはしばしば実際に大規模なデータを投入してみるまでわからない(そして耐えられない)。 ## 終了後ファシリテーターがやること - 次回のファシリテーターを決める(2023/01/06) - - 読む範囲を決める - 第5章2(動的計画法の例題)まで

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully