Ruby 使用数字洗牌对数组进行排序
我问了一些关于数字洗牌的问题,得到了满意的答案,但是这个练习对我来说还不是很清楚。练习要求: 给定一个具有不同数字的3位或4位数字,返回可由这些数字组成的所有唯一数字的排序数组 示例: 给定:Ruby 使用数字洗牌对数组进行排序,ruby,Ruby,我问了一些关于数字洗牌的问题,得到了满意的答案,但是这个练习对我来说还不是很清楚。练习要求: 给定一个具有不同数字的3位或4位数字,返回可由这些数字组成的所有唯一数字的排序数组 示例: 给定:123 返回:[123、132、213、231、312、321] 练习下面有一个解决方案(请参见解决方案),如下所示: def number_shuffle(number) no_of_combinations = number.to_s.size == 3 ? 6 : 24 digits = nu
123
返回:[123、132、213、231、312、321]
练习下面有一个解决方案(请参见解决方案),如下所示:
def number_shuffle(number)
no_of_combinations = number.to_s.size == 3 ? 6 : 24
digits = number.to_s.split(//)
combinations = []
combinations << digits.shuffle.join.to_i while combinations.uniq.size!=no_of_combinations
combinations.uniq.sort
end
def number_shuffle(数字)
没有任何组合=数量到大小=3?6:24
数字=要拆分的数字(/)
组合=[]
组合
。。。如果number.to_.s.size
等于3位,则组合数应为6,否则为24。我说得对不对
正确,因为有6种方式排列3位数字,24种方式排列4位数字
这是什么意思:combines.uniq.size=没有任何组合
重复之前的部分,同时重复,直到满足此方程,即创建一个随机组合:
digits = [1, 2 ,3]
digits.shuffle.join.to_i #=> 123
digits.shuffle.join.to_i #=> 132
digits.shuffle.join.to_i #=> 321
digits.shuffle.join.to_i #=> 123
。。。并将此组合添加到组合
数组中,直到该数组包含无组合
唯一元素
当然,这远非理想,因为可以一次又一次地创建相同的组合。您可以在一行中完成:
def number_shuffle(i)
i.to_s.chars.permutation.map(&:join).map(&:to_i)
end
输出:
number_shuffle(123)
# => [123, 132, 213, 231, 312, 321]
number_shuffle(1234)
# => [1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321]
这个问题的解决方案是错误的和低效的。它生成随机排列,直到找到唯一组合的正确计数。这就像通过替换随机值来解方程:
# x - 5 should be 0. Let's find x!
x = rand() unless x - 5 == 0
不要那样做。关于第一个问题:
假设具有不同数字的数字的数字排列数为n代码>(1*2*..*n
)
如果数字有3位,则排列的数字为3!=1*2*3=6
如果数字有4位,则排列的数字为4!=1*2*3*4=24
所以在这种情况下,你是对的。请注意,如果该数字中有重复的数字,则这将不起作用
关于第二个问题:
combines.uniq.size=无组合
是一种布尔测试,返回true
或false
。结合while
语句:some_code while boolean_test
,这意味着在boolean_test
为真时执行代码some_code
就你而言:
combinations << digits.shuffle.join.to_i while
combinations.uniq.size!=no_of_combinations
组合Q1
你不需要被告知。把它弄明白。假设p(n)
是可以排列(“排列”)具有n
不同数字的数字的方式的数量。首先,假设n=1
。那么显然,
p(1) = 1
现在假设n=2
。第一个数字可以在第二个数字之前或之后,因此:
p(2) = 2*p(1) = 2
现在让n
为大于2
的任意整数。对于最后的n-1
位的每个排列,第一位数字可以插入到n
的任何位置。例如,假设数字为1234
。最后三位数字的一种排列方式是423
。第一个数字1
可以插入4
位置中的任何一个:1423412342134231
因此:
p(n) = n*p(n-1)
看来:
p(n) = n!
我们可以通过归纳很容易地证明这是正确的:
p(1) = 1
p(2) = 2*1 = 2!
p(n) = n*p(n-1)
= n*(n-1)!
= n!
因此:
既然你知道这个公式,并且知道它为什么是真的,你就不会忘记它
Q2
让我们重新编写方法,添加一些put
:
def number_shuffle(number)
no_of_combinations = number.to_s.size == 3 ? 6 : 24
puts "no_of_combinations = #{no_of_combinations}"
digits = number.to_s.split(//)
puts "digits = #{digits}\n\n"
combinations = []
while true
a = digits.shuffle.join.to_i
puts "digits.shuffle.join.to_i = #{a}"
combinations << a
puts "combinations = #{combinations}"
b = combinations.uniq
puts " combinations.uniq = #{b}"
c = b.size
puts " combinations.uniq.size = #{c}"
puts " combinations.uniq.size==no_of_combos = #{c==no_of_combinations}"
break if c==no_of_combinations
end
combinations.uniq.sort
end
考虑到这个练习和方法的名称,Ruby Monk显然不想要最好的解决方案。但要向学员展示Array#shuffle
是如何工作的。所以本质上你是对的,但这与我无关。@astreal yep,你对函数给出了非常精确的解释。我只是想警告作者,他提供的解决方案与问题无关Shuffle
可以用于彩票游戏,但不能用于组合数学。“这远不理想,因为可以一次又一次地创建相同的组合。”最肯定的是,OP给出的解决方案似乎根本不是一个解决方案。这是关于本教程的一个陈述:-/
def number_shuffle(number)
no_of_combinations = number.to_s.size == 3 ? 6 : 24
puts "no_of_combinations = #{no_of_combinations}"
digits = number.to_s.split(//)
puts "digits = #{digits}\n\n"
combinations = []
while true
a = digits.shuffle.join.to_i
puts "digits.shuffle.join.to_i = #{a}"
combinations << a
puts "combinations = #{combinations}"
b = combinations.uniq
puts " combinations.uniq = #{b}"
c = b.size
puts " combinations.uniq.size = #{c}"
puts " combinations.uniq.size==no_of_combos = #{c==no_of_combinations}"
break if c==no_of_combinations
end
combinations.uniq.sort
end
number_shuffle(123)
no_of_combinations = 6
digits = ["1", "2", "3"]
digits.shuffle.join.to_i = 123
combinations = [123]
combinations.uniq = [123]
combinations.uniq.size = 1
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 321
combinations = [123, 321]
combinations.uniq = [123, 321]
combinations.uniq.size = 2
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 123
combinations = [123, 321, 123]
combinations.uniq = [123, 321]
combinations.uniq.size = 2
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 312
combinations = [123, 321, 123, 312]
combinations.uniq = [123, 321, 312]
combinations.uniq.size = 3
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 321
combinations = [123, 321, 123, 312, 321]
combinations.uniq = [123, 321, 312]
combinations.uniq.size = 3
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 132
combinations = [123, 321, 123, 312, 321, 132]
combinations.uniq = [123, 321, 312, 132]
combinations.uniq.size = 4
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 321
combinations = [123, 321, 123, 312, 321, 132, 321]
combinations.uniq = [123, 321, 312, 132]
combinations.uniq.size = 4
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 213
combinations = [123, 321, 123, 312, 321, 132, 321, 213]
combinations.uniq = [123, 321, 312, 132, 213]
combinations.uniq.size = 5
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 213
combinations = [123, 321, 123, 312, 321, 132, 321, 213, 213]
combinations.uniq = [123, 321, 312, 132, 213]
combinations.uniq.size = 5
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 123
combinations = [123, 321, 123, 312, 321, 132, 321, 213, 213, 123]
combinations.uniq = [123, 321, 312, 132, 213]
combinations.uniq.size = 5
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 231
combinations = [123, 321, 123, 312, 321, 132, 321, 213, 213, 123, 231]
combinations.uniq = [123, 321, 312, 132, 213, 231]
combinations.uniq.size = 6
combinations.uniq.size==no_of_combinations = true
#=> [123, 132, 213, 231, 312, 321]