[ Code Kata ] ๐Ÿคฏ python #14 ๋ฐฐ์—ด ๋‚ด 0์„ ๋ชจ๋‘ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋™์‹œํ‚ค๊ธฐ

Haileeยท2020๋…„ 12์›” 17์ผ
0

[ Code Kata ]

๋ชฉ๋ก ๋ณด๊ธฐ
20/28
post-thumbnail

๋ฌธ์ œ

์ฃผ์–ด์ง„ ์ˆซ์ž ๋ฐฐ์—ด์—์„œ, 0์„ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰์ชฝ์œผ๋กœ ์ด๋™์‹œ์ผœ์ฃผ์„ธ์š”.

  • ์›๋ž˜ ์žˆ๋˜ ์ˆซ์ž์˜ ์ˆœ์„œ๋Š” ๋ฐ”๊พธ์ง€ ๋ง์•„์ฃผ์„ธ์š”.
  • ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค.
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]

์ฒ˜์Œ์—” ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ 0๋งŒ ๋งจ ๋์œผ๋กœ ์ด๋™์‹œ์ผœ์ฃผ๋ฉด ๋˜๋Š” ์ค„ ์•Œ๊ณ , pop()๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋  ์ค„ ์•Œ์•˜๋‹ค.
๋‹ค์‹œ ์ฝ์–ด๋ณด๋‹ˆ ๋ฐฐ์—ด ๋‚ด ๋ชจ๋“  0์„ ๋์œผ๋กœ ์ด๋™์‹œํ‚ค๋ผ๋Š” ๊ฒƒ์ด์—ˆ...


๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ

def move_zeroes(nums):
  index = 0
  for i in nums:
    if 0 == i:
      a = nums.pop(index)
      nums.append(a)
    index += 1

  return nums

์šฐ์„  for loop์„ ํ†ตํ•ด ๋งŒ์•ฝ i๋ฒˆ์งธ ์š”์†Œ๊ฐ€ 0์ด๋ผ๋ฉด pop()ํ•œ ๋’ค ๋ฐฐ์—ด์˜ ๋งจ ๋งˆ์ง€๋ง‰์— ํ•ด๋‹น ์š”์†Œ๋ฅผ append()ํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค.

์ฒซ๋ฒˆ์งธ ๊ฒ€์ฆ์€ ๋ฌด์‚ฌํžˆ ๋„˜์–ด๊ฐ”๋Š”๋ฐ, ์™œ์ธ์ง€ [0,0,9,0,12]๋Š” ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋งŒ ์ด๋™๋˜๊ณ , ๊ทธ ์ดํ›„๋กœ๋Š” ๋ณ€ํ™”๊ฐ€ ์—†์—ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด [0,9,12,0,0] ์ด๋Ÿฐ์‹์œผ๋กœ!

์‚ฌ์‹ค ๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ๋Œ€๋กœ ์ง„ํ–‰ํ•˜๋ฉด, for loop์ด ์ง„ํ–‰๋˜๋Š” ์™€์ค‘์— ๋งจ ๋’ค์— ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ.
๊ทธ๋Ÿผ ๋ฐ˜๋ณต๋ฌธ ๋„์ค‘ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์ด ๋ฐ”๋€Œ์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹Œ๊ฐ€ ์‹ถ์–ด์„œ ๋ฌธ์ œ๊ฐ€ ๋  ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•˜๊ธด ํ–ˆ๋Š”๋ฐ, ๊ณผ์—ฐ ์ •๋ง ์ด๊ฒŒ ์›์ธ์ด ๋งž๋Š”์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.

๐Ÿ‘‰๐Ÿป ๋Š” ์‚ฌ์‹ค ์˜ํ–ฅ์ด ์žˆ์—ˆ๋‹ค.
์ฒซ๋ฒˆ์งธ ๋ฐฐ์—ด [0,1,0,3,12]๋Š” ์™„์ „ํžˆ ์šด ์ข‹๊ฒŒ ์–ป์–ด๊ฑธ๋ ธ๋˜ ๊ฒƒ!
๋‚ด๊ฐ€ for loop์„ ์ง„ํ–‰ํ•จ๊ณผ ๋™์‹œ์— ๋ฐฐ์—ด ๋‚ด๋ถ€๊ฐ€ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์—, index๋ผ๋Š” ์ˆซ์ž์˜ ๊ฐ’์ด ๋Š˜์–ด๋‚ ์ˆ˜๋ก, ๋ฐ”๋€ ๋ฐฐ์—ด ๋‚ด์—์„œ ํ•ด๋‹น ์ˆœ๋ฒˆ์˜ ์š”์†Œ๋ฅผ ์ฐพ๊ธฐ ๋•Œ๋ฌธ์—, ์• ์ดˆ์— ์„ฑ๋ฆฝ๋  ์ˆ˜ ์—†๋Š” ๋กœ์ง์ด์—ˆ๋‹ค.

๊ฒฐ๋ก ์€ ์˜ค๋‹ต..!

์•„์˜ˆ ์•ˆ์ „ํ•˜๊ฒŒ ๋ฐฐ์—ด์˜ ๋’ท๋ถ€๋ถ„๋ถ€ํ„ฐ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๋ฉด์„œ ๋ฐ”๊พธ๋˜์ง€, ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋‚ด์•ผ ํ–ˆ๋‹ค.
(์•ž์—์„œ๋ถ€ํ„ฐ ๋ฐ”๊ฟ”๋‚˜๊ฐ€๋ฉด ์™„์ „ํžˆ ์ˆœ์„œ๊ฐ€ ํ‹€์–ด์ง€๊ธฐ ๋•Œ๋ฌธ..!!)


Model Solution

def moveZeroes(nums):
  last0 = 0
  
  for i in range(0, len(nums)):
    if nums[i] != 0:
      nums[i], nums[last0] = nums[last0], nums[i]
      last0 += 1
      
  return nums
      
moveZeroes([0,1,0,3,12]);

๋‹ค๋ฅธ๋ถ„์ด ํ‘ผ ์ฝ”๋“œ!

1. enumerate( ) ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

def move_zeroes(nums):
  x = list([(i, x) for i,x in enumerate(nums) if x==0])
  
  for i in reversed(x):
    del nums[i[0]]
  for i in range(len(x)):
    nums.append(0)
  return nums

2. 0์ด ์•„๋‹Œ ์ˆซ์ž ๊ฒ€์‚ฌํ•˜๊ธฐ

def move_zeroes(nums):
  last_0 = 0 #๋ฐฐ์—ด์„ ์ˆœ์„œ๋ฅด 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ๋•Œ๋ฌธ์ด๋‹ค
  for i in range(len(nums)):
    if nums[i] != 0: #๋ฐฐ์—ด์•ˆ์— ์ˆซ์ž๊ฐ€ 0์ด ์•„๋‹ˆ๋ฉด
      temp = nums[i]  #0์ด ์•„๋‹Œ์ˆซ์ž temp์— ์ €์žฅ
      nums[i] = nums[last_0]
      nums[last_0] = temp #nums[last_0]์— temp 0์ด ์•„๋‹Œ์ˆซ์ž์ €์žฅ
      last_0 = last_0 + 1 #๊ทธ ๋‹ค์Œ๋ฐฐ์—ด์„ ์œ„ํ•ด 1์„ ๋”ํ•˜๊ธฐ
  return nums
profile
์›น ๊ฐœ๋ฐœ ๐Ÿท๐Ÿ˜Ž๐Ÿ‘Š๐Ÿป๐Ÿ”ฅ

0๊ฐœ์˜ ๋Œ“๊ธ€