0%

ARTS 2020 Week 02

Algorithm

你让某些人为你工作了七天, 你要用一根金条作为报酬。这根金条要被分成七块。你必须在每天的活干完后交给他们一块。如果你只能将这根金条切割两次,你怎样给这些工人分?

分析:严格来说应该是智力题,不是算法题。
解决方法是切两次,把金条分成1/7,2/7,4/7三份,编号a,b,c

第一天,给a

第二天,给b,拿回a

第三天,给a

第四天,给c,拿回a,b

第五天,给a

第六天,给b,拿回a

第七天,给a

Review

Settling the Async-Await v withContext debate in Kotlin Coroutines

这篇文章主要讲了 Kotlin 协程中 Async-Await 和 withContext 的对比及使用场景。

1
2
3
4
5
6
7
fun main() = runBlocking {
println("I am working")
val opOne = async { operationOne() }.await()
val opTwo = async { operationTwo() }.await()
println("Done working.")
println("The multiplied result is ${opOne * opTwo}")
}

和下面的代码是等效的:

1
2
3
4
5
6
7
fun main() = runBlocking {
println("I am working")
val opOne = withContext(IO) { operationOneWithContext() }
val opTwo = withContext(IO) { operationTwowithContext() }
println("Done working.")
println("The multiplied result is ${opOne * opTwo}")
}

两种方法是差不多的,区别是 withContext 需要强制指定一个 Context。1.2.50 版本以后的推荐直接用第二种方法,更简洁一些。

但是文章有一点没说清楚,就是上面两个操作实际上是顺序执行的,如果希望同时执行的话,还是需要用 async,如下:

1
2
3
4
5
6
7
8
fun main() = runBlocking {
println("I am working")
val jobOne = async { operationOne() }
val jobTwo = async { operationTwo() }
jobOne.await()
jobTwo.await()
println("Done working.")
}

Tip

Android 提供给应用的存储目录主要分内部和外部两种,早期外部存储是 SD 卡的形式,现在基本都是内置的拓展存储了,这导致一些 API 比较容易混淆,这里记录一下。

内部存储放置在内部存储,不能被外部应用访问到,分 file 和 cache 两种,cache 可以被系统清理掉,只适合放一些临时文件。

外部存储是放在 SD卡/拓展存储,可以被外部的应用访问到,也分 file 和 cache 两种。有些常量定义了常用的 file 类型子目录名称,见下面的 Gist。

对非应用目录的外部存储目录,例如相册等写入是需要 WRITE_EXTERNAL_STORAGE 权限的。

All Android Directory Path 是一段方便快速查询的 gist,很实用,可以快速查询存储目录的 API 和参数以及所需权限。

Share

《枪炮、病菌与钢铁》读书笔记