Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c0b03f6

Browse files
Add files via upload
1 parent cdb7d55 commit c0b03f6

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

‎Kangaroo.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# You are choreographing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity).
2+
3+
# The first kangaroo starts at location and moves at a rate of meters per jump.
4+
# The second kangaroo starts at location and moves at a rate of meters per jump.
5+
# You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return YES, otherwise return NO.
6+
7+
# For example, kangaroo starts at with a jump distance and kangaroo starts at with a jump distance of . After one jump, they are both at , (, ), so our answer is YES.
8+
9+
# Function Description
10+
11+
# Complete the function kangaroo in the editor below. It should return YES if they reach the same position at the same time, or NO if they don't.
12+
13+
# kangaroo has the following parameter(s):
14+
15+
# x1, v1: integers, starting position and jump distance for kangaroo 1
16+
# x2, v2: integers, starting position and jump distance for kangaroo 2
17+
# Input Format
18+
19+
# A single line of four space-separated integers denoting the respective values of , , , and .
20+
21+
# Constraints
22+
23+
# Output Format
24+
25+
# Print YES if they can land on the same location at the same time; otherwise, print NO.
26+
27+
# Note: The two kangaroos must land at the same location after making the same number of jumps.
28+
29+
# Sample Input 0
30+
31+
# 0 3 4 2
32+
# Sample Output 0
33+
34+
# YES
35+
# Explanation 0
36+
37+
# The two kangaroos jump through the following sequence of locations:
38+
39+
# image
40+
41+
# From the image, it is clear that the kangaroos meet at the same location (number on the number line) after same number of jumps ( jumps), and we print YES.
42+
43+
# Sample Input 1
44+
45+
# 0 2 5 3
46+
# Sample Output 1
47+
48+
# NO
49+
# Explanation 1
50+
51+
# The second kangaroo has a starting location that is ahead (further to the right) of the first kangaroo's starting location (i.e., ). Because the second kangaroo moves at a faster rate (meaning ) and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print NO.
52+
53+
54+
55+
56+
57+
58+
59+
60+
#!/bin/python3
61+
62+
import math
63+
import os
64+
import random
65+
import re
66+
import sys
67+
68+
# Complete the kangaroo function below.
69+
70+
def kangaroo(x1, v1, x2, v2):
71+
i = 10000
72+
counter = 0
73+
while(i > 0):
74+
if(x1 == x2):
75+
return "YES"
76+
counter = 1
77+
break
78+
else:
79+
x1 += v1
80+
x2 += v2
81+
i -= 1
82+
if(counter == 0):
83+
return "NO"
84+
85+
86+
if __name__ == '__main__':
87+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
88+
89+
x1V1X2V2 = input().split()
90+
91+
x1 = int(x1V1X2V2[0])
92+
93+
v1 = int(x1V1X2V2[1])
94+
95+
x2 = int(x1V1X2V2[2])
96+
97+
v2 = int(x1V1X2V2[3])
98+
99+
result = kangaroo(x1, v1, x2, v2)
100+
101+
fptr.write(result + '\n')
102+
103+
fptr.close()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /