Whats the best way to split an array in ruby into multiple smaller arrays of random size
来源:互联网 发布:tcl人工智能电视 编辑:程序博客网 时间:2024/04/29 17:28
I have multiple arrays in ruby of variable length from 1 to 40 :
@items is a typical array which could be anywhere from 1 to 40 in length. eg
@items = [1, 2, 3, 4, 5, 6]
I want to randomly split the array into smaller arrays of lengths either 1, 2 or 3 to give a result of (for example)
@items = [[1, 2],[3],[4,5,6]]
or
@items = [[1],[2, 3],[4],[5,6]]
etc
items, @items = @items.dup, []@items.push(items.shift(rand(1..3))) until items.empty?
0 0
- Whats the best way to split an array in ruby into multiple smaller arrays of random size
- Is there any best way to reduce the size of ibdata in mysql.?
- Best Way to Loop Through an Array in JavaScript
- php Split an Array into N Arrays
- Converting a array to an array of ranges in ruby
- Ruby Arrays: keep repeated values only from the end of an array of arrays
- Ruby CSV - An example of how to split CSV row data into fields
- Elegant way of creating an array of arrays?
- Ruby: Sorting an array of strings, in alphabetical order, that includes some arrays of strings
- How to sort an array of hashes into hashes with multiple values for a key?
- Ruby converting array of hashes to array of arrays
- Sorting an array of hash in Ruby
- ADF: The best way to indicate for records in a table the existence of details
- How to combine the value of multiple hashes within an array by the same key
- Size of an array
- Best way to sort an NSArray of NSDictionary's?
- How to find number of a specific integer in an array of Fixnums ruby
- Matching Portions of A String to Elements of an Array in Ruby
- lwip-内存管理函数说明
- linux内核中send与recv函数详解
- 自然语言分析之命名实体识别_Stanford Named Entity Recognizer (NER)简单实例
- java 集成手机网页支付流程
- linux watchdog 分析
- Whats the best way to split an array in ruby into multiple smaller arrays of random size
- CMS内容管理系统
- Redis-benchmark使用总结
- 小白入门笔记——科密XC-51A消费机常见问题整理
- Centos5.4下部署最新版MooseFS(1.6.13)
- java软件程序智能安装
- mysql 远程访问 错误2003 解决办法
- Struts2方法调用的三种方式
- vs2012安装cocos2d-x环境搭建中出现的与stl版本不兼容问题