App Academy

Nauseating_Numbers solution, missing "squaragonal?" method

Looks like in software engineering foundation, the solution is missing the squaragonal method

Here’s what I did, was very curious how you can make this code efficient or shorter than what it is (since there wasn’t a solution to compare it to).:

def squaragonal?(array) # this will give you the diagonal pairs
diagonal_first(array) || diagonal_last(array)
end

def diagonal_first(array)
i = 0
first = array[0][0]
while i < array.length
if first != array[i][i]
return false
break
else
first = array[i][i]
i += 1
end
end
true
end
def diagonal_last(array)
i = 0
x = array.length - 1
last = array[i][x]
while i < array.length
if last != array[i][x]
return false
break
else
last = array[i][x]
i += 1
x -= 1
end
end
true
end

results in: ```
p squaragonal?([
[:x, :y, :o],
[:x, :x, :x],
[:o, :o, :x],
]) # true

p squaragonal?([
[:x, :y, :o],
[:x, :o, :x],
[:o, :o, :x],
]) # true

p squaragonal?([
[1, 2, 2, 7],
[1, 1, 6, 7],
[0, 5, 1, 7],
[4, 2, 9, 1],
]) # true

p squaragonal?([
[1, 2, 2, 5],
[1, 6, 5, 0],
[0, 2, 2, 7],
[5, 2, 9, 7],
]) # false

This is my solution,which is not good :grin: :

def squarocol?(array)
    height = array.length
    width = height
    (0...height).each do |i|
        count_c = 1
        count_r = 1
        (0...width - 1).each do |j|
            count_c += 1 if array[i][j] == array[i][j + 1]
            count_r += 1 if array[j][i] == array[j + 1][i]
        end
        return true if count_c == height || count_r == width
    end
    false
end

I see the solution is not missing. I post it in here, you can try it.

def squarocol?(grid)
    return true if grid.any? { |row| row.uniq.length == 1 }
    return true if grid.transpose.any? { |col| col.uniq.length == 1 }
    false
end

I also suggest you can use markdown to show your code. Learn some simply format.

For example, to syntax highlight Ruby code:

Ha see I made the same mistake, I thought that maybe they mixed the two up, but you gotta go back to the problems and see what it asks it for squaragonal

the prompt was this:
Write a method squaragonal? that accepts 2-dimensional array as an argument. The method should return a boolean indicating whether or not the array contains all of the same element across either of its diagonals. You may assume that the 2-dimensional array has “square” dimensions, meaning it’s height is the same as it’s width.

p squaragonal?([
[:x, :y, :o],
[:x, :x, :x],
[:o, :o, :x],
]) # true

p squaragonal?([
[:x, :y, :o],
[:x, :o, :x],
[:o, :o, :x],
]) # true

p squaragonal?([
[1, 2, 2, 7],
[1, 1, 6, 7],
[0, 5, 1, 7],
[4, 2, 9, 1],
]) # true

p squaragonal?([
[1, 2, 2, 5],
[1, 6, 5, 0],
[0, 2, 2, 7],
[5, 2, 9, 7],
]) # false

I understand squarocol method that they did, the way they did it was beautiful and effiecient, the method it was missing was squaragonal

you gotta remember the squarocol has to give you an error when the array has an diagonal pair like such

p squarocol?([
    [:o, :x , :o],
    [:x, :o , :x],
    [:o, :x , :o],
]) # false

that’s why my code was:

def squaragonal?(array) # this will give you the diagonal pairs 
    diagonal_first(array) || diagonal_last(array)
end

def diagonal_first(array)
    i = 0 
        first = array[0][0]
        while i < array.length
            if first != array[i][i]
                return false 
                break 
            else 
                first = array[i][i]
                i += 1
        end
    end
    true
end
def diagonal_last(array)
    i = 0 
    x = array.length - 1
    last = array[i][x]
    while i < array.length 
        if last != array[i][x]
            return false
            break 
        else 
            last = array[i][x]
            i += 1 
            x -= 1
        end
    end
    true
end

and ha thanks for clearing up how to make your code or problem better with (```), makes questions and answers look allot more neater.

1 Like

Ha, you are right! I make a mistake I just see this question. I will do it later. Thank you!

This is my code I learn from the solution of squarocol?(grid): the method: Array#uniq

def squaragonal?(grid)
    main_diagonal, anti_diagonal = [], []
    (0...grid.length).each do |i|
        main_diagonal << grid[i][i]
        anti_diagonal << grid[i][-i - 1]
    end
    main_diagonal.uniq.length == 1 || anti_diagonal.uniq.length == 1
end
2 Likes

that looks amazing and allot shorter!!! thanks man

can you explain why you did [-i - 1]
in

 anti_diagonal << grid[i][-i - 1]

You are welcome! Let me try to explain it, For example: image

I just simply the grid as a 1-d array in every row.
It like that:

  1. The last element in the row is: grid[-1] = 1
  2. The second last element in the row is: grid[-2] = 2
  3. The i-th last element in the row is: grid[-i]

And then i begins from 0, so we need to minus 1, that’s why [-i - 1].

1 Like

Ha thanks good look bro!!! That makes perfect sense

You are welcome! Let’s continue to work hard. :pray: