# JS | 教练，我想做习题9

## 📖 题目1：标签生成器

• 它必须以井号（`#`）开头。
• 所有单词的首字母必须大写。
• 如果最终结果超过140个字符，则必须返回`false`
• 如果输入或结果为空字符串，则必须返回`false`

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

``function generateHashtag (str) {``}``

## 📖 题目2：合并的字符串检查器

``'codewars' is a merge from 'cdw' and 'oears':s:  c o d e w a r s   = codewarspart1:  c   d   w         = cdw``part2:    o   e   a r s   = oears``

``function isMerge(s, part1, part2) {return false;``}``

## 📖 题目3：约瑟夫斯幸存者

``josephus_survivor(7,3) => means 7 people in a circle;one every 3 is eliminated until one remains[1,2,3,4,5,6,7] - initial sequence[1,2,4,5,6,7] => 3 is counted out[1,2,4,5,7] => 6 is counted out[1,4,5,7] => 2 is counted out[1,4,5] => 7 is counted out[1,4] => 5 is counted out``[4] => 1 counted out, 4 is the last element - the survivor!``

``function josephusSurvivor(n,k){//your code here``}``

## 答案

### 🍗 题目1的答案

``function generateHashtag (str) {return str.length > 140 || str === '' ? false :'#' + str.split(' ').map(capitalize).join('');}function capitalize(str) {return str.charAt(0).toUpperCase() + str.slice(1);``}``

``function generateHashtag (str) {if(!str || str.length < 1) return false;var r = '#' + str.split(' ').map(function(el) {return el.charAt(0).toUpperCase() + el.slice(1).toLowerCase();}).join('');return r.length > 140?false:r;``}``

``function generateHashtag (str) {var hashtag = str.split(' ').reduce(function(tag, word) {return tag + word.charAt(0).toUpperCase() + word.substring(1);}, '#');return hashtag.length == 1 || hashtag.length > 140 ? false : hashtag;``}``

``function generateHashtag(str) {var hashed = '#' + str.split(' ').map(function(v) {return v.charAt(0).toUpperCase() + v.slice(1);}).join('');return hashed.length > 140 || str == "" ? false : hashed;``}``

``function generateHashtag (str) {var s = "#" + str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()}).split(" ").join("");if(str && s.length <= 140){return s;} return false;``}``

### 🍗 题目2的答案

``function isMerge(s, part1, part2) {return !s ? !(part1 || part2) :s[0] == part1[0] && isMerge(s.slice(1), part1.slice(1), part2) ||s[0] == part2[0] && isMerge(s.slice(1), part1, part2.slice(1));``}``

``function isMerge(s, part1, part2) {if (s === "" && part1 === "" && part2 === "") return true;if (s !== ""){if (s[0] === part1[0] && part1[0] === part2[0]){// we make two branches of an recursion and get OR of the answersreturn (isMerge(s.slice(1), part1.slice(1), part2) || isMerge(s.slice(1), part1, part2.slice(1)));} else if (s[0] === part1[0]){return isMerge(s.slice(1), part1.slice(1), part2);} else if (s[0] === part2[0]){return isMerge(s.slice(1), part1, part2.slice(1));}}return false;}``

``const isMerge = (s, part1, part2) =>(fn => !s ? !(part1 || part2) : fn(part1, part2) || fn(part2, part1))``((p1, p2) => s[0] === p1[0] && isMerge(s.slice(1), p1.slice(1), p2));``

``var isMerge = (s, x, y) => !s  ? !(x || y) : check(s, x, y) || check(s, y, x);``var check   = (a, b, c) => a[0] == b[0] && isMerge(a.slice(1), b.slice(1), c);``

``const isMerge = (s3, s1, s2) => {let dp = [];if (s1 === "") {return s2 === s3;}if (s2 === "") {return s1 === s3;}if (s1.length + s2.length !== s3.length) {return false;}for (let i = 0; i < s1.length + 1; i++) {dp.push([]);for (let j = 0; j < s2.length + 1; j++) {if (i === 0 && j === 0) {dp[i].push(true);}else {let char = s3.charAt(i + j - 1);if (char === s1.charAt(i - 1) && dp[i -1][j]) {dp[i].push(true);}else if (char === s2.charAt(j - 1) && dp[i][j - 1]) {dp[i].push(true);}else {dp[i].push(false);}}}}return dp[s1.length][s2.length];``};``

### 🍗 题目3的答案

``function josephusSurvivor(n, k){return n < 1 ? 1 : (josephusSurvivor(n - 1, k) + --k) % n + 1;``}``

``function josephusSurvivor(n,k){res=1;for (var i=1;i<=n;i++) res=(res+k-1)%i+1;return res;``}``

``function josephusSurvivor(n,k){var people = [],p = 0;for (var i = 0; i < n; i++) { people.push(i+1); }while (people.length > 1) {p = (p + k - 1) % people.length;people.splice(p, 1);}return people.pop();``}``

``const josephusSurvivor = (n, k) =>``n - 1 ? (josephusSurvivor(n - 1, k) + k - 1) % n + 1 : 1;``

``function josephusSurvivor(n, k) {// Source: https://en.wikipedia.org/wiki/Josephus_problem#The_general_case// (I couldn't come up with the formula myself :p)return n === 1 ? 1 : ((josephusSurvivor(n - 1, k) + k - 1) % n) + 1;``}``

11 声望

4 粉丝

0 条评论

## 📖 题目1：标签生成器

• 它必须以井号（`#`）开头。
• 所有单词的首字母必须大写。
• 如果最终结果超过140个字符，则必须返回`false`
• 如果输入或结果为空字符串，则必须返回`false`

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

``function generateHashtag (str) {``}``

## 📖 题目2：合并的字符串检查器

``'codewars' is a merge from 'cdw' and 'oears':s:  c o d e w a r s   = codewarspart1:  c   d   w         = cdw``part2:    o   e   a r s   = oears``

``function isMerge(s, part1, part2) {return false;``}``

## 📖 题目3：约瑟夫斯幸存者

``josephus_survivor(7,3) => means 7 people in a circle;one every 3 is eliminated until one remains[1,2,3,4,5,6,7] - initial sequence[1,2,4,5,6,7] => 3 is counted out[1,2,4,5,7] => 6 is counted out[1,4,5,7] => 2 is counted out[1,4,5] => 7 is counted out[1,4] => 5 is counted out``[4] => 1 counted out, 4 is the last element - the survivor!``

``function josephusSurvivor(n,k){//your code here``}``

## 答案

### 🍗 题目1的答案

``function generateHashtag (str) {return str.length > 140 || str === '' ? false :'#' + str.split(' ').map(capitalize).join('');}function capitalize(str) {return str.charAt(0).toUpperCase() + str.slice(1);``}``

``function generateHashtag (str) {if(!str || str.length < 1) return false;var r = '#' + str.split(' ').map(function(el) {return el.charAt(0).toUpperCase() + el.slice(1).toLowerCase();}).join('');return r.length > 140?false:r;``}``

``function generateHashtag (str) {var hashtag = str.split(' ').reduce(function(tag, word) {return tag + word.charAt(0).toUpperCase() + word.substring(1);}, '#');return hashtag.length == 1 || hashtag.length > 140 ? false : hashtag;``}``

``function generateHashtag(str) {var hashed = '#' + str.split(' ').map(function(v) {return v.charAt(0).toUpperCase() + v.slice(1);}).join('');return hashed.length > 140 || str == "" ? false : hashed;``}``

``function generateHashtag (str) {var s = "#" + str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()}).split(" ").join("");if(str && s.length <= 140){return s;} return false;``}``

### 🍗 题目2的答案

``function isMerge(s, part1, part2) {return !s ? !(part1 || part2) :s[0] == part1[0] && isMerge(s.slice(1), part1.slice(1), part2) ||s[0] == part2[0] && isMerge(s.slice(1), part1, part2.slice(1));``}``

``function isMerge(s, part1, part2) {if (s === "" && part1 === "" && part2 === "") return true;if (s !== ""){if (s[0] === part1[0] && part1[0] === part2[0]){// we make two branches of an recursion and get OR of the answersreturn (isMerge(s.slice(1), part1.slice(1), part2) || isMerge(s.slice(1), part1, part2.slice(1)));} else if (s[0] === part1[0]){return isMerge(s.slice(1), part1.slice(1), part2);} else if (s[0] === part2[0]){return isMerge(s.slice(1), part1, part2.slice(1));}}return false;}``

``const isMerge = (s, part1, part2) =>(fn => !s ? !(part1 || part2) : fn(part1, part2) || fn(part2, part1))``((p1, p2) => s[0] === p1[0] && isMerge(s.slice(1), p1.slice(1), p2));``

``var isMerge = (s, x, y) => !s  ? !(x || y) : check(s, x, y) || check(s, y, x);``var check   = (a, b, c) => a[0] == b[0] && isMerge(a.slice(1), b.slice(1), c);``

``const isMerge = (s3, s1, s2) => {let dp = [];if (s1 === "") {return s2 === s3;}if (s2 === "") {return s1 === s3;}if (s1.length + s2.length !== s3.length) {return false;}for (let i = 0; i < s1.length + 1; i++) {dp.push([]);for (let j = 0; j < s2.length + 1; j++) {if (i === 0 && j === 0) {dp[i].push(true);}else {let char = s3.charAt(i + j - 1);if (char === s1.charAt(i - 1) && dp[i -1][j]) {dp[i].push(true);}else if (char === s2.charAt(j - 1) && dp[i][j - 1]) {dp[i].push(true);}else {dp[i].push(false);}}}}return dp[s1.length][s2.length];``};``

### 🍗 题目3的答案

``function josephusSurvivor(n, k){return n < 1 ? 1 : (josephusSurvivor(n - 1, k) + --k) % n + 1;``}``

``function josephusSurvivor(n,k){res=1;for (var i=1;i<=n;i++) res=(res+k-1)%i+1;return res;``}``

``function josephusSurvivor(n,k){var people = [],p = 0;for (var i = 0; i < n; i++) { people.push(i+1); }while (people.length > 1) {p = (p + k - 1) % people.length;people.splice(p, 1);}return people.pop();``}``

``const josephusSurvivor = (n, k) =>``n - 1 ? (josephusSurvivor(n - 1, k) + k - 1) % n + 1 : 1;``

``function josephusSurvivor(n, k) {// Source: https://en.wikipedia.org/wiki/Josephus_problem#The_general_case// (I couldn't come up with the formula myself :p)return n === 1 ? 1 : ((josephusSurvivor(n - 1, k) + k - 1) % n) + 1;``}``