這個題目是 Kayak 發佈的代碼挑戰題目。java
最簡單的描述就是不使用循環,輸出 0 到 5,而後一樣不是會用循環的方式再次輸出 5 到 0。git
Write a program that counts in sequential order when given a start and end value - without using any iterative programing loops, i.e. while, for, do, for-each, etc.github
You can assume that both the start and end values will always be positive and that the start value will always be less then the end value. There should only be one method with the following signature:less
void countUp(int start, int end) {
// All code exercise code should go here
}oop
Here is example output with start=0 and end=5:測試
[ 0,1,2,3,4,5]ui
Continuing with part 1 change the output of the test, so that it now prints out in sequential order to the end value (only once), but then also counts down to the start value.spa
Again, using no iterative loops, and assuming that both the start and end values will always be positive and that start value will always be less then the end value. There should only be one method with the following signature:翻譯
void countUpAndDown(int start, int end) {
// All code exercise code should go here
}debug
Here is example output with start=0 and end=5:
[0,1,2,3,4,5,4,3,2,1,0]
這裏我不按照原文一字一字的翻譯,可是儘可能按照題目的要求把題目解釋清楚。
最簡單的描述就是不使用循環,輸出 0 到 5,而後一樣不是會用循環的方式再次輸出 5 到 0。
本題目分 2 部分,第一部分是不使用循環的方式輸出 0 到 5,第二部分是不使用循環的方式輸出 0 到 5 之後,再輸出 5 到 0。
其中須要注意的是 5 只能輸出一次。
不使用 For 循環的方式輸出 0 到 5 ,咱們能夠想到有幾個方法。
第一個方法可能比較容易想到的就是遞歸調用,你能夠根據輸入的值,遞歸調用須要的次數就能夠輸出值了。你還能夠採用計算機時鐘的方式進行輸出。
在這裏咱們採用遞歸調用的方式進行輸出。
源代碼和有關代碼的更新請訪問 GitHub:
測試類請參考:
代碼思路請參考:
package com.ossez.codebank.interview; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * https://www.cwiki.us/display/ITCLASSIFICATION/Count+Up+Down * * @author YuCheng * */ public class KayakCountUpDown { private final static Logger logger = LoggerFactory.getLogger(KayakCountUpDown.class); static int minNumber = 0; static int maxNumber = 0; int tmpN = 0; List<Integer> retList = new ArrayList<Integer>(); /** * * @param start * @param end * @return */ public List<Integer> countUp(int start, int end) { logger.debug("BEGIN"); maxNumber = end; tmpN = start; moveUp(0); retList.add(end); return retList; } /** * * @param start * @param end * @return */ public List<Integer> countUpDown(int start, int end) { logger.debug("BEGIN"); minNumber = start; maxNumber = end; tmpN = start; moveUp(0); retList.add(end); moveDown(1); return retList; } /** * * @param n */ private void moveUp(int n) { retList.add(tmpN); tmpN++; if (tmpN != maxNumber) { moveUp(tmpN + 1); } } /** * * @param n */ private void moveDown(int n) { tmpN = (maxNumber - n); retList.add(tmpN); if (tmpN != minNumber) { moveDown(n + 1); } } }
上面程序的測試結果以下:
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - TEST Count Up and Down
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - [2 -> 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP - [2, 3, 4, 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP & DOWN - [2, 3, 4, 5, 4, 3, 2]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - [0 -> 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP - [0, 1, 2, 3, 4, 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP & DOWN - [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - [-1 -> 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP - [-1, 0, 1, 2, 3, 4, 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP & DOWN - [-1, 0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0, -1]