Created
August 6, 2020 01:44
-
-
Save coreyjs/6d4ea46242f9f93a156b97972efc1b2e to your computer and use it in GitHub Desktop.
Left Rotate an Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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