Skip to content

Instantly share code, notes, and snippets.

@coreyjs
Created August 6, 2020 01:44
Show Gist options
  • Select an option

  • Save coreyjs/6d4ea46242f9f93a156b97972efc1b2e to your computer and use it in GitHub Desktop.

Select an option

Save coreyjs/6d4ea46242f9f93a156b97972efc1b2e to your computer and use it in GitHub Desktop.
Left Rotate an Array
#!/bin/ruby
require 'json'
require 'stringio'
# Complete the rotLeft function below.
def rotLeft(a, d)
out = Array.new(a.length)
(0..a.length-1).each do |i|
new_location = (i + (a.length - d)) % a.length
out[new_location] = a[i]
end
out
end
fptr = File.open(ENV['OUTPUT_PATH'], 'w')
nd = gets.rstrip.split
n = nd[0].to_i
d = nd[1].to_i
a = gets.rstrip.split(' ').map(&:to_i)
result = rotLeft a, d
fptr.write result.join " "
fptr.write "\n"
fptr.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment