알고리즘/코드워

[python]Bouncing Balls

(ㅇㅅㅎ) 2020. 3. 14. 22:42
728x90
반응형

https://www.codewars.com/kata/5544c7a5cb454edb3c000047/train/python

 

Codewars: Train your coding skills

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

 

한 아이가 h 높이에서 공을 창문으로 던질 때 공은 h 곱하기 bounce만큼 튀어 오른다. 아이의 엄마가 window에서 볼 때 엄마는 공이 지나가는 것을 몇 번이나 보는지가 문제이다.

 

더보기

 

변수 h, bounce와 window가 각 조건을 만족하지 않으면 -1을 반환해야 한다는 규칙이 있다. 

이 규칙을 잘 지키면서 h가 window보다 클 경우 내려갈 때와 올라갈 때를 구분지어서 코드를 작성하였다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# My Code
def bouncingBall(h, bounce, window):
    cnt = 0
    if 0 < h and 0 < bounce < 1 and window < h:
        while h > window:
            # 내려갈 때
            cnt += 1
            h *= bounce
            if h > window:
                # 올라갈 때
                cnt += 1
        return cnt
    else:
        return -1
 
if __name__=='__main__':
    answer = bouncingBall(30.661.5)
    print(answer)
    answer = bouncingBall(300.661.5)
    print(answer)
반응형

'알고리즘 > 코드워' 카테고리의 다른 글

[python]Removing Elements  (0) 2020.03.20
[python]CamelCase Method  (0) 2020.03.15
[python]Simple Pig Latin  (0) 2020.03.13
[python]Highest Scoring Word  (0) 2020.03.12
[python]Build Tower  (0) 2020.03.11