首页 > 解决方案 > 无法理解社区答案在此打字稿练习作业中的工作原理

问题描述

我通过练习练习从头开始学习打字稿

我成功解决了Pangram的第5次练习

我的解决方案如下图

class Pangram {

     alphabet = "abcdefghijklmnopqrstuvwxyz"

    constructor(private pangram:string) {
        this.pangram = pangram.toLowerCase().normalize()
    }

    isPangram():boolean{
        for( let leter of this.alphabet){
            if(this.pangram.indexOf(leter) < 0) return false 
        }
        return true;
    }
}

export default Pangram

为了变得更好,我正在尝试查看其他解决方案来学习,这个引起了我的注意,因为我不擅长正则表达式(还)

class Pangram {
    constructor(private text:string) {
    }

    isPangram():boolean {
        var set = new Set(this.text.toLowerCase().replace(/[^A-Za-z]/g, ''))
        return set.size == 26        
    }
}

export default Pangram

我没有得到这个好,因为第一次阅读我虽然这会用空字符替换文本中的所有字母所以我认为他会检查大小等于零为什么,他正在检查 26 这真的如何工作,我不'不知道如何调试我只有一个测试可以运行以确保它工作正常,他的解决方案工作得很好

请在第二个解决方案中真正发生了什么

我正在为好奇的读者添加此作业的使用过的单元测试集

import Pangram from './pangram'

describe('Pangram()', () => {
  it('empty sentence', () => {
    const pangram = new Pangram('')
    expect(pangram.isPangram()).toBe(false)
  })

  it('pangram with only lower case', () => {
    const pangram = new Pangram("the quick brown fox jumps over the lazy dog")
    expect(pangram.isPangram()).toBe(true)
  })

  it("missing character 'x'", () => {
    const pangram = new Pangram("a quick movement of the enemy will jeopardize five gunboats")
    expect(pangram.isPangram()).toBe(false)
  })

  it("another missing character 'x'", () => {
    const pangram = new Pangram("the quick brown fish jumps over the lazy dog")
    expect(pangram.isPangram()).toBe(false)
  })

  it("pangram with underscores", () => {
    const pangram = new Pangram("the_quick_brown_fox_jumps_over_the_lazy_dog")
    expect(pangram.isPangram()).toBe(true)
  })

  it("pangram with numbers", () => {
    const pangram = new Pangram("the 1 quick brown fox jumps over the 2 lazy dogs")
    expect(pangram.isPangram()).toBe(true)
  })

  it('missing letters replaced by numbers', () => {
    const pangram = new Pangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog")
    expect(pangram.isPangram()).toBe(false)
  })

  it('pangram with mixed case and punctuation', () => {
    const pangram = new Pangram("\"Five quacking Zephyrs jolt my wax bed.\"")
    expect(pangram.isPangram()).toBe(true)
  })

  it('pangram with non-ascii characters', () => {
    const pangram = new Pangram("Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.")
    expect(pangram.isPangram()).toBe(true)
  })
})

谢谢

标签: regextypescriptset

解决方案


在行

var set = new Set(this.text.toLowerCase().replace(/[^A-Za-z]/g, ''))

正则表达式匹配所有非字母字符(或更准确地说,所有不在ISO 基本拉丁字母表中的字符)。因此传递给Set构造函数的字符串将包含字符串中所有字母字符的小写版本text

构造Set函数接受一个可迭代的参数并遍历它,只将它的每个不同元素中的一个添加到集合中。字符串是一个可迭代对象,其元素是字符串中的字符。所以new Set("pangram")将是一个Set正好包含 6 个元素"p", "a", "n", "g", "r", 和"m". 请注意,它有 6 个,而不是 7 个元素。该字符串"pangram"有两个 letter 实例"a",但 aSet只包含唯一/不同的元素。因此(new Set("pangram")).size6

小写基本拉丁字母表中正好有 26 个不同的字母。如果将仅包含小写字母的字符串传递给Set构造函数,则size结果的 theSet不能超过 26。如果正好是 26,则意味着 26 个字母中的每一个必须至少存在一个……意思是原始字符串是一个pangram。如果小于 26,则原始字符串必须至少缺少 26 个字母表中的一个,这意味着它不是一个 pangram。

因此,str => new Set(str.toLowerCase().replace(/[^a-z]/g,'')).size === 26这是一种编写函数来分类其输入字符串是否为 pangram 的简洁方法。简洁有其优点,尽管它在极端化时可能是荒谬的。

好的,希望对您有所帮助。祝你编码好运!


推荐阅读