2017-02-23 3 views
1

Ich muss eine Funktion erstellen (mit Pseudocode), die Tiefe des angegebenen Elements in einem Array zurückgibt (mit optionalen Arrays innerhalb), zB:Finden der Tiefe des angegebenen Elements in einem Array von Arrays (oder Liste von Listen usw.)

def array[] = {"a", {"b", {"c"}, "d"}, {{{}}}, "e"}; 

für "e" sollte es 0 zurückgibt, für "c" es sollte den Wert 2 usw. Wenn es gibt kein bestimmtes Element in Array-Funktion sollte den Wert -1 zurück.

Ich habe ein paar Mal versucht, aber ich habe keine Ahnung, für elegant (und arbeiten ..) Lösung, nur so viel:

func foo(array[], var element) { 
    int depth = 0; 
    boolean found = false; 
    boolean recursion = false; 
    boolean foundInRecursion = false; 

    for (int i = 0; i < array.length; i++) { 
     if (array[i] instanceof array[]) { 
      depth++; 
      recursion = true; 
      foo(array[i], element); 
      recursion = false; 
     } else if (array[i] == element) { 
      if (recursion) { 
       foundInRecursion = true; 
      } else { 
       found = true; 
      } 
     } 
    } 

    if (foundInRecursion) { 
     return depth; 
    } else if (found){ 
     return 0; 
    } else { 
     return -1; 
    } 
} 

ich wirklich jede mögliche Hilfe schätzen würde! Dank

Antwort

0

In Ihrem Pseudo-Code:

func depth(array[], var element) { 
    /* this function returns how deep the element is in the given array */ 
    for (int i = 0; i < array.length; i++) { 
     current = array[i] 

     if current == element { 
      return 0; // element is right in the array we are given 
     } 

     if (current instanceof array[]) { 
      // Whoa, subarray! How deep our element is in it? 
      subdepth = depth(current, element) 
      if subdepth >= 0 { 
       // Element was found in *sub*-array, so add 1 to the depth 
       return subdepth + 1; 
      } 
     } 
    } 
    // If we are here, we found nothing 
    return -1; 
} 
+0

Der Code funktioniert ordnungsgemäß. Es ist möglich, die aktuelle Tiefe zu überschreiten, um das Ganze rekursiv zu machen, ich habe es nicht so gemacht, um die Idee klarer zu zeigen. – avysk

+0

danke für die Klarstellung - ich habe heute etwas gelernt. :) –

0

Ich glaube, dass eine solche elegante Lösung sollte ein iterativer Code sein, der über jede Schicht geht.

public int IterativeDepth(List<Object> lst, T element) 
{ 
    // Set the first iteration 
    int depth = 0; 
    List<Object> next = new List<Object>(); 

    while (! IsNullOrEmpty(lst)) 
    // For each layer 
    { 
     foreach (var current in lst) 
     // For each element of a layer 
     { 
      if (current instanceof T && current == element) 
      // Found it, return the depth 
      { 
       return depth; 
      } 

      if (current instanceof List) { 
      // It's a list, append it to the next iteration 
       next.Add(current); 
      } 
     } 

     // Set the next iteration 
     lst = next; 
     next.clear(); 
     depth += 1; 
    } 

    // Found nothing 
    return -1; 
} 
Verwandte Themen