-
Notifications
You must be signed in to change notification settings - Fork 0
[Defibrillators task] Fix type error - defibrillator instead of defibrilator #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Slavunderkind
wants to merge
2
commits into
master
Choose a base branch
from
defibrillator_solution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| # Solution for https://www.codingame.com/ide/puzzle/defibrillators | ||
| class Defibrillators | ||
| attr_accessor :lon, :lat, :n, :defibrilators | ||
| attr_accessor :lon, :lat, :defibrilators_count, :defibrillators | ||
|
|
||
| def initialize | ||
| @lon = parse(gets.chomp) | ||
| @lat = parse(gets.chomp) | ||
| @n = gets.to_i | ||
| @defibrilators = {} | ||
| @lon = degrees_to_radians(gets.chomp) | ||
| @lat = degrees_to_radians(gets.chomp) | ||
| @defibrilators_count = gets.to_i | ||
| @defibrillators = {} | ||
| end | ||
|
|
||
| def calculate_x(d_long, d_lat) | ||
|
|
@@ -22,39 +22,27 @@ def distance(x, y) | |
| Math.sqrt(x * x + y * y) * 6371 | ||
| end | ||
|
|
||
| def parse(var) | ||
| (var.sub(',', '.').to_f * Math::PI) / 180 | ||
| def degrees_to_radians(degrees) | ||
| (degrees.sub(',', '.').to_f * Math::PI) / 180 | ||
| end | ||
|
|
||
| def find_closest(defibrilators_hash) | ||
| min = defibrilators_hash.values.first | ||
| closest_name = defibrilators_hash.keys.first | ||
|
|
||
| defibrilators_hash.map do |key, value| | ||
| if value < min | ||
| min = value | ||
| closest_name = key | ||
| end | ||
| end | ||
| closest_name | ||
| end | ||
|
|
||
| def init_defibrilators(defibrilator) | ||
| d_long = parse(defibrilator[4]) | ||
| d_lat = parse(defibrilator[5]) | ||
| def add_defibrillator(defibrillator) | ||
| d_long = degrees_to_radians(defibrillator[4]) | ||
| d_lat = degrees_to_radians(defibrillator[5]) | ||
| x = calculate_x(d_long, d_lat) | ||
| y = calculate_y(d_lat) | ||
| defibrilators[defibrilator[1]] = distance(x, y) | ||
| defibrillators[defibrillator[1]] = distance(x, y) | ||
| end | ||
|
|
||
| def start | ||
| n.times do | ||
| Array.new(defibrilators_count) do | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @evgenispasov-which special for you There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| defib = gets.chomp | ||
| init_defibrilators(defib.split(';')) | ||
| add_defibrillator(defib.split(';')) | ||
| end | ||
| puts find_closest(defibrillators) | ||
| defibrillators.min_by { |_, v| v }.first | ||
| end | ||
| end | ||
|
|
||
| obj = Defibrillators.new | ||
| obj.start | ||
| answer = obj.start | ||
| puts answer | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inside the
distancefunction, you can use Ruby's "power" operator - so instead ofyou can do