Skip to content

讀書會第 24次聚會 20210406 #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 6, 2021
Merged

讀書會第 24次聚會 20210406 #16

merged 12 commits into from
Apr 6, 2021

Conversation

ytyubox
Copy link
Member

@ytyubox ytyubox commented Mar 23, 2021

LeetCode 讀書會第 24次聚會

leetcode 讀書會通知

  1. 項目: 第 24 次聚會

  2. 目的: 線上一起寫題目, 由有想法的人帶領, 先解題, 再看該題有趣的解法

  3. 時間: 4/06 (二) 20:00 ~ 21:00

  4. 地點: google meet 線上 (前 10 分鐘預備鏈接)

  5. 解題項目: https://leetcode.com/explore/learn/card/binary-search/126/template-ii/

  6. 共筆: GitHub https://github.com/programmingbookclub/Leetcode-club

@ytyubox ytyubox changed the title meetup 24 20210406 讀書會第 24次聚會 20210406 Mar 23, 2021
@SamTienz SamTienz self-requested a review March 31, 2021 02:29
@ytyubox
Copy link
Member Author

ytyubox commented Apr 3, 2021

補充算數平均數如何使用 Iteration 來避免 overflow

考慮給定 array of k where k == Int.max / 2:
[k, k, k, k] 的 算數平均數 應為 4 * k / 4 = k,然而 4 * k 為 4*(1/2) == 2 * Int.max 則有問題。

http://www.heikohoffmann.de/htmlthesis/node134.html

// swift
func average(_ arr: [Int], after: (Double) -> Void) -> Double {
    var r:Double = 0
    for (i, v) in arr.enumerated() {
        r += (Double(v) - r) / Double(i + 1)
        after(r)
    }
    return r
}

Example:

// Swift
let sut = [1, 2, 4, 5]
var captured = [Double]()
let result = average(sut, after: { i in
     captured.append(i)
}) 
XCTAssertEqual(result, 3.0)
XCTAssertEqual(captured, [1.0, 1.5, 2.3333333333333335, 3.0])

Copy link
Member Author

@ytyubox ytyubox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danishuotw
我發現我自己在使用 while 的時候,不太喜歡 繼續邏輯,而是使用 阻擋邏輯,例如
while (left < right) 我喜歡寫成 while true { if left >= right {break} ....
我的那個有專有名詞 a loop and a half
提出來看看有沒有討論空間。

Copy link
Contributor

@louis222220 louis222220 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Search for a Range in TypeScript

@louis222220
Copy link
Contributor

@ytyubox
我反而是喜歡用你所謂的 繼續邏輯
我認為可讀性比較高,可以比較清楚找到跳出迴圈的條件,

不過我有時會再加上 反邏輯,通常是邏輯判斷比較複雜的時候,
i.e. while( !(跳出邏輯) )

Example:

// 當 leftIndex > rightIndex、或是 left 的值跟 target 相等時,就離開迴圈
while (!(leftIndex > rightIndex || nums[leftIndex] === targetValue)) {
  ...
  ...
}

我發現我自己在使用 while 的時候,不太喜歡 繼續邏輯,而是使用 阻擋邏輯,例如
while (left < right) 我喜歡寫成 while true { if left >= right {break} ....

@danishuotw

This comment has been minimized.

@danishuotw danishuotw closed this Apr 6, 2021
@ytyubox ytyubox reopened this Apr 6, 2021
@danishuotw
Copy link
Contributor

@danishuotw
我發現我自己在使用 while 的時候,不太喜歡 繼續邏輯,而是使用 阻擋邏輯,例如
while (left < right) 我喜歡寫成 while true { if left >= right {break} ....
我的那個有專有名詞 a loop and a half
提出來看看有沒有討論空間。

@ytyubox
其實我第一次看到你寫 while(true) 我就覺得可讀性很好。
但我在思考的時候,還是習慣用 阻擋邏輯 😓
我之後再多練習 a loop and a half ,慢慢改過來,感謝您的建議 👍

@ytyubox
Copy link
Member Author

ytyubox commented Apr 6, 2021

@louis222220 關於你的反邏輯:

// 當 leftIndex > rightIndex、或是 left 的值跟 target 相等時,就離開迴圈
while (!(leftIndex > rightIndex || nums[leftIndex] === targetValue)) {
  ...

我看到你用 De Morgan's laws(德摩根定律) 來處理,我的意思是,兩個都可以,你選擇可讀性比較好就行了。

@ytyubox
Copy link
Member Author

ytyubox commented Apr 6, 2021

@danishuotw 這個我就加進來喔,你還是可以繼續討論,不過因為範圍已經過去了,要用留言的方式聊了。
#16 (comment)

@ytyubox ytyubox merged commit d233deb into main Apr 6, 2021
@ytyubox ytyubox deleted the meet-up-24 branch April 6, 2021 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants