Eloquent Javascript - ch4 - arraytoList - 递归

时间:2017-10-03 08:24:23

标签: javascript recursion

我在Eloquent Javascript中通过所有与此特定练习相关的解决方案,但没有解决我的问题。

您可以看到arrayToList function here

我的问题是如何通过从开始到结束而不是结束开始来编写程序。

这是我的解决方案:

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId 'pl.test.simplewidget'
        minSdkVersion 15
        targetSdkVersion 25
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.8.0' 

    compile group: 'joda-time', name: 'joda-time', version: '2.9.9'

    compile 'com.github.zurche:plain-pie:v0.2.1' //pie chart library zurche/plain-pie
    compile 'com.github.lzyzsd:circleprogress:1.2.1' // circle progress bar

    compile 'com.jakewharton:butterknife:8.6.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
}

这很好。

function arrayToList(arr) {
  var entry = { value: null, rest: null}
  for (var i = 0; i < arr.length; i++) {
    if(entry.value == null) 
      entry.value = arr[i]
    else{ 
      entry.rest = arrayToList([arr[i]])
        }
  }
  return entry
}

console.log(arrayToList([10,20]));

但是下面给出了问题

Input - console.log(arrayToList([10, 20]));
output - { value: 10, rest: { value: 20, rest: null } }

我不知道如何递归调用,这样即使数组中有超过2个元素,它仍然可以创建列表结构

4 个答案:

答案 0 :(得分:0)

根据您与空数组的链接,该函数返回null,并且似乎只有未命名的数组将在此练习中传递,因此我不关心原始数组(在函数执行后将是emtpy)。

&#13;
&#13;
function arrayToList(arr, curr = null){
  switch(curr = arr.shift()){
    case undefined:
      return null
      break
    default:
      return {value: curr, rest: arrayToList(arr)}
  }
}

let arr = [10,20,30,0,50]
console.log(arrayToList(arr))
&#13;
.as-console-wrapper{top: 0; max-height: none!important;}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你很亲密。您需要在递归调用中传递数组的其余部分,例如

function arrayToList(arr) {
  var entry = arr.length? {value:arr[0]} : {};
  entry.rest = arr.length > 1? arrayToList(arr.slice(1)) : null;
  return entry;
}

可以缩短为:

value: undefined

您没有说明如何处理稀疏数组,上面要求连续数组没有丢失的元素。对于稀疏数组,结果中最终会有大量convert -list format

答案 2 :(得分:0)

我相信这种方法比this answer有所改进,因为空数组正确返回一个具有属性valuerest的对象,数组中的虚假值不会导致提前退出,并且通过将数组传递给函数而不会对数组进行变异。它使用简单的自上而下递归来生成嵌套列表。

&#13;
&#13;
function arrayToList (array, entry = { value: null, rest: null }) {
  if (array.length === 0) {
    return entry
  }

  return {
    value: array[0],
    rest: arrayToList(array.slice(1), null)
  }
}

console.log(arrayToList([])) // empty
console.log(arrayToList([10, 0, 20])) // falsy
&#13;
&#13;
&#13;

答案 3 :(得分:0)

function arrayToList(array) {
  var entry = { value: null, rest: null };

  for (var i = 0; i < array.length; i++) {

    entry = { value: array[i], rest: entry };
  }

  return entry;
}