i have a file called terain.dat which contains this matrix:
10
1 1 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 12 12 12
1 2 3 4 5 6 7 12 12 12
1 2 3 4 5 6 7 12 12 12
i want to read in the file and use the first number on the first line as the size of the matrix (which is 10 X 10 in this case). And then fill the 10 X 10 matrix with the numbers below.
this is what i have so far:
class Terrain
def initialize file_name
#input = IO.readlines(file_name) #read in file # reads in the file with the terrain detials
#matrix_size = #input[0].to_i # changes the first index to an int (so i can make a10X10 matrix)
#land = Matrix.[#matrix_size, #matrix_size] # will this make a 10 X 10 matrix??
end
end
i was wondering if this will make a 10X10 matrix and how do i fill it??
I'd write:
terrain = open("terrain.data") do |file|
size = file.lines.first.to_i
rows = file.lines.first(size).map { |line| line.split.map(&:to_i) }
Matrix.rows(rows)
end
actually no. The Matrix.[] is used for setting the values of a row.
So Matrix.[10,10] would create a Matrix with 2 rows, and in each column a 10.
What you are searching for is Matrix.build(row_size, column_size) where column_size defaults to row_size. This gives you an enumerator which you can use to set the values. (or you just pass a block to Matrix.build
I'd suggest a different approach:
arr = []
#input.each_index do |index|
arr[index] = #input[index].split ' '
end
#land = Matrix.build(10,10) do |row, column|
arr[row][column].to_i
end
You could skip over the first line, read the other lines, chomp them to remove the new lines and then split on white space. This will give you an array of arrays, which you can feed to Matrix.rows.
No need to declare the size. Try the following:
class Terrain
attr_accessor :m
def initialize file_name
data = IO.readlines(file_name)
data.each_line do |l|
data << l.split.map {|e| e.to_i}
end
#m = Matrix[*#data]
end
end
Or, even better:
class Terrain
attr_accessor :m
def initialize file_name
File.open(file_name).each do |l|
data << l.split.map {|e| e.to_i}
end
#m = Matrix[*#data]
end
end
No need for the size:
class Terrain
def initialize(file_name)
File.open(file_name) do |f|
#m = Matrix[*f.lines.map { |l| l.split.map(&:to_i) }]
end
end
end
Related
input integer 6
output :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Tried with loops but failed to get the output.
def pyramid(i)
(1..i).each do |n|
1.upto(n) do |x|
print "#{x} " # Mind double quotes
end
puts
end
end
pyramid(6)
Read more about Ruby I/O
Read more about loops in Ruby
I am trying to print all numbers between 1 and 50, using the following code:
[1..50].each{|n| puts n}
but the console print
[1..50]
I want to print something like this
1
2
3
4
...
50
Try the following code:
(1..50).each { |n| puts n }
The problem is that you're using [] delimiter instead of () one.
You can use [1..10] with a minor tweak:
[*1..10].each{ |i| p i }
outputs:
1
2
3
4
5
6
7
8
9
10
The * (AKA "splat") "explodes" the range into its components, which are then used to populate the array. It's similar to writing (1..10).to_a.
You can also do:
puts [*1..10]
to print the same thing.
So, try:
[*1..10].join(' ') # => "1 2 3 4 5 6 7 8 9 10"
or:
[*1..10] * ' ' # => "1 2 3 4 5 6 7 8 9 10"
To get the output you want.
The error here is that you are creating an Array object with a range as its only element.
> [1..10].size
=> 1
If you want to call methods like each on a range, you have to wrap the range in parentheses to avoid the method being called on the range's last element rather than on the range itself.
=> (1..10).each { |i| print i }
12345678910
Other ways to achieve the same:
(1..50).each { |n| print n }
1.up_to(50) { |n| print n }
50.times { |n| print n }
You can cast your range (in parentheses) to an array ([1 2 3 4 5 6... 48 49 50]) and join each item (e.g. with ' ' if you want all items in one line).
puts (1..50).to_a.join(' ')
# => 1 2 3 4 5 6 7 ... 48 49 50
I recently came upon the scary idea that Integer.count loops in Ruby start from 0 and go to n-1 while playing with the Facebook Engineering puzzlers. I did the dirty fix of adding one to the block variable in the beginning so that it would start at one instead.
Is there a prettier way?
Example:
10.times do |n|
n += 1
puts n
end #=> 012345789
Ruby supports a number of ways of counting and looping:
1.upto(10) do |i|
puts i
end
>> 1.upto(10) do |i|
> puts i
| end #=> 1
1
2
3
4
5
6
7
8
9
10
There's also step instead of upto which allows you to increment by a step value:
>> 1.step(10,2) { |i| puts i } #=> 1
1
3
5
7
9
You could use a range:
(1..10).each { |i| puts i }
Ranges give you full control over the starting and ending indexes (as long as you want to go from a lower value to a higher value).
Try
(1..10).each do |i|
# ... i goes from 1 to 10
end
instead. It is also easier to read when the value of i matters.
Old, but this might be something somebody's lookin for..
5.times.with_index(100){|i, idx| p i, idx};nil
#=>
0
100
1
101
2
102
3
103
4
104
There is of course the while-loop:
i = 1
while i<=10 do
print "#{i} "
i += 1
end
# Outputs: 1 2 3 4 5 6 7 8 9 10
I want to parse a text file, where
I get numbers that are between parenthesis like
this:
1 2 3 (4 - 7) 8 9
1 3 8 (7 - 8) 2 1
1 2 (8 - 10) 3 2
should return an array for each:
array1:
4
7
8
array2:
7
8
10
I am thinking of using split for each line, like line.split("("), but that doesn't quite doing the trick.. I was wondering if there is something more sophisticated for the job.
Any help appreciated,
Ted
data = <<EOS
1 2 3 (4 - 7) 8 9
1 3 8 (7 - 8) 2 1
1 2 (8 - 10) 3 2
EOS
lines = data.split("\n")
def get_inner(lines)
lines.map { |line| line.partition("(")[2].partition(")")[0].split(" - ")}
end
a1, a2 = *[get_inner(lines).map {|a| a.first },get_inner(lines).map {|a| a.last }]
puts a1
puts a2
# =>
4
7
8
7
8
10
I would look into using things like Substring / IndexOf as well as split.
You could also try a regular expression to find numbers seperated by spaces in between the ( ) but regular expressions can be a bit of a pain.
Hmm just found this
http://www.rubular.com/ I got the expression I needed
((\d+)-(\d+))
I have array say "a"
a =
1 4 5
6 7 2
if i use function
b=sort(a)
gives ans
b =
1 4 2
6 7 5
but i want ans like
b =
5 1 4
2 6 7
mean 2nd row should be sorted but elements of ist row should remain unchanged and should be correspondent to row 2nd.
sortrows(a',2)'
Pulling this apart:
a = 1 4 5
6 7 2
a' = 1 6
4 7
5 2
sortrows(a',2) = 5 2
1 6
4 7
sortrows(a',2)' = 5 1 4
2 6 7
The key here is sortrows sorts by a specified row, all the others follow its order.
You can use the SORT function on just the second row, then use the index output to sort the whole array:
[junk,sortIndex] = sort(a(2,:));
b = a(:,sortIndex);
How about
a = [1 4 5; 6 7 2]
a =
1 4 5
6 7 2
>> [s,idx] = sort(a(2,:))
s =
2 6 7
idx =
3 1 2
>> b = a(:,idx)
b =
5 1 4
2 6 7
in other words, you use the second argument of sort to get the sort order you want, and then you apply it to the whole thing.