如何找到3×15阵列1×15阵列? [英] How to find 1 X 15 array from 3 X 15 array?

查看:114
本文介绍了如何找到3×15阵列1×15阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去创建一个从3×15阵列1×15尺寸独特的阵列。我没能做到这一点。请帮我在这。先谢谢了。

3×15阵列的是

<$p$p><$c$c>Array[(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3)]

解决方案

您有一组3个字符(1,2,3),你想从3个字符的字母表中的15个字母每一个可能的字。解决的办法是递归的:

 让大小是你的字的大小,给定的参数
让字母是你的字符集,给定的参数
如果大小为0,那么
  与空字符串返回集合
我们的结果是可能的字的集合,开始空
让一部分是问题的大小-1和字母的解决方案
对于子集的每一个字:
  对于每一个字符的字母:
    让newword是字符和字的级联
    加入newword导致
返回结果
 

在(教学,未优化)的javascript:

 虚词(大小,字母){
  如果(大小== 0)返回[''];
  VAR的结果= [];
  VAR子集=字(尺寸-1,字母);
  对于(VAR I = 0; I&LT; subset.length;我++){
    VAR字=子集[我]
    对于(VAR J = 0; J&LT; alphabet.length; J ++){
      VAR字符=字母[J]。
      VAR newword =字符+字;
      result.push(newword);
    }
  }
  返回结果;
}
话(3,['1','2','3']);
// 111,211,311,121,221,321,131,231,331,112,212,312,122,222,322,132,232,332,113,213,313,123,223,323,133,233,333
 

请注意:有15个字母与一组3个字符超过14百万字。所以计算是相当长的。

编辑::如果你的阵列并不总是具有完全相同的3个字符,那么你必须改变功能的第一线。该阵列已经被赋予作为参数,则该函数弹出它将为字母使用第一个阵列,并给出了其余的递归调用:

 虚词(阵列){
  如果(arrays.length == 0)返回[''];
  VAR的结果= [];
  无功字母= arrays.shift();
  VAR子集=字(阵列);
  对于(VAR I = 0; I&LT; subset.length;我++){
    VAR字=子集[我]
    对于(VAR J = 0; J&LT; alphabet.length; J ++){
      VAR字符=字母[J]。
      VAR newword =字符+字;
      result.push(newword);
    }
  }
  返回结果;
}
字(['A','B','C'],['D','E','F'],['H','我','J'])
// adh,bdh,cdh,aeh,beh,ceh,afh,bfh,cfh,adi,bdi,cdi,aei,bei,cei,afi,bfi,cfi,adj,bdj,cdj,aej,bej,cej,afj,bfj,cfj
 

Im trying to create 1 X 15 dimension unique array from 3 X 15 array. I am not able to achieve this .Please help me on this . Thanks in advance.

The 3 X 15 array was

Array[(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3)]

解决方案

You have a set of 3 characters (1, 2, 3) and you want every possible word of 15 letters from your alphabet of 3 characters. The solution is recursive:

Let size be your word size, given as parameter
Let alphabet be your character set, given as parameter
If size is 0 then
  return collection with empty string
Let result be the collection of possible words, starting empty
Let subset be the solution of the problem for size-1 and alphabet
For every word in subset:
  For every character in alphabet:
    Let newword be the concatenation of character and word
    Add newword to result
return result

In (didactic, unoptimised) javascript:

function words(size, alphabet) {
  if(size == 0) return [''];
  var result = [];
  var subset = words(size-1, alphabet);
  for(var i=0; i<subset.length; i++) {
    var word = subset[i];
    for(var j=0; j<alphabet.length; j++) {
      var character = alphabet[j];
      var newword = character+word;
      result.push(newword);
    }
  }
  return result;
}
words(3,['1','2','3']);
// 111,211,311,121,221,321,131,231,331,112,212,312,122,222,322,132,232,332,113,213,313,123,223,323,133,233,333

note: there are more than 14 millions words of 15 letters with a set of 3 characters. So the computation is quite long.

edit: If your arrays don't always have the same exact 3 characters, then you have to change the first lines of the function. The arrays have to be given as argument, then the function pops the first array which it will use as alphabet and gives the rest to the recursive call:

function words(arrays) {
  if(arrays.length == 0) return [''];
  var result = [];
  var alphabet = arrays.shift();
  var subset = words(arrays);
  for(var i=0; i<subset.length; i++) {
    var word = subset[i];
    for(var j=0; j<alphabet.length; j++) {
      var character = alphabet[j];
      var newword = character+word;
      result.push(newword);
    }
  }
  return result;
}
words([['a','b','c'],['d','e','f'],['h','i','j']])
// adh,bdh,cdh,aeh,beh,ceh,afh,bfh,cfh,adi,bdi,cdi,aei,bei,cei,afi,bfi,cfi,adj,bdj,cdj,aej,bej,cej,afj,bfj,cfj

这篇关于如何找到3×15阵列1×15阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆