Multiply all even indexed integers by two
来源:互联网 发布:java中的类型转换 编辑:程序博客网 时间:2024/06/06 00:45
You can also multiply alternate numbers by 2
a = 4408041234567901
1.arr = a.to_s.chars.map.with_index {|n,i| i.even? ? n.to_i * 2 : n.to_i }# => [8, 4, 0, 8, 0, 4, 2, 2, 6, 4, 10, 6, 14, 9, 0, 1]
Improving a little bit, you can use a Hash
to find the number to be multiplied.
2.h = {true => 2, false => 1} a.to_s.each_char.map.with_index {|n,i| n.to_i * h[i.even?]}
3.coef = [2, 1].cycle
a.to_s.each_char.map { |v| v.to_i * coef.next }
0 0
- Multiply all even indexed integers by two
- [LeetCode] Add Two Numbers、Divide Two Integers、Multiply Strings、Add Binary、Plus One
- Substring with Concatenation of All Words & Median of Two Sorted Arrays & Divide Two Integers
- WSN Journal indexed by SC
- 'union all' two select sentences contain 'order by'
- WSN Journal indexed by SCI(转)
- Represent an integer by the sum of at least two consecutive integers
- Two big numbers to multiply
- LeetCode: Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode: Divide Two Integers
- [LeetCode] Divide Two Integers
- Divide Two Integers
- Divide Two Integers
- LeetCode28:Divide Two Integers
- [Leetcode] Divide Two Integers
- LeetCode Divide Two Integers
- Divide Two Integers
- 过零检测电路原理与作用 可控整流
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 360自动切换到Googel内核
- 初学 Java Web 开发,请远离各种框架,从 Servlet 开发
- PON产品发展历程
- Multiply all even indexed integers by two
- StringUtils类和collectionUtils的用法
- Tor隐身大法 —— 用Tor来帮助我们进行渗透测试
- 深入理解 Java中的 流 (Stream)
- 整数划分
- jqueryMobile的介绍
- c++的历史-异常
- 类图的几种关系总结
- python操作mysql数据库如何解决中文乱码的问题