codewars练习记录4

【8 kyu】Reversing Words in a String

You need to write a function that reverses the words in a given string. A word can also fit an empty string. If this is not clear enough, here are some examples:

As the input may have trailing spaces, you will also need to ignore unneccesary whitespace.

Example (Input --> Output)
"Hello World" --> "World Hello"
"Hi There." --> "There. Hi"

翻译:
您需要编写一个函数来反转给定字符串中的单词。单词也可以容纳空字符串。如果这还不够清楚,下面是一些示例:
由于输入可能有尾随空格,您还需要忽略不必要的空白。

解:

function reverse(string){
  return string.split(' ').reverse().join(' ')
}
【8 kyu】Merge two sorted arrays into one

You are given two sorted arrays that both only contain integers. Your task is to find a way to merge them into a single one, sorted in asc order. Complete the function mergeArrays(arr1, arr2), where arr1 and arr2 are the original sorted arrays.
You don't need to worry about validation, since arr1 and arr2 must be arrays with 0 or more Integers. If both arr1 and arr2 are empty, then just return an empty array.
Note: arr1 and arr2 may be sorted in different orders. Also arr1 and arr2 may have same integers. Remove duplicated in the returned result.

Examples (input -> output)
*[1, 2, 3, 4, 5], [6, 7, 8, 9, 10] -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*[1, 3, 5, 7, 9], [10, 8, 6, 4, 2] -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*[1, 3, 5, 7, 9, 11, 12], [1, 2, 3, 4, 5, 10, 12] -> [1, 2, 3, 4, 5, 7, 9, 10, 11, 12]

翻译:
给您两个排序数组,它们都只包含整数。您的任务是找到一种方法,将它们合并到一个单独的文件中,按asc顺序排序。完成函数mergeArrays(arr1,arr2),其中arr1和arr2是原始排序数组。
您不需要担心验证问题,因为arr1和arr2必须是包含0个或更多整数的数组。如果arr1和arr2都为空,则只返回一个空数组。

注:arr1和arr2可以按不同的顺序排序。arr1和arr2也可以有相同的整数。删除返回结果中的重复项。

解:

//数组拼接后去重再排序
function mergeArrays(arr1, arr2) {
 arr1.push(...arr2)
  return [...new Set(arr1)].sort((a,b)=>a-b)
}
【8 kyu】Add Length

What if we need the length of the words separated by a space to be added at the end of that same word and have it returned as an array?

Example(Input --> Output)
"apple ban" --> ["apple 5", "ban 3"]
"you will win" -->["you 3", "will 4", "win 3"]

Your task is to write a function that takes a String and returns an Array/list with the length of each word added to each element .
Note: String will have at least one element; words will always be separated by a space.
翻译:
如果我们需要在同一单词的末尾添加由空格分隔的单词的长度,并将其作为数组返回,该怎么办?
您的任务是编写一个函数,该函数接受一个String并返回一个Array/list,其中每个单词的长度添加到每个元素中。
注意:字符串将至少有一个元素;单词之间总是用空格隔开。

解:

function addLength(str) {
  return str.split(" ").map(x=> x + " " +x.length)
}
【8 kyu】How old will I be in 2099?

Philip's just turned four and he wants to know how old he will be in various years in the future such as 2090 or 3044. His parents can't keep up calculating this so they've begged you to help them out by writing a programme that can answer Philip's endless questions.
Your task is to write a function that takes two parameters: the year of birth and the year to count years in relation to. As Philip is getting more curious every day he may soon want to know how many years it was until he would be born, so your function needs to work with both dates in the future and in the past.
Provide output in this format: For dates in the future: "You are ... year(s) old." For dates in the past: "You will be born in ... year(s)." If the year of birth equals the year requested return: "You were born this very year!"
"..." are to be replaced by the number, followed and proceeded by a single space. Mind that you need to account for both "year" and "years", depending on the result.
Good Luck!
翻译:
菲利普刚满四岁,他想知道未来几年他会多大,比如2090岁或3044岁。他的父母无法继续计算这个数字,所以他们恳求你帮他们写一个程序,可以回答菲利普没完没了的问题。
你的任务是编写一个函数,它需要两个参数:出生年份和与之相关的年份。由于菲利普每天都变得越来越好奇,他可能很快就会想知道他出生前还有多少年,所以你的函数需要处理未来和过去的两个日期。
以这种格式提供输出:对于未来的日期:“您是…岁。”对于过去的日期:”您将在…年出生。“如果出生年份等于要求的年份,请返回:”您是今年出生的!“
“…”应替换为数字,后跟一个空格。请注意,根据结果,您需要同时考虑“年”和“年”。
祝你好运!
解:

// 主要处理year单复数形式
function  calculateAge(a,b) {
return a==b ? "You were born this very year!" : a>b ?  
       a-b==1 ? `You will be born in ${a-b} year.` : `You will be born in ${a-b} years.` :
       b-a==1 ? `You are ${b-a} year old.` :`You are ${b-a} years old.`
}
【8 kyu】Find the position!

When provided with a letter, return its position in the alphabet.

Input :: "a"
Ouput :: "Position of alphabet: 1"

翻译:
如果提供了字母,请返回其在字母表中的位置
解:

function position(letter){
  return `Position of alphabet: ${letter.toLowerCase().charCodeAt(0) - 96}`;
}
【7 kyu】Alternate capitalization

Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index 0 will be considered even.

For example, capitalize("abcdef") = ['AbCdEf', 'aBcDeF']. See test cases for more examples.

The input will be a lowercase string with no spaces.
翻译:
给定一个字符串,将分别占用偶数索引和奇数索引的字母大写,并按如下所示返回。索引0将被视为偶数。
解:

function capitalize(s){
  let odd = s.split("").map((x, i) => i % 2 != 0 ? x.toUpperCase() : x).join("");
  let even = s.split("").map((x, i) => i % 2 == 0 ? x.toUpperCase() : x).join("");
  return [even, odd];
};
【7 kyu】Sorted? yes? no? how?

Complete the method which accepts an array of integers, and returns one of the following:
"yes, ascending" - if the numbers in the array are sorted in an ascending order
"yes, descending" - if the numbers in the array are sorted in a descending order
"no" - otherwise
You can assume the array will always be valid, and there will always be one correct answer.
翻译:
完成接受整数数组的方法,并返回以下值之一:
“yes, ascending”-如果数组中的数字按升序排序
“yes, descending”-如果数组中的数字按降序排序
“no”-否则
您可以假设数组总是有效的,并且总是有一个正确的答案。
解:

function isSortedAndHow(array) {
   let arr = []
  for (let i = 0; i < array.length - 1; i++) {
    if (array[i] > array[i + 1]) {
      arr.push(true)
    } else {
      arr.push(false)
    }
  }
  return arr.every(x => x == true) ? "yes, descending" : arr.every(x => x == false) ? "yes, ascending" : "no" 
}
// 简便写法
/**
function isSortedAndHow(arr) {
  return arr.every((x,i)=>i==0||arr[i]>=arr[i-1])?'yes, ascending':
         arr.every((x,i)=>i==0||arr[i]<=arr[i-1])?'yes, descending':'no'
}
**/
【7 kyu】Factorial

Your task is to write function factorial.
翻译:
您的任务是编写函数阶乘。
解一:

function factorial(n){
 let sum = 1;
  for (let i = 1; i <= n; i++){
    sum = sum * i;
  }
  return sum; 
}

解二:

function factorial(n){
  return n ? n * factorial(n-1) : 1;
}
【8 kyu】Powers of 2

Complete the function that takes a non-negative integer n as input, and returns a list of all the powers of 2 with the exponent ranging from 0 to n ( inclusive ).

Examples
n = 0 ==> [1] ==> [2^0]
n = 1 ==> [1, 2] ==> [2^0, 2^1]
n = 2 ==> [1, 2, 4] ==> [2^0, 2^1, 2^2]

解:

function powersOfTwo(n){
  var result = [];
  for (var i = 0; i <= n; i++) {
    result.push(Math.pow(2, i));
  }
  return result;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容

  • API ReferencePreliminariesAll declarations are in jansson...
    梦落迹南天阅读 687评论 0 0
  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile丽语阅读 3,826评论 0 6
  • HTML 注释 HTML 标签分类(按照功能): 文本的修饰,文字排版,图片,链接,表格,列表,表单,框架 ,语音...
    Ethan_Lan阅读 446评论 0 0
  • C语言经典例程100例 这篇文章主要介绍了C语言经典例程100例,经典c程序100例,学习c语言的朋友可以参考一下...
    縸_3354阅读 334评论 0 0
  • ECMAScript - 学习笔记 🎬 🧩nvm node.js 包管理工具 nvm github[https:/...
    Super三脚猫阅读 611评论 0 1