Skip to content

Commit 15da13b

Browse files
Temperatures
1 parent 7e79ada commit 15da13b

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The "Solutions to CodinGame Puzzles" project is a collection of answers to codin
1616
| Onboarding 🛹 | [Python](./puzzles/python3/onboarding), [JavaScript](./puzzles/js/onboarding), [C++](./puzzles/cpp/onboarding) | Variables, Input/Output, Conditions |
1717
| The Descent 🌄 | [Python](./puzzles/python3/the-descent) ★, [Kotlin](./puzzles/kotlin/src/the-descent), [TypeScript](./puzzles/ts/the-descent), [C++](./puzzles/cpp/the-descent) | Conditions, Loops |
1818
| Power of Thor 1 ⚡ | [Python](./puzzles/python3/power-of-thor1) ★, [Kotlin](./puzzles/kotlin/src/power-of-thor1), [TypeScript](./puzzles/ts/power-of-thor1), [Bash](./puzzles/bash/power-of-thor1), [Swift](./puzzles/swift/power-of-thor1) | Input/Output, Conditions |
19-
| Temperatures ❄️ | [Python](./puzzles/python3/temperatures) ★, [Kotlin](./puzzles/kotlin/src/temperatures), [TypeScript](./puzzles/ts/temperatures), [Ruby](./puzzles/ruby/temperatures) | Conditions, Loops, Arrays |
19+
| Temperatures ❄️ | [Python](./puzzles/python3/temperatures), [Kotlin](./puzzles/kotlin/src/temperatures), [TypeScript](./puzzles/ts/temperatures), [Ruby](./puzzles/ruby/temperatures) ★ | Conditions, Loops, Arrays |
2020
| Mars Lander 1 🚀 | [Python](./puzzles/python3/mars-lander1), [Kotlin](./puzzles/kotlin/src/mars-lander1), [TypeScript](./puzzles/ts/mars-lander1) ★, [C++](./puzzles/cpp/mars-lander1.cpp) | Conditions, Loops |
2121
| ASCII Art 🎨 | [Python](./puzzles/python3/ascii-art), [Kotlin](./puzzles/kotlin/src/ascii-art), [TypeScript](./puzzles/ts/ascii-art), [Ruby](./puzzles/ruby/ascii-art) ★ | Strings |
2222
| Unary 1️⃣ | [Python](./puzzles/python3/unary), [TypeScript](./puzzles/ts/unary) ★, [Haskell](./puzzles/haskell/unary), [C#](./puzzles/cs/unary) | Strings, Encoding |

puzzles/python3/temperatures/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,18 @@ print(closest_temp if n > 0 else 0)
4141
## Edge Cases
4242

4343
These include scenarios such as an empty input list, all negative temperatures, all positive temperatures, temperatures equidistant from zero with opposite signs, a single temperature in the list, and a large range of temperatures. Testing with an empty list ensures the program outputs 0 as expected, while all negative and positive temperatures verify whether the program identifies the closest positive temperature to zero correctly. Similarly, testing temperatures equidistant from zero with opposite signs checks the program's output in such cases. Additionally, testing with a single temperature ensures the program handles single-item lists accurately, and providing a large range of temperatures evaluates the program's efficiency and performance.
44+
45+
## Example Input/Output
46+
47+
**Input**
48+
49+
```
50+
5
51+
1 -2 -8 4 5
52+
```
53+
54+
**Output**
55+
56+
```
57+
1
58+
```
Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,67 @@
11
# Temperatures
22

3+
## Description
4+
35
"Temperatures" is a beginner-level coding challenge available on the CodinGame platform. In this challenge, the player is given a list of integers representing temperature readings in Celsius degrees for a city during a certain period of time.
46

57
The objective is to write a program that finds and outputs the closest temperature to zero. If there are two temperatures that are equally close to zero, the program should output the positive one.
68

79
The challenge consists of writing a program that takes as input the list of temperature readings and outputs the temperature closest to zero. If the list is empty, the program should output 0.
810

9-
The challenge is designed to help players learn and practice programming skills such as array manipulation, looping, and conditional statements. It is a fun and engaging way to improve programming skills while solving a challenging and entertaining puzzle.
11+
## Solution Overview
12+
13+
The algorithm processes a list of integer temperatures to find the one closest to zero. Here's a step-by-step breakdown:
14+
15+
1. **Read Input**: It first reads the number of temperatures and the temperatures themselves from standard input. The temperatures are converted into an array of integers.
16+
17+
2. **Initialize Variables**
18+
19+
3. **Iterate Over Temperatures**
20+
21+
4. **Output the Result**
22+
23+
## Example Input/Output
24+
25+
**Input**
26+
27+
```
28+
5
29+
1 -2 -8 4 5
30+
```
31+
32+
**Output**
33+
34+
```
35+
1
36+
```
37+
38+
## Code Example
39+
40+
```ruby
41+
# Initialize variables
42+
closest_temp = nil
43+
closest_diff = nil
44+
45+
# Read the number of temperatures to analyze
46+
n = gets.to_i
47+
48+
# Read the temperatures as a string and split it into a list of integers
49+
temperatures = gets.split(" ").map(&:to_i)
50+
51+
# Find the temperature closest to zero
52+
temperatures.each do |temperature|
53+
# Calculate the absolute difference between the temperature and zero
54+
diff = (temperature - 0).abs
55+
# Check if the current temperature is closer to zero, handling the case where the closest difference is unknown
56+
if closest_temp.nil? || diff < closest_diff
57+
closest_temp = temperature
58+
closest_diff = diff
59+
# choose the positive one by comparing the temperature with zero
60+
elsif diff == closest_diff && temperature > 0
61+
closest_temp = temperature
62+
end
63+
end
64+
65+
# Print the closest temperature
66+
puts closest_temp || 0
67+
```

0 commit comments

Comments
 (0)