typescript综合练习1(展开音乐播放列表)

Playlist Soundness

What’s up, friend?!
I’m so pumped you’re joining us.
We’ve got a sick project we could totally use your help on!

See, someone’s giving us amazing recommendations for songs to play.
But they’re not just coming in as songs.
Sometimes they’re an album containing a array of songs.
And sometimes they’re a playlist with a method that returns an array of songs.

We’d like you to write a function for us that takes in an array of those items and returns a result playlist.
The result playlist should keep track of which songs appear under each artist, the in-order list of songs, and the total length of time across the playlist?

Can you do this for us, please?
Friend?

Setup

In one terminal, run the TypeScript compiler via the tsc script.
For example, to start the TypeScript compiler in watch mode:

npm run tsc -- --watch

In another terminal, run Jest via the test script.
For example, to start tests in watch mode:

npm run test -- --watch

Specification

Your code should export an unrollPlaylist function with an explicit return type annotation.

Parameters:

  1. items: An array where each element is either a Song, Album, or Playlist

    See ./index.test.ts for examples of data.

Return type: an object with…

  • artists: Object with artist names keyed to the array of songs they’re credited on
  • songs: An array of Songs
  • time: Total length of time across all songs

Notes

  • Don’t import code from one step into another.

代码

export interface Song {
	artist: string | string[];
	length: number;
	name: string;
	type: "song";
}

export interface Album {
	songs: Song[];
	type: "album";
}

export interface Playlist {
	resolve(): Song[];
	type: "playlist";
}

export type PlaylistItem = Album | Song | Playlist;

export interface Artists {
	[i: string]: string[];
}

export interface UnrolledPlaylist {
	artists: Artists;
	songs: string[];
	time: number;
}

export function unrollPlaylist(items: PlaylistItem[]): UnrolledPlaylist {
	const artists: Artists = {};
	const songs: string[] = [];
	let time = 0;

	function addSong(song: Song) {
		const songArtists =
			typeof song.artist === "string" ? [song.artist] : song.artist;

		for (const artist of songArtists) {
			artists[artist] ??= [];
			artists[artist].push(song.name);
		}

		time += song.length;
		songs.push(song.name);
	}

	for (const item of items) {
		switch (item.type) {
			case "song":
				addSong(item);
				break;

			case "album":
				item.songs.forEach(addSong);
				break;

			case "playlist":
				item.resolve().forEach(addSong);
				break;
		}
	}

	return { artists, songs, time };
}


let items : PlaylistItem[] = [
    {
        songs: [
            {
                artist: ["Queen"],
                length: 223,
                name: "Death On Two Legs (Dedicated to...)",
                type: "song",
            },
            {
                artist: "Tenacious D",
                length: 247,
                name: "Tribute",
                type: "song",
            },
            {
                artist: ["Queen"],
                length: 68,
                name: "Lazing on a Sunday Afternoon",
                type: "song",
            },
        ],
        type: "album",
    },
]

let unrolledPlaylist: UnrolledPlaylist = unrollPlaylist(items)
console.log(unrolledPlaylist)

输出

{
  artists: {
    Queen: [
      'Death On Two Legs (Dedicated to...)',
      'Lazing on a Sunday Afternoon'
    ],
    'Tenacious D': [ 'Tribute' ]
  },
  songs: [
    'Death On Two Legs (Dedicated to...)',
    'Tribute',
    'Lazing on a Sunday Afternoon'
  ],
  time: 538
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/604057.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

CSRF漏洞简介

csrf简介 CSRF 全称为跨站请求伪造( Cross-site request forgery ),是一种网络攻击方式,在 CSRF 的攻击场景中攻击者会伪造一个请求(这个请求一般是一个链接),然后欺骗目标用户进行点击&#xf…

C51版本Keil + STC-ISP 实现第一盏灯,从创建到实现

创建项目 1. 新建项目 Project -> New uVision Project 2.1 新建文件夹 2.2 输入文件名称, 并保存 3.1 选择当前位STC芯片的开发板,选择STC MCU Database 搜素具体芯片型号,进行配置: 3.2 选择通过搜索框搜索到stc相关芯片信息 如果st…

linux数据备份与恢复

目录 前言 1、数据备份和恢复中的两个关键性指标 2、linux系统的定时任务 1)本地定时任务crontab 在实验测试过程中,遇到多次crontab任务不执行问题 ,总结下来主要有几个方面原因: 2)分布式定时任务系统Jenkins 3、备份存储…

机房——蓝桥杯十三届2022国赛大学B组真题

问题分析 这题用深搜广搜都能做,不过我更倾向于用广搜,因为广搜能更容易找到目标点。那么是采用结构体存储边还是采用二维数组存储临接矩阵呢?我们注意到n的取值范围为1e5,用二维数组哪怕是bool类型就需要至少1e10Byte的连续空间,这个空间太大…

为软件教学文档增加实践能力

为了更方便软件教学,我们在凌鲨(OpenLinkSaas)上增加了公共资源引用的功能。 目前可以被引用的公共资源: 微应用常用软件公共知识库Docker模板 引用公共资源 引用微应用 目前微应用包含了主流数据库,终端等工具,可以方便的进行各种相关实…

【25届秋招备战C++】23种设计模式

【25届秋招备战C】23种设计模式 一、简介程序员的两种思维8大设计原则 二、具体23种设计模式2.1 创建型模式2.2 结构性模式2.3 行为型模式 三、常考模式的实现四、参考 一、简介 从面向对象谈起, 程序员的两种思维 底层思维:向下 封装:隐藏内部实现 多…

ASP.NET小型证券术语解释及翻译系统的设计与开发

摘 要 在系统设计上,综合各种翻译类型网站优缺点,设计出具有任何使用者都可添加术语信息的且只有管理员能够实现术语修改及删除等独特方式的术语查看管理系统。此方式能够使术语量快速增大,并且便于使用者及管理员操作,满足相互…

软件设计师-应用技术-面向对象程序设计题5

考题形式: 代码填空,5 - 6空,每空3分。 基础知识及技巧: 1. 类的定义: 2. 接口的定义: 给实现类具体代码,填写接口中方法。 3. 类、抽象类、继承类、抽象方法的定义: 抽象类&…

【管理咨询宝藏95】SRM采购平台建设内部培训方案

本报告首发于公号“管理咨询宝藏”,如需阅读完整版报告内容,请查阅公号“管理咨询宝藏”。 【管理咨询宝藏95】SRM采购平台建设内部培训方案 【格式】PDF版本 【关键词】SRM采购、制造型企业转型、数字化转型 【核心观点】 - 重点是建设一个适应战略采…

20240508请问GTX2080TI的300和300A核心的差异?

20240508请问GTX2080TI的300和300A核心的差异? 在拼多多/淘宝上,GTX2080TI的300A核心的会比300核心的贵100¥左右。 但是怎么区分呢? 300a核心和300请问怎么区分呢?[嘻嘻] devicr ID diviceid 1e07是300a 1e04是300 Gp…

2024041702-计算机操作系统 - 死锁

计算机操作系统 - 死锁 计算机操作系统 - 死锁 必要条件处理方法鸵鸟策略死锁检测与死锁恢复 1. 每种类型一个资源的死锁检测2. 每种类型多个资源的死锁检测3. 死锁恢复 死锁预防 1. 破坏互斥条件2. 破坏占有和等待条件3. 破坏不可抢占条件4. 破坏环路等待 死锁避免 1. 安全状态…

使用 Parallels Desktop 在 Mac 上畅玩 PC 游戏

我们不再需要接受 “Mac 不是为游戏而打造” 这一事实;Parallels Desktop 通过将电脑变成高性能的游戏设备,从而改变了一切。 Parallels Desktop 充分利用 Mac 硬件的强大功能,让您无缝畅玩 Windows 专享游戏。 性能得到提升,可玩…

基于 llama2 的提示词工程案例2

优化大型语言模型(LLMs) 优化大型语言模型(LLMs)中的提示词(prompts)是提高模型性能和输出相关性的重要手段。以下是一些优化提示词的方向: 明确性:确保提示词清晰明确,…

数据湖与数据网格:引领组织数据策略的未来

十多年来,组织已经采用数据湖来克服数据仓库的技术限制,并发展成为更加以数据为中心的实体。虽然许多组织已经使用数据湖来探索新的数据用例并改进其数据驱动的方法,但其他组织发现所承诺的好处很难实现。因此,许多数据湖计划的有…

SOLIDWORKS Electrical电气元件智能开孔

实际的电气元器件安装中,一些元器件需要穿过孔洞安装,例如按钮、指示灯会在配电柜的控制面板上,需要穿过控制面板安装。这部分内容放在软件建模、装配时,往往比较复杂因为考虑孔的大小符合元器件规格、孔跟随元器件移动、同一元器…

CR80清洁卡的重要性

在我们日常生活中,身份证、银行卡、信用卡等塑料卡片已经成为了不可或缺的一部分。这些卡片通常符合CR80标准,这意味着它们的尺寸和厚度符合国际标准,为了保证这些卡片的读取和使用效果,清洁维护显得尤为重要。 什么是CR80卡&…

Linux学习之禁用防火墙

查看防火墙状态 systemctl status firewalld.service 第一行前面的圆圈是有颜色的就是开启状态 黑色的就是关闭状态 关闭防火墙 systemctl stop firewalld.service 输入密码认证 再次查看防火墙状态 systemctl status firewalld.service 第一行前面的圆圈变成黑色说明关闭…

杰理-701-单线灯-ws2812-驱动

杰理-701-单线灯-ws2812-驱动 LED_gradual_open(); //调用后 呼吸灯 set_led_colour(R,G,B);//具体颜色 spi_dma_set_addr_for_isr //spi 配置dma 后灯才亮 #define LED_H 0x7c #define LED_L 0x40 发送高位和地位的字节,具体…

UP互助 帮助UP起号做视频 支持B站和抖音

【软件名字】:UP互助 【软件版本】:1.0 【软件大小】:17.5MB 【软件平台】:安卓 【测试机型】:小米9 1.随便登个邮箱,添加自己平台的频道,然后就可以帮助别人,添加频道后在添加…

仓库管理系统需求调研要点

仓库管理系统需求调研 一、仓库的作用 仓库分类 原材料仓库:用于存放生产所需的原材料和零部件,需要保持原材料的质量和数量稳定。半成品仓库:存放生产过程中的半成品和在制品,需要保持良好的生产流程和及时出库。成品仓库&#x…
最新文章