Extract Only Numbers

Tristan·2024년 7월 24일

Given a string of letters and numbers, only numbers are extracted from them and natural numbers are made in that order. It outputs the natural numbers created and the number of divisors of the natural numbers.

If you just extract a number from "t0e0a1c2h0er," it's 0, 0, 1, 2, 0 and if you make a natural number, it's 120. That is, the first digit 0 is ignored when making natural numbers. The output is 120, and then the next line is the number of divisors of 120.

The natural number produced by extraction does not exceed 100,000,000.

Input:

The first line is given a string mixed with numbers. The string cannot be longer than 50.

Output:

Output the natural numbers in the first line, and the number of divisors in the second line.

Input Example:

g0en2Ts8eSoft

Output Example:

28
6

Solution

  • create a for loop that will go through a
  • the .isdigit() function will go through every signle letters in array and check if they are interger
  • if the letter is an interger, we will save the number in to a num variable
  • num has a num * 10 + int(i) sequence, which will save the numebers in order
  • print num
  • now we will find the numbers of divisors
  • create a for loop that will go through 1 to num + 1
  • if the num / i 's leftover is 0 (num % i == 0), cnt will increase by 1
  • print cnt
a = input()

num = 0
cnt = 0

for i in a:
    if i.isdigit():
        num = num * 10 + int(i)
print (num)

for i in range (1, num + 1):
    if num % i == 0:
        cnt += 1
print (cnt)
profile
@Columbia

0개의 댓글