假设我的税金是:
A boy already has {a banana|an orange|an apple}, but he still want to eat more {ice cream|yogurt|cookie}.我如何能够编写javascript或php来计算所有可能组合的spintax的数量。
这个概念是计算在每对卷曲对中被管道分隔的相位,并将它们相乘,但我不知道如何计算这些相位,你能给我建议一下吗?
我试图寻找解决办法,但什么也找不到。
发布于 2019-08-28 06:10:14
var a='A boy already has {a banana|an orange|an apple}, but he still wants to eat more {ice cream|yogurt|cookies} before going to {school|college}.';
var h=a.match(/{[^}]+}/g).map(m=>m.split('|').length).reduce((p,v)=>p*v);
console.log('There are '+h+' possible combinations.')<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
脚本首先查找所有{}部分,并将它们收集到一个数组中。然后用map()扫描到使用|的每个元素。然后返回结果子数组中元素的计数,最后使用reduce()将所有计数组合到产品中。
https://stackoverflow.com/questions/57684196
复制相似问题