[7 kyu] Convert an array of strings to array of numbers
Some really funny web dev gave you a sequence of numbers from his API response as an sequence of strings!
You need to cast the whole array to the correct type.
Create the function that takes as a parameter a sequence of numbers represented as strings and outputs a sequence of numbers.
ie:["1", "2", "3"] to [1, 2, 3]
Note that you can receive floats as well.
翻譯:
哦,不!
一些非常有趣的web開發(fā)人員從他的API響應(yīng)中給出了一系列數(shù)字作為字符串序列!
您需要將整個數(shù)組轉(zhuǎn)換為正確的類型。
創(chuàng)建一個函數(shù),該函數(shù)將一系列表示為字符串的數(shù)字作為參數(shù),并輸出一系列數(shù)字。
即:[“1”、“2”、“3”]至[1、2、3]
請注意,您也可以接收浮動。
解:
function toNumberArray(stringarray)
{
return stringarray.map(Number);
}
[8 kyu] Regex count lowercase letters
Your task is simply to count the total number of lowercase letters in a string.
Examples
lowercaseCount("abc"); ===> 3
lowercaseCount("abcABC123"); ===> 3
lowercaseCount("abcABC123!@€£#%^&*()_-+=}{[]|':;?/>.<,~"); ===> 0
lowercaseCount("abcdefghijklmnopqrstuvwxyz"); ===> 26
翻譯:
您的任務(wù)只是計算字符串中小寫字母的總數(shù)。
解一:
function lowercaseCount(str){
return (str.match(/[a-z]/g) || []).length
}
解二:
function lowercaseCount(str){
let count = 0
for (let i = 0; i < str.length; i++) {
if (str[i].charCodeAt() > 96 && str[i].charCodeAt() < 123) { count++ }
}
return count
}
[8 kyu] Exclamation marks series #6: Remove n exclamation marks in the sentence from left to right
Remove n exclamation marks in the sentence from left to right. n is positive integer.
Examples
remove("Hi!",1) === "Hi"
remove("Hi!",100) === "Hi"
remove("Hi!!!",1) === "Hi!!"
remove("Hi!!!",100) === "Hi"
remove("!Hi",1) === "Hi"
remove("!Hi!",1) === "Hi!"
remove("!Hi!",100) === "Hi"
remove("!!!Hi !!hi!!! !hi",1) === "!!Hi !!hi!!! !hi"
remove("!!!Hi !!hi!!! !hi",3) === "Hi !!hi!!! !hi"
remove("!!!Hi !!hi!!! !hi",5) === "Hi hi!!! !hi"
remove("!!!Hi !!hi!!! !hi",100) === "Hi hi hi"
翻譯:
從左到右去掉句子中的n個感嘆號。n是正整數(shù)。
解:
function remove(s,n){
for (var i=0;i<n;i++) s=s.replace("!","");
return s;
}
[8 kyu] Kata Example Twist
This is an easy twist to the example kata (provided by Codewars when learning how to create your own kata).
Add the value "codewars" to the array websites/Websites 1,000 times.
翻譯:
這是對示例kata(由Codewars在學(xué)習(xí)如何創(chuàng)建自己的kata時提供)的簡單修改。
將值“codewars”添加到陣列網(wǎng)站/網(wǎng)站1000次。
解:
var websites = new Array(1000).fill("codewars");
[7 kyu] Sort the Gift Code
Happy Holidays fellow Code Warriors!
Santa's senior gift organizer Elf developed a way to represent up to 26 gifts by assigning a unique alphabetical character to each gift. After each gift was assigned a character, the gift organizer Elf then joined the characters to form the gift ordering code.
Santa asked his organizer to order the characters in alphabetical order, but the Elf fell asleep from consuming too much hot chocolate and candy canes! Can you help him out?
Sort the Gift Code
Write a function called sortGiftCode/sort_gift_code/SortGiftCode that accepts a string containing up to 26 unique alphabetical characters, and returns a string containing the same characters in alphabetical order.
Examples (Input -- => Output):
"abcdef" -- => "abcdef"
"pqksuvy" -- => "kpqsuvy"
"zyxwvutsrqponmlkjihgfedcba" -- => "abcdefghijklmnopqrstuvwxyz"
翻譯:
代碼戰(zhàn)士們,節(jié)日快樂!
圣誕老人的高級禮物組織者Elf開發(fā)了一種方法,通過為每個禮物指定一個獨(dú)特的字母字符來表示多達(dá)26份禮物。在給每個禮物分配了一個角色后,禮物組織者精靈將這些角色連接起來,形成禮物訂購代碼。
圣誕老人要求組織者按字母順序排列角色,但小精靈因為吃了太多熱巧克力和糖果而睡著了!你能幫他嗎?
對禮品代碼進(jìn)行排序
編寫一個名為sortGiftCode/sort_gift_code/sortGiftCode的函數(shù),該函數(shù)接受最多包含26個唯一字母字符的字符串,并返回按字母順序包含相同字符的字符串。
解:
function sortGiftCode(code){
return code.split('').sort().join('');
}
[7 kyu] Gau? needs help! (Sums of a lot of numbers).
Due to another of his misbehaved, the primary school's teacher of the young Gau?, Herr J.G. Büttner, to keep the bored and unruly young schoolboy Karl Friedrich Gauss busy for a good long time, while he teaching arithmetic to his mates, assigned him the problem of adding up all the whole numbers from 1 through a given number n.
Your task is to help the young Carl Friedrich to solve this problem as quickly as you can; so, he can astonish his teacher and rescue his recreation interval.
Here's, an example:
f(n=100) // returns 5050
It's your duty to verify that n is a valid positive integer number. If not, please, return false (None for Python, null for C#, 0 for COBOL).
Note: the goal of this kata is to invite you to think about some 'basic' mathematic formula and how you can do performance optimization on your code.
Advanced - experienced users should try to solve it in one line, without loops, or optimizing the code as much as they can.
翻譯:
由于另一個行為不端的原因,年輕的Gau?的小學(xué)老師J.G.Büttner先生,為了讓這個無聊而不守規(guī)矩的年輕男孩Karl Friedrich Gauss在給他的同學(xué)們教算術(shù)的同時,讓他把從1到給定數(shù)字n的所有整數(shù)加起來。
你的任務(wù)是幫助年輕的卡爾·弗里德里希盡快解決這個問題;這樣,他就可以讓老師大吃一驚,挽救他的娛樂時間。
下面是一個例子:
f(n=100)//返回5050
你有責(zé)任驗證n是一個有效的正整數(shù)。如果不是,請返回false(Python為無,C#為空,COBOL為0)。
注意:這個kata的目的是邀請你思考一些“基本”的數(shù)學(xué)公式,以及如何對代碼進(jìn)行性能優(yōu)化。
高級經(jīng)驗豐富的用戶應(yīng)該嘗試用一行代碼來解決這個問題,而不需要循環(huán),或者盡可能地優(yōu)化代碼
解:
function f(n){
return /(^[1-9]\d*$)/.test(n) ? (1+n)*(n/2) : false
};