codewars练习记录6 js

【6 kyu】Tortoise racing

Two tortoises named A and B must run a race. A starts with an average speed of 720 feet per hour. Young B knows she runs faster than A, and furthermore has not finished her cabbage.

When she starts, at last, she can see that A has a 70 feet lead but B’s speed is 850 feet per hour. How long will it take B to catch A?

More generally: given two speeds v1 (A’s speed, integer > 0) and v2 (B’s speed, integer > 0) and a lead g (integer > 0) how long will it take B to catch A?

The result will be an array [hour, min, sec] which is the time needed in hours, minutes and seconds (round down to the nearest second) or a string in some languages.

If v1 >= v2 then return nil, nothing, null, None or {-1, -1, -1} for C++, C, Go, Nim, Pascal, COBOL, Erlang, [-1, -1, -1] for Perl,[] for Kotlin or “-1 -1 -1”.

Examples:

(form of the result depends on the language)
race(720, 850, 70) => [0, 32, 18] or “0 32 18”
race(80, 91, 37) => [3, 21, 49] or “3 21 49”

Note:
See other examples in “Your test cases”.

In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings.

** Hints for people who don’t know how to convert to hours, minutes, seconds:

Tortoises don’t care about fractions of seconds
Think of calculation by hand using only integers (in your code use or simulate integer division)
or Google: “convert decimal time to hours minutes seconds”

翻译:
两只名叫A和B的乌龟必须参加比赛。A以每小时720英尺的平均速度开始。小B知道她跑得比A快,而且还没吃完她的卷心菜。
当她开始时,她终于可以看到A领先70英尺,但B的速度是每小时850英尺。B要多长时间才能赶上A?
更一般地说:给定两个速度v1(A的速度,整数>0)和v2(B的速度,integer>0)以及一个领先g(整数>0,B需要多长时间才能赶上A?
结果将是一个数组[hour,min,sec],它是以小时、分钟和秒为单位的时间(四舍五入到最接近的秒),或者在某些语言中是一个字符串。
如果v1>=v2,那么对于C++、C、Go、Nim、Pascal、COBOL、Erlang,返回nil、nothing、null、None或{-1、-1、-1},对于Perl,返回[-1、-1,-1],对于Kotlin,返回[]或“-1-1-1-1”。
注:
请参阅“您的测试用例”中的其他示例。
在Fortran中,与任何其他语言一样,返回的字符串不允许包含任何冗余的尾随空格:您可以使用动态分配的字符串。
**对不知道如何转换为小时、分钟、秒的人的提示:
乌龟不在乎秒的分数
考虑只使用整数手工计算(在代码中使用或模拟整数除法)
或谷歌:“将十进制时间转换为小时-分-秒”
解1:

function race(v1, v2, g){
  let time=g/(v2-v1);
  return v2>v1 ? [Math.floor(time),Math.floor(time*60%60),Math.floor(time*3600%60)] : null;
}

解2:

function race(v1, v2, g) {
    if (v1 >= v2) {
      return null;
    }
    var time = new Date (g / (v2 - v1) * 3600 * 1000);
    return [time.getHours(), time.getMinutes(), time.getSeconds()];
}
【6 kyu】 Two Sum

Write a function that takes an array of numbers (integers for the tests) and a target number. It should find two different items in the array that, when added together, give the target value. The indices of these items should then be returned in a tuple / list (depending on your language) like so: (index1, index2).

For the purposes of this kata, some tests may have multiple answers; any valid solutions will be accepted.

The input will always be valid (numbers will be an array of length 2 or greater, and all of the items will be numbers; target will always be the sum of two different items from that array).
翻译:
编写一个函数,该函数接受一个数字数组(用于测试的整数)和一个目标数字。它应该在数组中找到两个不同的项,当它们相加时,会给出目标值。然后,这些项的索引应该以元组/列表(取决于您的语言)的形式返回,如:(index1,index2)。
为了这个kata的目的,一些测试可能有多个答案;任何有效的解决方案都将被接受。
输入将始终有效(数字将是长度为2或更大的数组,所有项都将是数字;目标将始终是该数组中两个不同项的总和)。
示例:

twoSum([1, 2, 3], 4) // returns [0, 2] or [2, 0]

解:

function twoSum(numbers, target) {
    for (var i = 0; i < numbers.length-1; i++) {
        for (var j = i+1; j < numbers.length; j++) {
            if (numbers[i] + numbers[j] === target) return [i, j];
        }
    }
}
【6 kyu】Counting Duplicates

Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.

Example

“abcde” -> 0 # no characters repeats more than once
“aabbcde” -> 2 # ‘a’ and ‘b’
“aabBcde” -> 2 # ‘a’ occurs twice and ‘b’ twice (b and B)
“indivisibility” -> 1 # ‘i’ occurs six times
“Indivisibilities” -> 2 # ‘i’ occurs seven times and ‘s’ occurs twice
“aA11” -> 2 # ‘a’ and ‘1’
“ABBA” -> 2 # ‘A’ and ‘B’ each occur twice

翻译:
编写一个函数,该函数将返回输入字符串中多次出现的不区分大小写的字母字符和数字的计数。可以假定输入字符串只包含字母(大写和小写)和数字。
解:

function duplicateCount(text){
  return (text.toLowerCase().split('').sort().join('').match(/([^])\1+/g) || []).length;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值