FizzBuzzKotlin - A function fizzBuzz to generate a list of string based on the input number

Overview

FizzBuzzKotlin

write a function fizzBuzz to generate a list of string based on the input number

我的解法

class Solution {
    fun fizzBuzz(n: Int): List<String> {
        return (1.. n).map { i -> findResponse(i) }
    }
    private fun findResponse(n: Int): String {
        return when {
            n % 15 == 0 -> "FizzBuzz"
            n % 3 == 0 -> "Fizz"
            n % 5 == 0 -> "Buzz"
            else -> n.toString()
        }
    }
}

reference from My Teacher Recca Chao's solution

Recca Chao's kata fizz-buzz

透過 map, when 這樣的 operator 可以簡化程式碼, 增加可讀性

因為 map, when 都是屬於一種標準抽象化的operation

You might also like...
🪄 This is an animation library with an increasing number of TextViews
🪄 This is an animation library with an increasing number of TextViews

CountNumberEvent 🪄 This is an animation library with an increasing number of TextViews Demo Setup Add it in your root build.gradle at the end of repo

Send Whatsapp Message Without Saving Mobile Number

Send Whatsapp Message Without Saving Mobile Number In this project i created the

This project aims to provide a solution for finding the right product for a given EAN (European Article Number)

This project aims to provide a solution for finding the right product for a given EAN (European Article Number)

Counterpart of onTouchEvent for Jetpack Compose and transform gesture with specific number of pointers

Counterpart of onTouchEvent for Jetpack Compose and transform gestures notify when last pointer is up number of pointer that are down or list of pointers that can be used to get details of events when gesture is on

A project aiming to generate KMP declarations from several library versions

kotlin-ketchup A project aiming to generate KMP declarations from several library versions LICENSE Apache 2.0. See ./LICENSE in this repository See th

Used to generate the template code of GetX framework
Used to generate the template code of GetX framework

Language: English | 中文简体 statement some fast code snippet prompt come from getx-snippets-intelliJ Description install Plugin effect Take a look at the

A Kotlin library used to analyse discrete Markov chains, in order to generate plausible sequences

Markov Markov is a Kotlin library used to analyse discrete Markov chains, in order to generate plausible sequences. Using This project is still under

Ktorm KSP extension to help generate boilerplate code.
Ktorm KSP extension to help generate boilerplate code.

Ktorm KSP extension to help generate boilerplate code. It can automatically generate Table objects through entity classes, while making entities defined by data classes easier to use, and supports custom extension code generation logic.

A project that helps us generate the test project to test the Gradle plugin.

Ktlint Gradle Provides the function to generate a Gradle project for us to test your Gradle plugin Latest plugin version: [1.0.0] Table of content How

Owner
gson
2019.11-2021.10 RedPill(Backend Developer) 2017.10-2019.10 CoolBitx (Backend Developer) 2016.5~2017.10 大猩猩軟體工程師(Java Web) 2015.4~2016.4 凌群軟體工程師(Java Web)
gson
A custom view for rating which easy to make and use, but function is excellent

QRatingView A custom view for rating which easy to make and use, but function is excellent Effect Picture Properties <declare-styleable name="QRat

QCoder 1 Dec 3, 2021
Kotlin extension function provides a facility to "add" methods to class without inheriting a class or using any type of design pattern

What is Kotlin Extension Function ? Kotlin extension function provides a facility to "add" methods to class without inheriting a class or using any ty

mohsen 21 Dec 3, 2022
Automatically generates UI demos which allow users to call any function with any parameters

Automatically generates UI demos which allow users to call any function (including composable ones) with any parameters. Useful for building demo screens in playground apps of various design systems.

Anton Popov 3 Jul 28, 2022
Kotlin Multiplatform String markup library

Thistle Kotlin multiplatform String markup library, inspired by SRML. Thistle is a common parser which produces an AST that can be rendered to a varie

Copper Leaf 52 Dec 30, 2022
Nice String in Kotlin

Nice String We'll say a string is nice if at least two of the following conditio

Bunyod Rafikov 0 Dec 28, 2021
Map-vs-list-comparator - The project compares the time needed to find a given element in a map vs the time needed to find a given element in a list.

Map vs List Comparator The project compares the time needed to find a given element in a map vs the time needed to find a given element in a list. To

null 0 Jan 4, 2022
To-Do-List - Create a To Do List-style App from scratch and drive the entire development process using Kotlin

To-Do-List! Crie um App no estilo "To Do List" do zero e conduza todo o processo

River Diniz 0 Feb 14, 2022
A library that extends the existing JDBC API so that data objects can be used as input (to set parameters) and output (from ResultSet's rows).

SqlObjectMapper This is a library that extends the existing JDBC API so that data objects can be used as input (to set parameters) and output (from Re

Qualified Cactus 2 Nov 7, 2022
A Simple Android library to get the number of words and give you the time it will take you to finish an article/story.

MinRead A Simple Android library to get the number of words and give you the time it will take you to finish an article/story. Prerequisite Androidx K

Nwokocha wisdom maduabuchi 36 Nov 17, 2021