How to sensibly split an array around an object
来源:互联网 发布:sql数据类型转换 编辑:程序博客网 时间:2024/05/18 00:58
I have an array of uncertain length, let's say [1,2,3,4,5]
. I want to subtract1
from everything before 3
, and add 1
to everything after3
, making the example [0,1,3,5,6]
. If there is no 3
, add 1
to everything: [1,2,4,5]
=> [2,3,5,6]
. What is the most graceful way of doing this?
a = [1, 2, 3, 4, 5]n = a.index(3) || -1a.map.with_index{|e, i| e + (i <=> n)}# => [0, 1, 3, 5, 6]
0 0
- How to sensibly split an array around an object
- How to remove Duplicates from an Array
- How to define an array in c#.
- How to initialize an array in C
- How to inherit an Object in JavaScript
- How to clone an object in JavaScript
- How to store an Object into an integer?
- How to copy values of an array into another array
- How to Split an Image into Chunks - Java ImageIO
- How to sort an array by its index? (SortIndex)
- How to call different methods as given in an array
- How to group anagrams in a string into an array
- How to sum values in an array with different hash
- How to format strings in an array in ruby?
- How to judge whether an array is empty?
- How to get the published properties of an persistent object
- How to determine the size of an object of class?
- How to copy an object in objective c
- acdreamoj1112 sg函数
- 二叉树的应用(1)--二叉树排序树基本操作
- exists 与 in 的区别
- Properties类读写.properties配置文件
- Mac OSX & iOS 操作系统学习笔记01——OSX进化史
- How to sensibly split an array around an object
- 基于RDP协议的开源rdesktop简介
- 连载:面向对象葵花宝典:思想、技巧与实践(34) - DIP原则
- SQL查询案例:多行转换为一行
- [LeetCode] Linked List Cycle II
- CVPR14与图像视频检索相关的论文
- Google发布内涵图 暗示Android 5.0系统将至
- 图像检索中为什么仍用BOW和LSH
- 使用mac地址ssh