fork download
  1. def f1(list_of_list):
  2. result = []
  3. for inner_list in list_of_list:
  4. for x in inner_list:
  5. if x not in result:
  6. result.append(x)
  7. return result
  8.  
  9. nested_list = [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
  10. print('Original List', nested_list)
  11. print('Transformed Flat List', f1(nested_list))//Enter you code here.
  12. //Please indent properly.
  13.  
  14. <?php
  15. //program to find the common elements of the two array
  16. //here we have to array A and B from which w have to find the common element
  17. //first we sort then using merge sort and after then for traversing through
  18. //the array in one iteration we can find the comman elements the given array
  19. //this is an inspace algorithm meansno extra space is needed
  20.  
  21. //best case time complexity=O(nlogn)
  22. //O(nlogn)-> for sorting
  23. //O(n)-> for while loop to find comman element
  24.  
  25. //average case time complexity=O(nlogn)
  26. //O(nlogn)-> for sorting
  27. //O(n)-> for while loop to find comman element
  28.  
  29. //worst case time complexity =O(nlogn)
  30. //O(nlogn)-> for sorting
  31. //O(n)-> for while loop to find comman element
  32.  
  33.  
  34.  
  35. $commonArray=array();
  36. $A=array(3,4,5,6,7,8,9,10,36,58,27,48);
  37. $B=array(3,10,4,5,6,8,12,24,37,27,50);
  38. sort($A);
  39. sort($B);
  40. $size1=sizeof($A);
  41. $size2=sizeof($B);
  42. $counter1=0;
  43. $counter2=0;
  44. while(($counter1< $size1) && ($counter2)<($size2))//traversing through the array
  45. {
  46.  
  47. if ($A[$counter1] == $B[$counter2])
  48. {
  49. array_push($commonArray,$A[$counter1]); //to enter comman element in the output array
  50. $counter1=$counter1+1;
  51. $counter2=$counter2+1;
  52. }
  53. else if ($A[$counter1] < $B[$counter2])
  54. {
  55. $counter1=$counter1+1; }
  56.  
  57. else
  58. {
  59. $counter2=$counter2+1;
  60. }
  61. }
  62.  
  63. print_r($commonArray);//to print the output array
  64. ?>
  65.  
  66.  
Success #stdin #stdout 0.03s 26152KB
stdin
Standard input is empty
stdout
def f1(list_of_list):
    result = []
    for inner_list in list_of_list:
        for x in inner_list:
            if x not in result:
                result.append(x)
    return result

nested_list = [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
print('Original List', nested_list)
print('Transformed Flat List', f1(nested_list))//Enter you code here.
//Please indent properly.

Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 8
    [5] => 10
    [6] => 27
)