인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

souk21098141's profile image
souk21098141

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

2. Extract only numbers

숫자만 추출 질문

Written on

·

222

0

안녕하세요 강의 덕분에 많이 도움이 되고 있습니다.

저는 아래와 같이 코드를 짰습니다.

숫자만 걸러내기 위해 입력 문자 하나하나 ord를 적용해서

숫자인지 확인하였습니다. 그리고 숫자만 따로 문자로 만들어서 int()로 형변환을 하니 0이 사라졌습니다.

이렇게 문제를 해결해도 되는 것인가요??

import sys

import math

#sys.stdin=open("input.txt","rt")

arr = input()

def find_gcd(x):

    cnt = 0

    for i in range(1, x+1):

        if x % i == 0 :

            cnt +=1

    return cnt

st = ''

for i in range(len(arr)):

    #print(ord(arr[i]))

    if ord('0') <= ord(arr[i]) <= ord('9'):

        st += arr[i]

res = int(st)

print(res)

print(find_gcd(res))

python코테 준비 같이 해요!

Answer 1

1

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

네. 잘하신 코드입니다.

souk21098141's profile image
souk21098141

asked

Ask a question