iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 5
1

05 - The Hashtag Generator

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

Instruction

The marketing team is spending way too much time typing in hashtags.
Let's help them with our own Hashtag Generator!

Here's the deal:

It must start with a hashtag (#).
All words must have their first letter capitalized.
If the final result is longer than 140 chars it must return false.
If the input or the result is an empty string it must return false.

Example

  " Hello there thanks for trying my Kata"  =>  "#HelloThereThanksForTryingMyKata"
  "    Hello     World   "                  =>  "#HelloWorld"
  ""                                        =>  false

Ruby

Init

  def generateHashtag(str)
    # ...
  end

Sample Testing

  Test.assert_equals(generateHashtag(""), false, "Expected an empty string to return false")
  Test.assert_equals(generateHashtag(" " * 200), false, "Still an empty string")
  Test.assert_equals(generateHashtag("Do We have A Hashtag"), "#DoWeHaveAHashtag", "Expected a Hashtag (#) at the beginning.")
  Test.assert_equals(generateHashtag("Codewars"), "#Codewars", "Should handle a single word.")
  Test.assert_equals(generateHashtag("Codewars Is Nice"), "#CodewarsIsNice", "Should remove spaces.")
  Test.assert_equals(generateHashtag("Codewars is nice"), "#CodewarsIsNice", "Should capitalize first letters of words.")
  Test.assert_equals(generateHashtag("code" + " " * 140 + "wars"), "#CodeWars")
  Test.assert_equals(generateHashtag("Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong Cat"), false, "Should return false if the final word is longer than 140 chars.")
  Test.assert_equals(generateHashtag("a" * 139), "#A" + "a" * 138, "Should work")
  Test.assert_equals(generateHashtag("a" * 140), false, "Too long")

Javascript

Init

  function generateHashtag (str) {
    // Code here
  };

Sample Testing

  Test.assertEquals(generateHashtag(""), false, "Expected an empty string to return false")
  Test.assertEquals(generateHashtag(" ".repeat(200)), false, "Still an empty string")
  Test.assertEquals(generateHashtag("Do We have A Hashtag"), "#DoWeHaveAHashtag", "Expected a Hashtag (#) at the beginning.")
  Test.assertEquals(generateHashtag("Codewars"), "#Codewars", "Should handle a single word.")
  Test.assertEquals(generateHashtag("Codewars Is Nice"), "#CodewarsIsNice", "Should remove spaces.")
  Test.assertEquals(generateHashtag("Codewars is nice"), "#CodewarsIsNice", "Should capitalize first letters of words.")
  Test.assertEquals(generateHashtag("code" + " ".repeat(140) + "wars"), "#CodeWars")
  Test.assertEquals(generateHashtag("Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong Cat"), false, "Should return false if the final word is longer than 140 chars.")
  Test.assertEquals(generateHashtag("a".repeat(139)), "#A" + "a".repeat(138), "Should work")
  Test.assertEquals(generateHashtag("a".repeat(140)), false, "Too long")

Thinking

想法(1): 須先思考如何處理傳進來的值為空白的狀況,會影響應該判斷長度為 140 || 139
想法(2): 將值切開分群然後轉為大寫再組回來,並且再加上 #hashtag

https://ithelp.ithome.com.tw/upload/images/20200920/20120826uApzNhLLnM.jpg
圖片來源:Unsplash NordWood Themes

Hint & Reference

Solution

Ruby

  # Solution 1
  def generateHashtag(str)
    string = str.split.map(&:capitalize).join
    if (string.empty? || string.length > 139)
      false
    else
      "##{string}"
    end
  end
  
  # Solution 2
  def generateHashtag(str)
    return false if str.strip.empty?
    str = str.split(" ").map(&:capitalize).join.prepend("#")
    str.length > 140 ? false : str
  end

Javascript

  // Solution 1
  function generateHashtag (str) {
    let wordArray = str.split(' ').filter(char => char !== "");
    let result = "#";
    
    result = result + wordArray.map(word => {
      let capitalizedWord = word.charAt(0).toUpperCase() + word.slice(1);
      return capitalizedWord;
    }).join('');
    
    return wordArray.length === 0 || result.length > 140 ? false : result
  };

上一篇
見習村04 - Bit Counting
下一篇
見習村06 - Character with longest consecutive
系列文
見習村-30 Day CodeWars Challenge30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言