반응형
Quiz>
You have a string that consist only of digits.
You need to find how many zero digits ("0") are at the beginning of the given string.
Input:
A string, that consist of digits.
Output:
An Int.
Example:
beginning_zeros('100') == 0
beginning_zeros('001') == 2
beginning_zeros('100100') == 0
beginning_zeros('001001') == 2
beginning_zeros('012345679') == 1
beginning_zeros('0000') == 4
Precondition:
0-9
def beginning_zeros(number: str) -> int:
# your code here
return 0
if __name__ == '__main__':
print("Example:")
print(beginning_zeros('100'))
# These "asserts" are used for self-checking and not for an auto-testing
assert beginning_zeros('100') == 0
assert beginning_zeros('001') == 2
assert beginning_zeros('100100') == 0
assert beginning_zeros('001001') == 2
assert beginning_zeros('012345679') == 1
assert beginning_zeros('0000') == 4
print("Coding complete? Click 'Check' to earn cool rewards!")
Solve>
1. 초기값 지정
def beginning_zeros(number: str):
initial_value = 0
2. 숫자를 하나씩 가져와서 n이 0이랑 같으면
def beginning_zeros(number: str):
for n in number:
if int(n) == 0:
3. 초기값 1을 더한다.
def beginning_zeros(number: str):
for n in number:
if int(n) == 0:
initial_value += 1
4. 다른 숫자이면 초기값을 반환한다.
def beginning_zeros(number: str):
else:
return initial_value
5. for문이 종료되면 초기값에 더해진 값을 반환한다.
def beginning_zeros(number: str):
return initial_value
Code>
def beginning_zeros(number: str):
initial_value = 0
for n in number:
if int(n) == 0:
initial_value += 1
else:
return initial_value
return initial_value
Example>
if __name__ == '__main__':
print("Example:")
print(beginning_zeros('100'))
# These "asserts" are used for self-checking and not for an auto-testing
assert beginning_zeros('100') == 0
assert beginning_zeros('001') == 2
assert beginning_zeros('100100') == 0
assert beginning_zeros('001001') == 2
assert beginning_zeros('012345679') == 1
assert beginning_zeros('0000') == 4
print("Coding complete? Click 'Check' to earn cool rewards!")
Result>
Example:
0
Coding complete? Click 'Check' to earn cool rewards!
반응형
'Python_Matter > [Check_IO]Elementary' 카테고리의 다른 글
Between Markers (0) | 2020.04.11 |
---|---|
Nearest Value (0) | 2020.04.11 |
Split Pairs (0) | 2020.04.11 |
Max Digit (0) | 2020.04.11 |
Replace First (0) | 2020.04.11 |