具有嵌套数组的Ruby相同的字符的所有索引 [英] All indexes having the same character in nested array Ruby

查看:83
本文介绍了具有嵌套数组的Ruby相同的字符的所有索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看如果一个指数,指数说3,都具有相同的字符。例如:

  nested_array = [[47,44,71,'X',88],
                  [22,69,75,'X',73],
                  [83,85,97,'X',57],
                  [25,31,96,'X',51],
                  [75,70,54,'X',83]

我们可以看到,指数 3 拥有所有 X'X 于它,而不是数字。我希望能够以验证是否所有索引有一个 X ,如果是的话,程序返回true。如果所有,但有1 X的它会返回false。

我现在已经实现了这个条件语句,因为它通过每个迭代指数寻找一个值的大小等于1。这似乎这样的伎俩。

 如果array.map {|零|零[0]}。uniq.size == 1
  返回true
ELSIF array.map {| ONE | [1]}。uniq.size == 1
  返回true
ELSIF array.map {| 2 |两次[2]}。uniq.size == 1
  返回true
ELSIF array.map {|三| 3 [3]}。uniq.size == 1
  返回true
ELSIF array.map {|福| 4 [4]}。uniq.size == 1
  返回true
其他
  返回false
结束


解决方案

  nested_array.empty? || nested_array.map {|一行|行[3]}。uniq.one?

为空数组的特殊情况是必要的,因为他们平凡履行为每个元素指定的任何条件,但唯一性测试将不会把它捡起来。随意丢弃它,如果你不会,其实有一个空数组,或者如果数组为空测试应该失败。

编辑:好像我误解了这个问题 - 试试这个来替代你的多,如果:

  nested_array.transpose.any? {|栏| column.uniq.one? }

当你翻转的行和列,是否有任何(曾经是列)仅具有一个独特的元件的行的?

(如果你想真正 [] ,再次,preFIX与 nested_array.empty?|| ... )。

编辑:感谢spickermann,更换 .size == 1 。一是?。它确实读好。

I'm trying to see if an index, say index 3, all has the same character. For example:

nested_array =   [[47, 44, 71, 'x', 88],
                  [22, 69, 75, 'x', 73],
                  [83, 85, 97, 'x', 57],
                  [25, 31, 96, 'x', 51],
                  [75, 70, 54, 'x', 83]]

As we can see, index 3 has all x'x in it instead of numbers. I want to be able to verify if ALL of the indexes have an x, if so, program returns true. If all but 1 have x's it would return false.

I have now implemented this conditional statement as it iterates through each index looking for a value the size equal to 1. This seemed to do the trick.

if array.map {|zero| zero[0]}.uniq.size == 1
  return true
elsif array.map {|one| one[1]}.uniq.size == 1
  return true
elsif array.map {|two| two[2]}.uniq.size == 1
  return true
elsif array.map {|three| three[3]}.uniq.size == 1
  return true
elsif array.map {|four| four[4]}.uniq.size == 1
  return true
else
  return false
end

解决方案

nested_array.empty? || nested_array.map {|row| row[3]}.uniq.one?

The special condition for empty arrays is necessary because they trivially fulfill any condition that is specified for each element, but the uniqueness test would not pick it up. Feel free to drop it if you will not in fact have an empty array, or the test should fail if the array is empty.

EDIT: Seems I misunderstood the question - try this as an alternative to your multi-if:

nested_array.transpose.any? { |column| column.uniq.one? }

"When you flip rows and columns, does any of the rows (that used to be columns) have only one unique element?"

(if you want true for [], again, prefix with nested_array.empty? || ...).

EDIT: Thanks to spickermann, replaced .size == 1 with .one?. It really does read better.

这篇关于具有嵌套数组的Ruby相同的字符的所有索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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