brettstahlman
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
#! /usr/bin/ruby
nums = ARGV.map { |v| v.to_i }
gsize = nums.shift.to_i
res = nums.inject([]) do |ary, val|
ary.unshift val
if ary.size % gsize == 0
ary = ary.rotate gsize
end
if ary.size == nums.size
rot_by = ary.size % gsize
# Perform final rotation
ary = ary.rotate rot_by
end
ary
end
puts "result: #{res}"
Comment hidden because of low score. Click to expand.
0
of 0 vote
# Cmdline Input:
# prog val1 val2 ... valN
nums = ARGV.map { |v| v.to_i }
n = nums.size
res = []
idxs = (0...n).to_a
nums.each do |num|
i = (rand * idxs.size).floor
res[idxs[i]] = num
idxs.delete_at i
end
puts "Shuffled array: #{res}"
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
- brettstahlman May 14, 2015