Skip to content

Commit

Permalink
Merge pull request #435 from fartem/1859_Sorting_the_Sentence
Browse files Browse the repository at this point in the history
2023-12-22 v. 3.8.0: added "1859. Sorting the Sentence"
  • Loading branch information
fartem authored Dec 22, 2023
2 parents e89faa8 + d9dbe55 commit 37f0a50
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 1837. Sum of Digits in Base K | [Link](https://leetcode.com/problems/sum-of-digits-in-base-k/) | [Link](./lib/easy/1837_sum_of_digits_in_base_k.rb) |
| 1844. Replace All Digits with Characters | [Link](https://leetcode.com/problems/replace-all-digits-with-characters/) | [Link](./lib/easy/1844_replace_all_digits_with_characters.rb) |
| 1848. Minimum Distance to the Target Element | [Link](https://leetcode.com/problems/minimum-distance-to-the-target-element/) | [Link](./lib/easy/1848_minimum_distance_to_the_target_element.rb) |
| 1859. Sorting the Sentence | [Link](https://leetcode.com/problems/sorting-the-sentence/) | [Link](./lib/easy/1859_sorting_the_sentence.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '3.7.9'
s.version = '3.8.0'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
12 changes: 12 additions & 0 deletions lib/easy/1859_sorting_the_sentence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

# https://leetcode.com/problems/sorting-the-sentence/
# @param {String} s
# @return {String}
def sort_sentence(s)
s
.split
.sort { |a, b| a[-1] <=> b[-1] }
.map { |a| a[0..-2] }
.join(' ')
end
12 changes: 12 additions & 0 deletions test/easy/test_1859_sorting_the_sentence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/easy/1859_sorting_the_sentence'
require 'minitest/autorun'

class SortingTheSentenceTest < ::Minitest::Test
def test_default
assert_equal('This is a sentence', sort_sentence('is2 sentence4 This1 a3'))
assert_equal('Me Myself and I', sort_sentence('Myself2 Me1 I4 and3'))
end
end

0 comments on commit 37f0a50

Please sign in to comment.