๐Ÿง  ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž ๋ฐฐ์—ด

๋ฌธ์ œ

array์˜ ๊ฐ element ์ค‘ divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” element๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด ๋ฐฐ์—ด์— -1์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜๋ผ.

  • arr์€ ์ž์—ฐ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์ด๋‹ค.
  • ์ •์ˆ˜ i, j์— ๋Œ€ํ•ด i โ‰  j ์ด๋ฉด arr [i] โ‰  arr [j]์ด๋‹ค.
  • divisor๋Š” ์ž์—ฐ์ˆ˜์ด๋‹ค.
  • array๋Š” ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฐฐ์—ด์ด๋‹ค.

๋‚˜์˜ ํ’€์ด

def num_array(arr, divisor):
    answer = [a for a in arr if a % divisor == 0]
    return sorted(answer) if answer else [-1]

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋‚˜๋ฆ„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€์–ด๋‚ธ ๊ฒƒ ๊ฐ™๋‹ค. (๋ฟŒ๋“ฏ) ์ตœ๋Œ€ํ•œ ์งง๊ฒŒ ์จ๋ณด๋ ค๊ณ  ๋…ธ๋ ฅํ–ˆ๋‹ค.

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

def num_array(arr, divisor): return sorted([n for n in arr if n%divisor == 0]) or [-1]

์ด ๋ถ„์ฒ˜๋Ÿผ or๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฆฌ์ŠคํŠธ์˜ ์œ ๋ฌด๋ฅผ ํŒ๋‹จํ•ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

References


Written by@ugaemi
Record things I want to remember

๐Ÿฑ GitHub๐Ÿ“š Reading Space