>rotate matrix outer elements using JUST 2 loops
it's fucking impossible
Rotate matrix outer elements using JUST 2 loops
kill you are self
>dude in CS you learn more than math majors!
>can't grasp basic linear algebra
kek
who are you quoting?
>matrixlet
Kill yourself Canadian scum.
for row in rows:
for column in columns:
if element is outer then copy to a new matrix at the new rotated location
else if element is not then copy to a new matrix at the same location
fin
>if element is outer
Good luck implementing this.
>if element is outer
kek
If the element is in the first or last row and/or in the first or last column
>t. never worked with matrices in his life but decides to opine
>two loops and over 9000 ifs
>t. not happy that other people can answer the question while you cant
I was saying that implementing that is easy, i was not saying that the original solution was efficient
Can do it in one loop.
Let's see the code pajeet.
This. You just need a "next" method to handle the maneuvering.
let pair = (1,1)
let prev = matrix[pair]
while(next(pair) != (1,1)){
let temp = matrix[pair]
matrix[pair] = prev
prev = temp
pair = next(pair)
}
matrix[1,1] = prev
method next(pair){
//Left as an exercise left to the reader
//Hint: Go right, then down, then left, then up.
}
...
>Sup Forums
>Go right, then down, then left, then up.
Klossy?
>Go right, then down, then left, then up
>t. the guy who hasn't worked with matrices once in his life
>I am going to insult you despite not knowing how to approach the problem myself
awesome
I bet you guys can't even solve it with 4 loops
One loop.
def rotate(A):
n,m = len(A),len(A[0])
if 1 in (n,m): return
r,c,dr,dc = 0,0,0,1
v = A[r][c]
for i in range(2*(n+m-2)):
if dc and i+1 == m: dr,dc = 1,0
if dr and i+1 == m+n-1: dr,dc = 0,-1
if dc and i+1 == 2*m+n-2: dr,dc = -1,0
r,c =r+dr,c+dc
A[r][c],v = v,A[r][c]
public class RotateOuter {
public static void main(String[] args) {
int[][] matrix = {{1,2,3,4},{12,0,0,5},{11,0,0,6},{10,9,8,7},};
int c1 = matrix[0][0], c2 = matrix[matrix.length-1][matrix[0].length-1];
for(int i = 1;i
Is Intel putting chewing gum under their chips now???
int size, tempA, tempB, tempC, tempD;
size = matrix.size() - 1;
tempA = tempB = tempC = tempD = 0;
for(int i = 0; i
Oops didn't need those checks.
def rotate(A):
n,m = len(A),len(A[0])
if 1 in (n,m): return
d = {m-1:(1,0), m+n-2:(0,-1), 2*m+n-3: (-1,0)}
r,c,dr,dc = 0,0,0,1
v = A[r][c]
for i in range(2*(n+m-2)):
dr,dc = d[i] if i in d else (dr,dc)
r,c =r+dr,c+dc
A[r][c],v = v,A[r][c]
Doesn't work with non-square matrices.
>size
>lenght
sorry those are 3 loops
we're reaching new levels of grammar
Yeah I just realized that it might not be square, easily adaptable to two loops though. or maybe inefficiently still to one.
Normally someone would pass in the dimensions...
Depends on the implementation retard
All you're doing is basically a linked-list traversal but with 1 additional dimension (hence the second loop).
Haven't you ever done array maps before?
len() is O(1) in Cpython
Judging by number of loops is stupid since you just convert any number of loops into one big loop with some added control logic.
Better metric is your rotation should be done in time O(n+m) not O(nm).
>lets write the loop and leave the actual problem as an exercise
Nice solution, user.
Only works for square matricies because I'm a brainlet, also I used 's solution
def rotate(mat):
newmat = copy.deepcopy(mat);
for i in range(len(mat)):
for j in range(len(mat[i])):
if i == 0:
newmat[j][len(mat) - 1] = mat[i][j]
elif i == len(mat) - 1:
newmat[j][0] = mat[i][j]
elif j == 0:
newmat[0][len(mat[i]) - 1 - i] = mat[i][j]
elif j == len(mat[i]) - 1:
newmat[len(mat) - 1][len(mat[i]) - 1 - i] = mat[i][j]
return newmat
>unnecessarily making the algorithm n^2 because for some reason you're unable to recognize the boundaries that define "outer" before runtime
breh
Worked on my shitty solution a bit more.
public class RotateOuter {
public static void main(String[] args) {
int[][] matrix = {{1,2,3,4},{12,0,0,5},{11,0,0,6},{10,9,8,7},};
int c1 = matrix[0][0], c2 = matrix[matrix.length-1][matrix[0].length-1];
int c3 = matrix[0][matrix[0].length-1], c4 = matrix[matrix.length-1][0];
for(int i = 1;i
this pajeet, damn
Even if I only looped once I would still have to copy an entire row to a column in newmat, would have to be a second loop using that method
You're a brainlet.
In what application you need to do such operation?
#include
using namespace std;
#define x 5 //number of rows
#define y 6 //numer of collumns
void fillR(int a[][y]){
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
a[i][j]=rand()%10;
}
}
}
void print(int a[][y]){
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
cout
give your constants proper FULLY CAPITALIZED longer than 1 letter names
Haskell, zero """loops"""
rotate :: [[Integer]] -> [[Integer]]
rotate (x:xs) = let tl = head x
tr = last . head $ xs
bl = last . last . init $ xs
br = last . last $ xs
mid y = transpose $ apply_chunks (rshift tl) id (shift br) $ transpose y
in apply_chunks (shift tr) mid (rshift bl) (x:xs)
apply_chunks :: ([Integer] -> [Integer]) -> ([[Integer]] -> [[Integer]]) -> ([Integer] -> [Integer]) -> [[Integer]] -> [[Integer]]
apply_chunks top mid bot (x:xs) = (top x):((mid (init xs)) ++ [(bot (last xs))])
transpose :: [[Integer]] -> [[Integer]]
transpose ([]:_) = []
transpose x = (map head x) : transpose (map tail x)
shift :: Integer -> [Integer] -> [Integer]
shift x (_:xs) = xs ++ [x]
rshift :: Integer -> [Integer] -> [Integer]
rshift x l = reverse . (shift x) . reverse $ l
displ :: [Integer] -> String
displ x = unwords $ map show x
main :: IO()
main = mapM_ putStrLn $ map displ $ rotate [[1..4], [5..8], [9..12], [13..16]]
Make me
Needs to be bl = head . last . init $ xs
You could do that with just one loop.
When will you faggots realize that meme arrows aren't exclusively for quoting?
fuck it, just stop using loops and hard code everything why don't cha
>9000 ifs
Its four conditionals, which run in constant time.
3
We are maintaining normal levels of new