반응형
정수배열을 string으로 바꿔서 count() 적용하기. string으로 바꿔야 각 자리 숫자를 비교할 수 있다.
arr = [7, 77, 17]
1. 이중반복문 : for in
def solution(arr):
chr = []
for i in arr:
chr.append(str(i))
result = 0
for i in chr:
for j in i:
if j == '7':
result += 1
return result
2. str/list.count()
def solution2(arr):
chr = ''
for i in arr:
chr += str(i)
return chr.count('7')
3. list를 string으로 변경하는 방법
3-1. ''.join(list) : map()을 통해 arr에 있는 정수를 모두 str로 변
def solution3(arr):
return ''.join(map(str, arr)).count('7')
3-2. str(list)
def solution4(arr):
return str(arr).count('7')
3-3. 반복문
def solution5(arr):
return sum([str(i).count('7') for i in arr])
반응형
'프로그래밍 > 알고리즘 & 자료구조' 카테고리의 다른 글
[프로그래머스] 이진수 더하기 with 파이썬 (0) | 2023.04.06 |
---|---|
[프로그래머스] 컨트롤 제트 with 파이썬 (0) | 2023.04.05 |
[프로그래머스] 가까운 수 with 파이썬 (0) | 2023.04.03 |
[프로그래머스] 중복된 숫자 갯수 with 파이썬 (0) | 2023.04.02 |
[프로그래머스] 2차원으로 만들기 with 파이썬 (0) | 2023.04.01 |
댓글