๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution

Edabit Very Easy Swift

by Fomagran ๐Ÿ’ป 2020. 1. 19.
728x90
๋ฐ˜์‘ํ˜•

Generate a Countdown of Numbers in an Array

Example

countdown(5) โžž [5, 4, 3, 2, 1, 0] countdown(1) โžž [1, 0] countdown(0) โžž [0]

์ˆซ์ž๋ฅผ ๋„ฃ์œผ๋ฉด ์ˆซ์ž๋ถ€ํ„ฐ 0๊นŒ์ง€ ๋ฐฐ์—ด์—์„œ ๋„ฃ์–ด์„œ ๋ณด์—ฌ์ค˜ ๊ทธ๋Œ€์‹  ์ˆœ์„œ๋Š” ์ˆซ์ž -> 0 ์ˆœ์œผ๋กœ

๋‚ด ํ’€์ด

for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž์—์„œ 0๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋นผ์„œ ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค ์ƒ๊ฐ

func countdown(_ start: Int) -> [Int] {
    var arr = [Int]()
    for i in 0...start{
        arr.append(start-i)
    }
    return arr
}

๋” ์ข‹์€ ์ •๋‹ต

ํ•ต์‹ฌ ๋„ˆ reversed() ์•Œ์•„?

func countdown(_ start: Int) -> [Int] {
	return (0...start).reversed()
}
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€