codewars练习记录7 js

【6 kyu】Persistent Bugger.

Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.

For example (Input --> Output):

39 --> 3 (because 39 = 27, 27 = 14, 14 = 4 and 4 has only one digit)
999 --> 4 (because 9
99 = 729, 729 = 126, 126 = 12, and finally 12 = 2)
4 --> 0 (because 4 is already a one-digit number)

翻译:
编写一个函数persistence,它接受一个正参数num并返回其乘法持久性,即必须将num中的数字相乘到一个数字的次数。
解一:

function persistence(num) {
  var arr = num.toString().split('')
  var sum = 0
  var count = 0
  while (arr.length > 1) {
    sum = arr.reduce((a, b) =>
      a * b
    )
    count++
    arr = sum.toString().split("")
  }
  return count
}

解二:

const persistence = num => {
  return `${num}`.length > 1 
    ? 1 + persistence(`${num}`.split('').reduce((a, b) => a * b)) 
    : 0;
}
【6 kyu】Help the bookseller !

A bookseller has lots of books classified in 26 categories labeled A, B, … Z. Each book has a code c of 3, 4, 5 or more characters. The 1st character of a code is a capital letter which defines the book category.

In the bookseller’s stocklist each code c is followed by a space and by a positive integer n (int n >= 0) which indicates the quantity of books of this code in stock.

For example an extract of a stocklist could be:

L = {“ABART 20”, “CDXEF 50”, “BKWRK 25”, “BTSQZ 89”, “DRTYM 60”}.
or
L = [“ABART 20”, “CDXEF 50”, “BKWRK 25”, “BTSQZ 89”, “DRTYM 60”] or …

You will be given a stocklist (e.g. : L) and a list of categories in capital letters e.g :

M = {“A”, “B”, “C”, “W”}
or
M = [“A”, “B”, “C”, “W”] or …

and your task is to find all the books of L with codes belonging to each category of M and to sum their quantity according to each category.

For the lists L and M of example you have to return the string (in Haskell/Clojure/Racket/Prolog a list of pairs):

(A : 20) - (B : 114) - (C : 50) - (W : 0)

where A, B, C, W are the categories, 20 is the sum of the unique book of category A, 114 the sum corresponding to “BKWRK” and “BTSQZ”, 50 corresponding to “CDXEF” and 0 to category ‘W’ since there are no code beginning with W.

If L or M are empty return string is “” (Clojure/Racket/Prolog should return an empty array/list instead).

Notes:
In the result codes and their values are in the same order as in M.
See “Samples Tests” for the return.
翻译:
一个书商有很多书,分为26个类别,分别标有A、B、…Z。每本书的代码c为3、4、5个或更多字符。代码的第一个字符是定义图书类别的大写字母。
在书商的库存清单中,每个代码c后面都有一个空格和一个正整数n(int n>=0),表示该代码的库存图书数量。
例如,库存清单的摘录可以是:
L={“ABART 20”,“CDXEF 50”,“BKWRK 25”,“BTSQZ 89”,“DRTYM 60”}。

L=[“ABART 20”,“CDXEF 50”,“BKWRK 25”,“BTSQZ 89”,“DRTYM 60”]或。。。。
您将收到一份库存清单(例如:L)和一份用大写字母表示的类别清单,例如:
M={“A”、“B”、“C”、“W”}

M=[“A”、“B”、“C”、“W”]或。。。
你的任务是找到所有L的书,它们的代码属于M的每一类,并根据每一类求出它们的数量。
对于示例的列表L和M,您必须返回字符串(在Haskell/Clojure/Racket/Prolog中,是一对列表):
(A:20)-(B:114)-(C:50)-(W:0)
其中A、B、C、W是类别,20是类别A的唯一书籍的总和,114是对应于“BKWRK”和“BTSQZ”的总和,50对应于“CDXEF”,0对应于类别“W”,因为没有以W开头的代码。
如果L或M为空,则返回字符串为“”(Clojure/Racket/Prolog应返回空数组/列表)。
笔记:
结果代码及其值的顺序与M中的相同。
请参阅“样品测试”了解退货信息。

大概意思就是:

b = [“BBAR 150”, “CDXE 515”, “BKWR 250”, “BTSQ 890”, “DRTY 600”]
c = [“A”, “B”, “C”, “D”]
b代表书类别,例如“BBAR”开头代表B类图书,后面跟着的数字代表书的本数。
c代表需要计算这些类的本数,例如这里是A、B、C、D类,因此在b中找A、B、C、D类的书并分别计总数。
得出的结果应该是:(A : 0) - (B : 1290) - (C : 515) - (D : 600)

解一:

function stockList(listOfArt, listOfCat){
   if (listOfArt.length == 0 || listOfCat.length == 0){
     return ""
   }  
   let res = []
  for (cat in listOfCat) {
    let total = 0
    for (art in listOfArt) {

      if (listOfArt[art][0] == listOfCat[cat][0]) {
        total = total + Number(listOfArt[art].split(' ')[1])
      }
    }
    if (res.length != 0) { res += " - " }
    res += "(" + listOfCat[cat] + " : " + total + ")"
  }
  return res
}

解二:

function stockList(listOfArt, listOfCat) {
  if (!listOfArt.length || !listOfCat.length) return ''
  return listOfCat.map(w => {
    const s = listOfArt.reduce((a, b) => a + (b.charAt(0) === w ? +b.split(' ')[1] : 0), 0)
    return `(${w} : ${s})`
  }).join(' - ')
}
【6 kyu】Buying a car

Let us begin with an example:
A man has a rather old car being worth 2000. He saw a secondhand car being worth $8000. He wants to keep his old car until he can buy the secondhand one.

He thinks he can save $1000 each month but the prices of his old car and of the new one decrease of 1.5 percent per month. Furthermore this percent of loss increases of 0.5 percent at the end of every two months. Our man finds it difficult to make all these calculations.

Can you help him?

How many months will it take him to save up enough money to buy the car he wants, and how much money will he have left over?

Parameters and return of function:

parameter (positive int or float, guaranteed) start_price_old (Old car price)
parameter (positive int or float, guaranteed) start_price_new (New car price)
parameter (positive int or float, guaranteed) saving_per_month
parameter (positive float or int, guaranteed) percent_loss_by_month
nbMonths(2000, 8000, 1000, 1.5) should return [6, 766] or (6, 766)

Detail of the above example:

end month 1: percent_loss 1.5 available -4910.0
end month 2: percent_loss 2.0 available -3791.7999…
end month 3: percent_loss 2.0 available -2675.964
end month 4: percent_loss 2.5 available -1534.06489…
end month 5: percent_loss 2.5 available -395.71327…
end month 6: percent_loss 3.0 available 766.158120825…
return [6, 766] or (6, 766)
where 6 is the number of months at the end of which he can buy the new car and 766 is the nearest integer to 766.158… (rounding 766.158 gives 766).

Note:

Selling, buying and saving are normally done at end of month. Calculations are processed at the end of each considered month but if, by chance from the start, the value of the old car is bigger than the value of the new one or equal there is no saving to be made, no need to wait so he can at the beginning of the month buy the new car:

nbMonths(12000, 8000, 1000, 1.5) should return [0, 4000]
nbMonths(8000, 8000, 1000, 1.5) should return [0, 0]
We don’t take care of a deposit of savings in a bank:-)

翻译:
让我们从一个例子开始:
一个男人有一辆相当旧的车,价值2000美元。他看到一辆二手车价值8000美元。他想把他的旧车留到能买二手车为止。
他认为他每个月可以节省1000美元,但他的旧车和新车的价格每月下降1.5%。此外,在每两个月结束时,这一百分比的损失增加了0.5%。我们的人发现很难进行所有这些计算。
你能帮他吗?
他需要多少个月才能攒够钱来买他想要的汽车,他还剩下多少钱?
参数和函数返回:
参数(正整数或浮点,有保证)start_price_old(旧车价格)
参数(正整数或浮点,有保证)start_price_new(新车价格)
参数(正整数或浮点,保证)saving_per_month(每月存钱)
参数(正浮点数或整数,保证)percent_loss_by_month(损失比例)
nbMonths(2000、8000、1000、1.5)应返回[6,766]或(6,766)
以上示例的详细信息:
月末1:percent_loss 1.5可用-4910.0
月末2:percent_loss 2.0可用-3791.7999。。。
月末3:percent_loss 2.0可用-2675.964
月末4:percent_loss 2.5可用-1534.06489。。。
月末5:percent_loss 2.5可用-395.71327。。。
月末6:percent_loss 3.0可用766.158120825。。。
return[6766]或(6766)
其中6是他可以购买新车的月数,766是最接近766.158的整数……(766.158四舍五入为766)。
注:
销售、购买和储蓄通常在月末完成。计算在每个考虑的月末进行,但如果从一开始就偶然发现旧车的价值大于或等于新车的价值,则无需节省,无需等待,以便他可以在月初购买新车:
nbMonths(12000,8000,1000,1.5)应返回[0,4000]
nbMonths(8000,8000,1000,1.5)应返回[0,0]
我们不负责银行存款:-)

这道题难点在于读题,脑壳疼,大意是讲:旧车换新车,根据每月存的钱和每月车子价格的下降比例,问大概多久能换到车,还能剩下多少钱。
解:

function nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth){
  let months = 0
  let saving = 0
  while (startPriceOld + saving < startPriceNew) {
    months += 1
    saving += savingperMonth
    if (months % 2 == 0) {
      percentLossByMonth += 0.5
    }
    startPriceOld *= ((100 - percentLossByMonth) / 100)
    startPriceNew *= ((100 - percentLossByMonth) / 100)

  }
  return [months, Math.round(startPriceOld + saving - startPriceNew)]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值