Guest User

Untitled

a guest
Mar 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. /**
  2. * 写一个函数,传入一个数组,
  3. * 要求返回数组中所有的元素组合的数组
  4. *
  5. * [1, 2] => [
  6. * 12,
  7. * 21,
  8. * ]
  9. * [1, 2, 3] => [
  10. * 123, 132,
  11. * 213, 231,
  12. * 312, 321,
  13. * ]
  14. * [1, 2, 3, 4] => [
  15. * 1234, 1243, 1324, 1342, 1423, 1432,
  16. * 2134, 2143, 2314, 2341, 2413, 2431,
  17. * 3124, 3142, 3214, 3241, 3412, 3421,
  18. * 4123, 4132, 4213, 4231, 4312, 4321,
  19. * ]
  20. *
  21. */
  22.  
  23. const DEBUG = false
  24.  
  25. const combineArray = (data, arr, comb) => {
  26. arr = arr || data
  27. comb = comb || ''
  28.  
  29. return arr.map((value, index, currentArr) => {
  30. const _comb = comb ? `${comb},${value}` : `${value}`
  31.  
  32. const l = index - 1
  33. const r = index + 1
  34.  
  35. const p = currentArr.slice(0, Math.max(0, l + 1))
  36. const n = currentArr.slice(Math.min(r, currentArr.length))
  37.  
  38. const _arr = p.concat(n)
  39.  
  40. if (DEBUG) {
  41. console.group('flag')
  42.  
  43. console.log(`%cdata:`, 'color: purple', `[${data}]`)
  44.  
  45. console.log(`@:`, index)
  46. console.log(`currentArr => ${currentArr}`)
  47. console.log(`comb => ${comb}`)
  48. console.log(`arr => ${arr}`)
  49. console.log(`-----------------------`)
  50.  
  51. console.log(`_comb => ${_comb}`)
  52. console.log(`_arr => ${_arr}`)
  53. console.log(`=======================`)
  54.  
  55. console.groupEnd()
  56. }
  57.  
  58. if (_arr.length) {
  59. return combineArray(data, _arr, _comb)
  60. } else {
  61. if (DEBUG) {
  62. console.log(
  63. `%c● %c${_comb}`,
  64. 'color: red; font-size: 14px',
  65. 'padding: .2em .42em; font-weight: 700; background-color: #ddd; border-radius: 4px'
  66. )
  67. }
  68.  
  69. return _comb
  70. }
  71.  
  72. })
  73. }
  74.  
  75. const formatArray = (data, fn, result) => {
  76. fn = fn || factorial(data.length)
  77. result = result || []
  78.  
  79. for (let o of data) {
  80. if (Array.isArray(o)) {
  81. formatArray(o, fn, result)
  82. } else {
  83. result.push(o)
  84. }
  85. }
  86.  
  87. if (result.length === fn) {
  88. return result
  89. }
  90. }
  91.  
  92. const factorial = (num) => {
  93. if (Number.isInteger(num)) {
  94. if (num === 0) {
  95. return 1
  96. } else {
  97. return num * factorial(num - 1)
  98. }
  99. }
  100. }
  101.  
  102. const a = [1, 2]
  103. const b = [1, 2, 3]
  104. const c = [1, 2, 3, 4]
  105.  
  106.  
  107. // console.log( combineArray(a) )
  108. // console.log( combineArray(b) )
  109. console.log( combineArray(c) )
  110. console.log( formatArray(combineArray(c)) )
  111.  
  112. // console.log( combineArray( ['A'] ) )
  113. // console.log( combineArray( ['A', 'B'] ) )
  114. console.log( combineArray( ['A', 'B', 'C'] ) )
  115.  
  116. console.log( formatArray(combineArray( ['A', 'B', 'C'] )) )
Add Comment
Please, Sign In to add comment