>rotate matrix outer elements using JUST 2 loops
it's fucking impossible
kill you are self
>dude in CS you learn more than math majors!
>can't grasp basic linear algebra
kek
>>61524111
who are you quoting?
>>61524091
>matrixlet
>>61524091
Kill yourself Canadian scum.
>>61524091
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
>>61524255
>if element is outer
Good luck implementing this.
>>61524255
>if element is outer
kek
>>61524299
If the element is in the first or last row and/or in the first or last column
>>61524255
>t. never worked with matrices in his life but decides to opine
>>61524255
>>61524348
>two loops and over 9000 ifs
>>61524349
>t. not happy that other people can answer the question while you cant
>>61524402
I was saying that implementing that is easy, i was not saying that the original solution was efficient
Can do it in one loop.
>>61524430
Let's see the code pajeet.
>>61524452
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.
}
>>61524498
>>61524498
>/g/
>>61524498
>Go right, then down, then left, then up.
Klossy?
>>61524498
>Go right, then down, then left, then up
>t. the guy who hasn't worked with matrices once in his life
>>61524525
>>61524552
>>61524589
>>61524596
>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<matrix[0].length;i++){
int t1 = matrix[0][i], t2 = matrix[matrix.length-1][matrix[0].length-1-i];
matrix[0][i] = c1;
matrix[matrix.length-1][matrix[0].length-1-i] = c2;
c1 = t1;
c2 = t2;
}
for(int i = 1;i<matrix.length;i++){
int t1 = matrix[i][matrix[0].length-1], t2 = matrix[matrix.length-1-i][0];
matrix[i][matrix[0].length-1] = c1;
matrix[matrix.length-1-i][0] = c2;
c1 = t1;
c2 = t2;
}
for(int i = 0;i<matrix.length;i++) {
for(int j = 0;j<matrix[0].length;j++)
System.out.printf("%3d",matrix[i][j]);
System.out.println();
}
}
}
Provided I understood this correctly.
>>61524776
Is Intel putting chewing gum under their chips now???
>>61524091int size, tempA, tempB, tempC, tempD;
size = matrix.size() - 1;
tempA = tempB = tempC = tempD = 0;
for(int i = 0; i <= size; i++){
swap(matrix, 0, i, tempA);
swap(matrix, i, size, tempB);
swap(matrix, size, size - i, tempC);
swap(matrix, size - i, 0, tempD);
}
1 (one) loop fgt. swap(a,b,c,d) just swaps the value of a[b][c] with the variable d. Sort of pseudocode obviously.
>>61524875
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]
>>61524884
Doesn't work with non-square matrices.
>>61524875
>>61524876
>>61524884
>>61524969
>size
>lenght
sorry those are 3 loops
>>61524110
we're reaching new levels of grammar
>>61524977
Yeah I just realized that it might not be square, easily adaptable to two loops though. or maybe inefficiently still to one.
>>61525007
Normally someone would pass in the dimensions...
>>61525007
Depends on the implementation retard
>>61524091
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?
>>61525007
len() is O(1) in Cpython
>>61524091
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).
>>61524633
>lets write the loop and leave the actual problem as an exercise
Nice solution, anon.
Only works for square matricies because I'm a brainlet, also I used >>61524255 's solutiondef 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
>>61525260
>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<Math.max(matrix[0].length,matrix.length);i++){
if(i<matrix[0].length) {
int t1 = matrix[0][i], t2 = matrix[matrix.length-1][matrix[0].length-1-i];
matrix[0][i] = c1;
matrix[matrix.length-1][matrix[0].length-1-i] = c2;
c1 = t1;
c2 = t2;
}
if(i<matrix.length) {
int t3 = matrix[i][matrix[0].length-1], t4 = matrix[matrix.length-1-i][0];
matrix[i][matrix[0].length-1] = c3;
matrix[matrix.length-1-i][0] = c4;
c3 = t3;
c4 = t4;
}
}
}
}
>>61525302
>>61525335
this pajeet, damn
>>61525303
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
>>61524299
You're a brainlet.
In what application you need to do such operation?
#include <iostream>
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 << a[i][j] << " ";
}
cout << endl;
}
}
void rotate(int a[][y]){
int aux1=a[0][y-1];
int aux2=a[x-1][0];
for (int i = 1; i < y; i++){
a[0][y-i]=a[0][y-1-i];
a[x-1][i-1]=a[x-1][i];
}
for (int j = 1; j < x; j++){
a[x-j][y-1]=a[x-j-1][y-1];
a[j-1][0]=a[j][0];
}
a[1][y-1]=aux1;
a[x-2][0]=aux2;
}
int main(){
int a[x][y];
fillR(a);
print(a);
rotate(a);
cout << endl;
print(a);
return 0;
}
It has a fill and print functions for easy reproduction, the rotate function is the desired solution.
>>61525651
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]]
>>61526024
Make me
>>61526418
Needs to bebl = head . last . init $ xs
You could do that with just one loop.
>>61524121
When will you faggots realize that meme arrows aren't exclusively for quoting?
>>61524452
fuck it, just stop using loops and hard code everything why don't cha
>>61524402
>9000 ifs
Its four conditionals, which run in constant time.
>>61525020
We are maintaining normal levels of new