[python ๊ธฐ์ดˆ] Code-kata week2-2

EMMAยท2022๋…„ 3์›” 22์ผ
0

[wecode] Code-kata

๋ชฉ๋ก ๋ณด๊ธฐ
7/12

๐Ÿ–ฅ Code-kata week2-2


๋ฌธ์ œ
์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์ธ nums๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

์ˆซ์ž์ค‘์—์„œ ๊ณผ๋ฐ˜์ˆ˜(majority, more than a half)๊ฐ€ ๋„˜์€ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.

์˜ˆ๋ฅผ ๋“ค์–ด,
nums = [3,2,3]
return 3

nums = [2,2,1,1,1,2,2]
return 2


ํ’€์ด

  • ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์˜ ๊ธฐ์ค€์€ ํ•ด๋‹น๊ฐ’์˜ ๊ฐœ์ˆ˜ > ๊ณผ๋ฐ˜์ˆ˜(์ธ์ž์˜ ์ „์ฒด๊ธธ์ด/2)
  • ๊ธธ์ด๊ฐ€ 0์ผ ๊ฒฝ์šฐ ''์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค
def more_than_half(nums):
    new_list = []
    for i in nums: 
        cnt = nums.count(i)
        if cnt > len(nums)/2:
            new_list.append(i)
    if len(new_list) == 0:
        return ''
    else:
        return new_list[0]
profile
์˜ˆ๋น„ ๊ฐœ๋ฐœ์ž์˜ ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ | explore, explore and explore

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