-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #435 from fartem/1859_Sorting_the_Sentence
2023-12-22 v. 3.8.0: added "1859. Sorting the Sentence"
- Loading branch information
Showing
4 changed files
with
26 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -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 |
This file contains 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 |
---|---|---|
@@ -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 |