๋ชฉ๋กFilter (4)

Focus On Develop ๐ŸคŸ๐ŸคŸ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฌธ์ž์—ด ๋‚ด p์™€ y์˜ ๊ฐœ์ˆ˜

๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์—†์ด p์™€ y๋ฅผ ์ฐพ์•„์•ผ ํ•˜๋ฏ€๋กœ, s๋ฅผ ์†Œ๋ฌธ์ž๋‚˜ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•œ ํ›„์— ์นด์šดํŠธ๋ฅผ ํ•˜๋ฉด ๋˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋‹ค! func solution(_ s:String) -> Bool { let string = s.lowercased() let pCnt = string.filter { $0 == "p" }.count let yCnt = string.filter { $0 == "y" }.count return pCnt == yCnt } ์‚ฌ์‹ค ๋‚˜๋Š” ํ•œ์ค„๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋„ ์ด๋ ‡๊ฒŒ ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์ต์ˆ™ํ•ด์ ธ์žˆ๋‹ค.(์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ์„ฑ์ด ์—†๋‹ค๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๊ณ ๋ คํ–ˆ์„๋•Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ƒ์—์„œ ๋ณ€์ˆ˜ ์„ ์–ธ์€ ์ตœ์†Œ๋กœ ํ•˜๋Š”๊ฒŒ ์ข‹๊ธด ํ•˜๊ฒ ๋‹ค.) ์—…๋ฌด๋ฅผ ํ•˜๋‹ค๊ฐ€ ์Šต๊ด€์ด ๋˜๋ฒ„๋ฆฐ.. ๐Ÿ˜ญ ๐Ÿ˜ญ ์•„๋ฌดํŠผ ์ด๋ ‡๊ฒŒ ํ’€๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ดค๋Š”๋ฐ, S..

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž ๋ฐฐ์—ด

์ด๋ฒˆ์—๋Š” ๋˜ ํ›„ํšŒํ•˜์ง€ ๋ง๊ณ  filter๋ฅผ ์ ๊ทน ํ™œ์šฉํ•ด์•ผ๊ฒ ๋‹ค!! (๋‚œ์ด๋„ ๋งค์šฐ ์‰ฌ์šด ๋ฌธ์ œ๋ฅผ ๋ณด๋‹ˆ ๋ฐ”๋กœ ๋– ์˜ค๋ฅด๋Š”๊ตฐ ใ…‹ใ…‹ ๐Ÿ‘๐Ÿ‘) returnํ•˜๋Š” ๋ฐฐ์—ด์ด ์ •๋ ฌ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ •๋ ฌ ์‹œ์ ์€ filter๋ฅผ ํ•˜๊ธฐ ์ „/ํ›„ ๋ฌด๊ด€ํ•  ๋“ฏ ํ•˜๋‹ค. func solution(_ arr:[Int], _ divisor:Int) -> [Int] { let divided = arr.filter { $0 % divisor == 0 }.sorted() return divided.count != 0 ? divided : [-1] } ์•„๋งˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ ํ’€๊ธฐ ์‹œ์ž‘ํ•œ ์ดํ›„๋กœ ์ œ์ผ ๋‹จ์‹œ๊ฐ„์— ํ’€์€ ๋ฌธ์ œ์ธ๋“ฏ..!? ใ…‹ใ…‹ ๋„ˆ๋ฌด ์‰ฌ์šด๊ฑฐ ์ฐพ์•„ํ‘ธ๋Š” ๋Š๋‚Œ์ด๋ผ ์˜ค๋Š˜์€ ๋ช‡๊ฐœ ๋” ํ’€์–ด์•ผ๊ฒ ๋‹ค ๐Ÿ˜‚ ๐Ÿ˜‚ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์‚ฌ์šฉ๋œ filter๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด?? ๐Ÿ‘‰ 2020/12..

[Swift] map, filter, reduce

Apple Developer Document์—์„œ map, filter, reduce์— ๋Œ€ํ•œ ์ •์˜๋ฅผ ๋ฐœ์ทŒํ•ด์˜ค๊ธด ํ–ˆ์ง€๋งŒ.. ์•„๋ฌด๋ž˜๋„ ๋‚˜๋Š” ์˜์–ด๋Š” ์–ด๋ ต๋”๋ผ. ๐Ÿ˜‚ ๐Ÿ˜‚ ๋‚˜์ค‘์—๋ผ๋„ ์‰ฝ๊ฒŒ ์ฐพ์•„๋ณด๋ ค๊ณ  ํ•œ๊ธ€๋กœ ์ •๋ฆฌํ•ด๋†”์•ผ์ง€ ใ…‹ใ…‹ ์ƒ์„ธ๋‚ด์šฉ : map | Apple Document 1. map ๊ทธ๋ ‡๋‹ค๊ณ  ํ•œ๋‹ค.. ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ํด๋กœ์ €(์กฐ๊ฑด)๋กœ ๋ฐฐ์—ด์˜ ๊ฐ ํ•ญ๋ชฉ๋“ค์„ ๋ฐ˜์˜ํ•œ ๊ฒฐ๊ณผ๋ฌผ์„ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์˜ˆ์‹œ) 2. filter ํด๋กœ์ €(์กฐ๊ฑด)๋กœ ๋ฐฐ์—ด์˜ ๊ฐ ํ•ญ๋ชฉ๋“ค์„ ๋น„๊ตํ•˜์—ฌ ์ผ์น˜ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฌผ์„ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์˜ˆ์‹œ) 3. reduce ํด๋กœ์ €(์กฐ๊ฑด)๋กœ ๋ฐฐ์—ด์˜ ๊ฐ ํ•ญ๋ชฉ๋“ค์„ ์žฌ๊ท€์ ์œผ๋กœ ์ ์šฉ์‹œ์ผœ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์ดˆ๊ธฐ๊ฐ’, ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํด๋กœ์ € ์˜ˆ์‹œ)

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฒด์œก๋ณต

์šฐ์„  ์ œํ•œ์‚ฌํ•ญ์„ ๋ณด๋ฉด ์ „์ฒด Input์˜ ์ˆ˜๋Š” ํฌ์ง€ ์•Š์•„์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๋Š” ํฌ๊ฒŒ ๋ฌธ์ œ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์ง€๋Š” ์•Š๋‹ค. ๋„๋‚œ๋‹นํ•œ ํ•™์ƒ, ์—ฌ๋ฒŒ์˜ ์ฒด์œก๋ณต์„ ๊ฐ€์ง„ ํ•™์ƒ์ด ๊ฐ๊ฐ ๋ฐ˜๋“œ์‹œ ์กด์žฌํ•˜๋‹ˆ, ์—†๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋”ฐ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™๋‹ค. (๋ณดํ†ต ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€๋•Œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๋๋ถ€๋ถ„ ์กฐ๊ฑด(?) ์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•ด์ค˜์•ผ ํ•  ๋ถ€๋ถ„์ด ์ข…์ข… ์ƒ๊ธฐ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.) reserve๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์—ฌ๋ฒŒ์ด ์žˆ๋Š” ํ•™์ƒ์ด ๋„๋‚œ๋‹นํ•œ ํ•™์ƒ(lost)์—๊ฒŒ ๋นŒ๋ ค์ค€๋‹ค๊ณ  ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€ ๊ฒƒ์ด๋‹ค. ๋จผ์ € reserve์— ์žˆ๋Š” ํ•™์ƒ์ด lost์—๋„ ์žˆ๋Š”์ง€ ์ฒดํฌ (์—ฌ๋ฒŒ์ด ์žˆ์–ด๋„ ๋„๋‚œ๋‹นํ–ˆ์œผ๋ฉด ๋นŒ๋ ค์ค„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ) ํ•˜๊ณ , ๋ณธ์ธ ์•ž -> ๋’ค ์ˆœ์„œ๋กœ lost์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด ๋นŒ๋ ค์ฃผ๊ณ , ๋„๋‚œ๋‹นํ•œ ๋ชฉ๋ก์—์„œ ์ง€์›Œ์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค. ์—ฌ๊ธฐ์„œ ๋นŒ๋ ค์ฃผ๋Š”..