简体   繁体   English

我们如何获得两次递归调用的最小值,一个是加法,另一个是减法?

[英]How could we get min of two recursive calls, one which adds, another which subtracts?

We are doing this programming exercise: a Office Space #2 - Closest Three from Twos .我们正在做这个编程练习:办公空间 #2 - 距离最近的三个 The statement is:声明是:

You like listening to music at the office.你喜欢在办公室听音乐。

You frequently have to change the volume in your headphones thanks to your talkative coworkers, and you find yourself using the + and - on your keyboard to do so.由于您的同事健谈,您经常需要更改耳机的音量,而您发现自己使用键盘上的 + 和 - 来这样做。 The keys increase and decrease the volume by 2 with each keystroke.每次击键时,这些键将音量增加和减少 2。 There's just one problem - you prefer numbers that are divisible by 3.只有一个问题 - 您更喜欢可被 3 整除的数字。

Sometimes your coworkers are loud, and other times they're at lunch or late to work - Adjust the volume accordingly!有时您的同事很吵,有时他们正在吃午饭或上班迟到 - 相应地调整音量!

Write a function that takes in current volume and outputs how many times you will have to press the - or + key on your keyboard to make the volume a number that is divisible by three.编写一个函数,接收当前音量并输出您必须按键盘上的 - 或 + 键的次数,以使音量成为可被 3 整除的数字。

The music can't go lower than 0 volume on this plane of existance.在这个存在平面上,音乐不能低于 0 音量。

ex: volume = 8 : output = 1 (one stroke of the - key would take you to 6, which is divisible by 3, rather than the two strokes of the + key to get to 12)例如:volume = 8 : output = 1(按 - 键会带你到 6,它可以被 3 整除,而不是按两次 + 键会得到 12)

We have written the following code:我们编写了以下代码:

function musicalOCD(volume) {
  console.log("\n\nvolume: "+volume);
  if(volume%3==0) return 0;
  return Math.min(1+musicalOCD(volume+2),1+musicalOCD(volume-2));
}

However it only passes the base case test, when volume is divisible by 13.然而,它仅通过基本案例测试,当体积可被 13 整除时。

If we pass in other volume, it outputs:如果我们传入其他卷,它会输出:

RangeError: Maximum call stack size exceeded
    at Socket.Readable.removeListener (_stream_readable.js:852:47)
    at write (console.js:172:12)
    at Console.log (console.js:200:3)
    at musicalOCD (test.js:5:11)
    at musicalOCD (test.js:7:21)

Being the trace:作为踪迹:

volume: 2


volume: 4


volume: 6


volume: 2


volume: 4


volume: 6

So we could write the code to just do recursive calls adding values to volume:所以我们可以编写代码来进行递归调用,将值添加到音量:

function musicalOCD(volume) {
  console.log("\n\nvolume: "+volume);
  if(volume%3==0) return 0;
  return 1+musicalOCD(volume+2);
}

However in the following tests:但是在以下测试中:

describe("Fixed tests", function() {
  const testing = (volume, exp) => it(`Testing for ${volume}`, () => assert.strictEqual(musicalOCD(volume), exp));
  testing(4, 1);
  testing(12, 0);
  testing(20, 1);
  testing(22, 1);
  testing(68, 1);
});

Testing for 20 and 68 would output 2 instead of 1...测试 20 和 68 将输出 2 而不是 1...

And if we do the reverse:如果我们反过来做:

function musicalOCD(volume) {
  console.log("\n\nvolume: "+volume);
  if(volume%3==0) return 0;
  return 1+musicalOCD(volume-2);
}

Then testing for 22 and 4 would output 2 instead of 1...然后测试 22 和 4 将输出 2 而不是 1 ...

How could we combine both recursive calls in a general answer?我们如何将两个递归调用结合到一个通用答案中?

We have read:我们已阅读:

 const testing = (volume, exp) => { console.assert(musicalOCD(volume) === exp, 'Testing for ${volume} - failure'); }; testing(4, 1); testing(12, 0); testing(20, 1); testing(22, 1); testing(68, 1); console.log('Run tests - done'); function musicalOCD(volume) { return Math.min( // <------- will return min value of two tests _musicalOCD(volume, +2), _musicalOCD(volume, -2) ); } function _musicalOCD(volume, mod) { if (volume < 0) return 0; if (volume >= 100) return 0; //console.log("\\n\\nvolume: " + volume); if (volume % 3 == 0) return 0; return 1 + _musicalOCD(volume + mod, mod); }

You could use an approach where you fork the recursive call by testing decrementing forst and then incrementing.您可以使用一种方法,通过测试递减forst然后递增来分叉递归调用。

 function musicalOCD(volume) { if (volume % 3 === 0) return 0; return 1 + (musicalOCD(volume - 2) && musicalOCD(volume + 2)); } [[4, 1], [12, 0], [20, 1], [22, 1], [68, 1]].forEach(([v, r]) => console.log(v, musicalOCD(v), r));

With directions for testing.带有测试说明。

 function musicalOCD(volume, dir) { console.log(dir); if (volume % 3 === 0) return 0; return 1 + (musicalOCD(volume - 2, 'down') && musicalOCD(volume + 2, 'up')); } [[4, 1], [12, 0], [20, 1], [22, 1], [68, 1]].forEach(([v, r]) => console.log(v, musicalOCD(v), r));
 .as-console-wrapper { max-height: 100% !important; top: 0; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作减去数字的参数函数 - How to make an argument function which subtracts numbers 即时通讯使用onkeyup()来调用javascript函数,该函数会添加一些值,而在使用退格键时,它不会减去该值,而是会添加每个值 - im using onkeyup() to call javascript function which adds some value, while using backspace it not subtracts the value instead it adds every values 如何在 reactjs 中进行 function 异步,它从 redux 操作中调用另外两个函数 - How can I make a function Async in reactjs which calls another two functions from redux actions 如何获取我们单击的元素的索引 - How to get the index of which element we clicked on React state 加1减2 - React state adds 1 but subtracts 2 用JavaScript从另一个列表中减去一个列表中的值 - Subtracts values one list from another in javascript javascrpt,如何在创建两个数组的键/值对之前迭代创建对象? - javascrpt, how could we create an object iterating through two arrays which are the key/value pairs before creating them? 如何对一个调用另一个返回promise的函数进行单元测试? - How to unit test a function which calls another that returns a promise? 如何测试其中调用了另一个 API function 的 function - NodeJS - How to test a function which has calls a another API function in it - NodeJS 如何从可能被可选逗号字符分割的字符串中提取数据? - How does one extract data from a string which possibly could get split by an optional comma character?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM