iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 6
1
自我挑戰組

見習村-30 Day CodeWars Challenge系列 第 6

見習村06 - Character with longest consecutive

06 - Character with longest consecutive repetition

Don't say so much, just coding...

Instruction

For a given string s find the character c (or C) with longest consecutive repetition and return:

  [c, l]

where l (or L) is the length of the repetition. If there are two or more characters with the same l return the first in order of appearance.

For empty string return:

  ["", 0]

Ruby

Init

  def longest_repetition chars
    # "Implement me!"
  end

Sample Testing

  describe "Solution" do
    it "should work for example tests" do
      Test.assert_equals(longest_repetition("aaaabb"), ['a', 4])
      Test.assert_equals(longest_repetition("bbbaaabaaaa"), ['a', 4])
      Test.assert_equals(longest_repetition("cbdeuuu900"), ['u', 3])
      Test.assert_equals(longest_repetition("abbbbb"), ['b', 5])
      Test.assert_equals(longest_repetition("aabb"), ['a', 2])
      Test.assert_equals(longest_repetition("ba"), ['b', 1])
      Test.assert_equals(longest_repetition(""), ['', 0])
      Test.assert_equals(longest_repetition("aaabbcccddd"), ['a', 3])
    end
  end

Javascript

Init

  function longestRepetition(s) {
    // return ["", 0];
  }

Sample Testing

  Test.describe("Longest repetition", ()=>{
    Test.it("Example tests", function(){
      Test.assertDeepEquals( longestRepetition("aaaabb"),      ["a",4] );
      Test.assertDeepEquals( longestRepetition("bbbaaabaaaa"), ["a",4] );
      Test.assertDeepEquals( longestRepetition("cbdeuuu900"),  ["u",3] );
      Test.assertDeepEquals( longestRepetition("abbbbb"),      ["b",5] );
      Test.assertDeepEquals( longestRepetition("aabb"),        ["a",2] );
      Test.assertDeepEquals( longestRepetition(""),            ["",0] );
      Test.assertDeepEquals( longestRepetition("ba"),          ["b",1] );
    });
  });

Thinking

想法(1): 與 見習村02 - Unique In Order 有點類似,都可以先將輸入的值分群
想法(2): 分群後的陣列,可以再來排序由大到小
想法(3): 排序完成後再來判斷如果傳進來的值是空的要另外回傳 ["", 0],不是則回傳最大的值

https://ithelp.ithome.com.tw/upload/images/20200921/20120826gqBHgls5v7.jpg
圖片來源:Unsplash Dayne Topkin

Hint & Reference

Solution

Ruby

  # Solution 1
  def longest_repetition chars
    char = chars.gsub(/(.)\1*/).max_by(&:size)
    if char.nil? 
      ['', 0] 
    else 
      [char.split('').first, char.split('').count]
    end
  end
  
  # Solution 2
  def longest_repetition chars
    char = chars.gsub(/(.)\1*/).max_by(&:size)
    char.nil? ? ['', 0] : [char.split('').first, char.split('').count]
  end
  
  # Solution 3
  def longest_repetition chars
    return ['', 0] if chars.empty?
    
    char = chars.gsub(/(.)\1*/).max_by(&:size).split('')
    [char.first, char.count]
  end
  
  # Solution 4
  def longest_repetition chars
    return ["", 0] if chars.empty?
    
    chars.chars.chunk(&:itself)
               .map{ |char, chars| [char, chars.size] }
               .max_by { |char, size| size }     
  end

Javascript

  // Solution 1
  function longestRepetition(s) {
    let string = s.split("");
    let result = ["", 0];
  
    for(let i = 0; i < string.length; i++){
      let repetitions = 1;
      while(string[i] == string[i+repetitions]){
        repetitions++;
      }
      if(repetitions > result[1]){
        result = [string[i],repetitions]
      }
    }
    return result;
  }

上一篇
見習村05 - The Hashtag Generator
下一篇
見習村07 - Sum of Digits / Digital Root
系列文
見習村-30 Day CodeWars Challenge30

尚未有邦友留言

立即登入留言