Skip to content

Commit 4a9eebf

Browse files
Onboarding
1 parent 255fdb9 commit 4a9eebf

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed
Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
1-
Here's a possible solution to the "Onboarding" challenge on CodinGame using Python:
1+
# Onboarding Puzzle
2+
3+
## Description
4+
5+
In this problem, you need to choose which enemy to shoot based on their distance from your ship. You can compare the distances of the two enemies and then shoot at the closest one.
6+
7+
## Algorithm
8+
9+
The solution uses a `while` loop to continuously read input from the standard input until the program is terminated. In each iteration of the loop, we read in the name and distance of two enemies using the `input()` function, and then compare their distances using an `if` statement. If the distance of the first enemy is less than the distance of the second enemy, we print the name of the first enemy. Otherwise, we print the name of the second enemy.
10+
11+
## Example Input/Output
12+
13+
**Input**
14+
15+
```
16+
Nobody
17+
Rock
18+
9999
19+
70
20+
```
21+
22+
**Output**
23+
24+
```
25+
Rock
26+
```
27+
28+
## Code Example
29+
30+
The following code example provides a solution to the Onboarding puzzle. It reads input from the standard input, compares the distances of two enemies, and prints the name of the closest enemy to the standard output.
231

332
```python
33+
import sys
34+
import math
35+
36+
# game loop
437
while True:
538
enemy_1 = input() # name of enemy 1
639
dist_1 = int(input()) # distance to enemy 1
740
enemy_2 = input() # name of enemy 2
841
dist_2 = int(input()) # distance to enemy 2
942

10-
# Determine which enemy is closer and print its name
43+
# Compare the distances of the two enemies
1144
if dist_1 < dist_2:
45+
# If enemy 1 is closer, shoot enemy 1
1246
print(enemy_1)
1347
else:
48+
# If enemy 2 is closer or at the same distance, shoot enemy 2
1449
print(enemy_2)
15-
```
1650

17-
In this solution, we use a `while` loop to continuously read input from the standard input until the program is terminated. In each iteration of the loop, we read the name and distance of two enemies, and then determine which one is closer based on their distances. Finally, we print the name of the closer enemy using `print()`.
51+
```
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
if __name__ == "__main__":
2-
# game loop
1+
if __name__ == '__main__':
32
while True:
4-
enemy1: str = input() # name of enemy 1
5-
distance1: int = int(input()) # distance to enemy 1
6-
enemy2: str = input() # name of enemy 2
7-
distance2: int = int(input()) # distance to enemy 2
3+
enemy_1 = input() # name of enemy 1
4+
dist_1 = int(input()) # distance to enemy 1
5+
enemy_2 = input() # name of enemy 2
6+
dist_2 = int(input()) # distance to enemy 2
87

9-
# Display enemy1 name when enemy1 is the closest, enemy2 otherwise
10-
if distance1 < distance2:
11-
print(enemy1)
8+
# Compare the distances of the two enemies
9+
if dist_1 < dist_2:
10+
# If enemy 1 is closer, shoot enemy 1
11+
print(enemy_1)
1212
else:
13-
print(enemy2)
13+
# If enemy 2 is closer or at the same distance, shoot enemy 2
14+
print(enemy_2)

0 commit comments

Comments
 (0)