CodingTour
ARTS #139

Algorithm

本周选择的算法题是:Detect Capital

规则

We define the usage of capitals in a word to be right when one of the following cases holds:

  • All letters in this word are capitals, like "USA".
  • All letters in this word are not capitals, like "leetcode".
  • Only the first letter in this word is capital, like "Google".

Given a string word, return true if the usage of capitals in it is right.

Example 1:

Input: word = "USA"
Output: true

Example 2:

Input: word = "FlaG"
Output: false

Constraints:

  • 1 <= word.length <= 100
  • word consists of lowercase and uppercase English letters.

Solution

class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        isupper = word[-1].isupper()
        for i in range(len(word) - 2, -1, -1):
            if word[i].isupper() != isupper and (isupper or i != 0):
                return False
        return True

Review

Spotify System Architecture

内容和我想象的完全不一样,通篇如同广告软文一样充斥着各种各样的链接,此外就是一些废话,比如功能性需求和非功能性需求之间的差异,无非是 Usability、Reliability、Good Performance、Low latency 之类的。

文章中提到 Erik Bernhardsson 花了6年时间开发 Spotify 的音乐推荐算法,这篇 2013 年的文章 Model benchmarks 揭示了部分故事。

Spotify 作为顶级互联网公司还有两个著名的开源作品:

  • Annoy - 性能非常好的高维稠密向量聚类或距离检索算法
  • Luigi - 流式任务调度框架

Tip

尝试用 antd protable 实现一个复杂的场景,太方便了,不到30行代码就可以搞定~

Share

Kotlin Native 支持的平台很全了:

  • JVM
  • JS
  • Android / Android NDK
  • Apple
  • Linux
  • Windows
  • WebAssembly

而且 Kotlin 可以跑在 JVM 上,也能调用 Java 代码,但 Kotlin 并不是 Java,借助于 LLVM,纯 Kotlin 代码能直接编译成平台原生代码,实现无 VM 跨平台。

一个 KMM 项目会默认创建三个目标平台:

  • common
  • android
  • iOS

它们之间的关系是:

common 存放通用代码,最核心的逻辑应该放在这里,如果想使用平台特定的能力/接口,可以借助 shortcuts 继承 common 来实现:

代码上就像这样:

// Common
expect fun randomUUID(): String

// Android
import java.util.*
actual fun randomUUID() = UUID.randomUUID().toString()

// iOS
import platform.Foundation.NSUUID
actual fun randomUUID(): String = NSUUID().UUIDString()

Kotlin 在 JetBrains 和 Google 的大力支持下,已经是一个很成熟的跨平台方案了,既有高级语言的特性,又能完美兼容平台本身的能力,希望未来能有更多像 Ktor 这样的库,持续建设生态。