Programming Quiz. See pic. Post your code

Programming Quiz. See pic. Post your code.

Attached: 1525735622064.png (1080x1106, 682K)

Other urls found in this thread:

download.racket-lang.org/docs/5.0/html/htdp-langs/beginner-abbr-prim-ops.html
en.wikipedia.org/wiki/Operator_(computer_programming)
htdp.org/
twitter.com/AnonBabble

Wasn't there something similar in the last Advent of Code? I wasn't able to solve it, and the solutions I saw seemed pretty complicated.

are the people on Jow Forums who can't do this

also, don't post that SLUT.

Attached: 1523306308217.gif (541x443, 2.52M)

int n, m;
assert(n > 0 && m > 0);
return a rectangular array of size n by m filled witrh integers 1 through n*m arranged in an inwards spiral pattern starting at top left;

>also, don't post that SLUT.

Attached: you should be able to solve this.jpg (500x481, 29K)

>>>/projecteuler/

>are the people on Jow Forums who can't do this
Pajeet, my son

People who post in nvidia, amd, intel, apple, linux, windows, browser, thinkpad, mpv, discord, watch, keyboard, private tracker, headphone, smartphone, chinkshit, battlestation threads definitely can't do that.

you forgot dpt too

I think I have a way in my head to do this but I feel like it's going to be extremely convoluted. Before I try, can you tell me if there's meant to be a very simple way to go about this I'm not aware of, or is it going to get fiddly?

/dpt/ is full of Pajeets.

>tfw 7GL

Attached: 1526247019788.jpg (377x351, 24K)

can i solve this in matlab or do you autistic fucks get triggered?

Just solve it, nigger.

wait, my cuckbook can't open matlab and abaqus at the same time

i'm trying with octave

yes it was one of the first problems too

/dpt/ here. buffer less solution:

#lang racket

(define n (read))
(define width (+ 2 (inexact->exact (floor (/ (log (* n n)) (log 10))))))

(define (clockwise x y n)
(define n-1 (sub1 n))
(define k (min (min x (- n-1 x))
(min y (- n-1 y))))

(define start (add1 (* 4 k (- n k))))

(define n-1-k (- n-1 k))
(define n-1-k-k (- n-1-k k))

(+ start (if (= y k)
(- x k)
(+ n-1-k-k
(if (= x n-1-k)
(- y k)
(+ n-1-k-k
(if (= y n-1-k)
(- n-1-k x)
(+ n-1-k-k (- n-1-k y)))))))))

(define (counter-clockwise x y n) (clockwise y x n))

(for ([y (in-range n)])
(for ([x (in-range n)])
(display (~a (clockwise x y n) #:min-width width #:align 'right)))
(newline))

What a crazy language that is.
Also, variable names include operators. It's madness.

What happened to M?

gay nigger

>What a crazy language that is.
First fucking line.

>racket
Why the hell is this trash still popular with normies trying to learn cs?

looks like lisp? is this a derivative?

Not a question.
>That is.
>is that?

there is no operators in lisp, only procedures

def op_is_a_faggot(n, m):
sq = [[0] * m for _ in range(n)]

r = [0, n-1, 0, m-1]
d = (0, 1)
p = (0, 0)

nd = {(0,1): (1,0), (1,0): (0,-1), (0,-1): (-1,0), (-1,0): (0,1)}
nr = {(0,1): [1,0,0,0], (1,0): [0,0,0,-1], (0,-1): [0,-1,0,0], (-1,0): [0,0,1,0]}

i = 1

while r[0]

Attached: 1502247447293.png (141x46, 2K)

not implemented.

can you write a program to solve those sliding number puzzle things?

python 2?

I made a garbage. I'm sure there is a better way to do this but I didn't look it up.
size = input('gib numbers:')
size = [int(x) for x in size.split()]
matrix=[ [None]*size[0] for x in range(size[1]) ]
def m():
for x in matrix:
print(x)
x=0
y=0
count=0
while True:
if matrix[y][x] == None:
count+=1
else:
m()
break
matrix[y][x]=count
if x+1

yeah, there is a solution in the book "think like a programmer"

>make a random move
>check if the puzzle is solved
>if not, make another random move
>repeat until solved

I call it the "do what most humans do" algorithm.

download.racket-lang.org/docs/5.0/html/htdp-langs/beginner-abbr-prim-ops.html

Works in Python3, didn't bother testing it with 2.7 but it should work.

You could call it local search with no heuristic whatsoever. Or unlimited crazy random DFS.

can someone do this in haskell for me

and then in an assembly language

those are procedures. there no need of operators because there is no need of precedence since lisp is using reverse polish notation.

this is legal in lisp
(define old+ +) ;; saves original + procedure

;; defines our own + procedure
(define (+ a b)
(printf "(+ ~a ~a~)%" a b) ;; log/trace
(old+ a b))

(displayln (+ 1 2))

public class Spiral {
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
int m = Integer.parseInt(args[1]);

int[][] a = new int[n][m];

fill(a, n, m);

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.printf("%3d", a[i][j]);
}
System.out.println();
}
}

static void fill(int[][] a, int n, int m) {
for (int k = 1, sn = 0, sm = 0;;) {
for (int i = sm; i < m; i++) {
a[sn][i] = k++;
}
if (++sn == n) break;
for (int i = sn; i < n; i++) {
a[i][m - 1] = k++;
}
if (sm == --m) break;
for (int i = m - 1; i >= sm; i--) {
a[n - 1][i] = k++;
}
if (sn == --n) break;
for (int i = n - 1; i >= sn; i--) {
a[i][sm] = k++;
}
if (++sm == n) break;
}
}
}

Attached: ss-2018-05-16-00-42-55.png (852x457, 55K)

One of the most powerful language

>using reverse polish notation.
It uses regular polish notation

why the fuck shoud i take a chinese cartoon pointing a question seriously¿ seems way too autistic and unprofessional

couldn't remember which one exactly.

because the people behind racket are amongst PLT finest. today, the two communities doing the most of the stuff in plt are racket and haskell.

You and me clearly have a different understanding of what operators are.

Even if in polish notation (+ 1 2), "+" is still an operator, it just happens not to be in the middle. However it is an operator because it's a symbol defining a standard function. A letter for example "a" is also a symbol, it can define a function, but it's not standard, nobody will understand it without context.

Are you into research? I'm just not seeing where the popularity among noobs comes from. Theory is way over their heads.

no, "+" is nothing else than a symbol. it's just that our current math convention use it to represent the operation known as "addition". like the number 5 is represented by the symbol "5", or "IV", or "101" (binary), or ...

an operator, programming wise, is a function with specific semantics for precedence.

en.wikipedia.org/wiki/Operator_(computer_programming)

>Are you into research?
no. but racket has been used as a educational platform for years.

htdp.org/
berkeley uses it to teach sicp
mit were using it for sicp too.
www.bootstrapworld.org/

#include

using namespace std;

int main()
{
int x;
int y;

cout > x;

cout > y;
cin.ignore();

int stage = 1;
int numToInsert = 1;

int rectangleArray[x][y];

for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
if ((stage % 2) == 0)
rectangleArray[(x - 1) - j][i] = numToInsert;
else
rectangleArray[j][i] = numToInsert;

numToInsert++;
}
stage++;
}

for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
cout

i did it with recursion

function A = answerthispostoryourmomwilldie(n,m)

if n == 1
A = [1:m];
return;
end
if m == 1
A = [1:n]';
end

A = zeros(n,m)

A(1,:) = [1:m]
A(:,m) = [m:m+n-1]'
A(end,[end:-1:1]) = [A(end,end):2*m+n-2]
A([end:-1:2],1) = [A(end,1):2*(m+n)-4]'

A(2:n-1,2:m-1) = A(2,1) + answerthispostoryourmomwilldie(n-2,m-2)

I don't see how the wikipedia says that, it even gives an example pretty similar to what you're defining is not an operator: "1 2 3 4 +". Just change postfix to prefix.

Also, 5, or "IV" are also simbols, yes, but in no way define standard functions. They define constant values.

oc, add semicolons, i was just debugging

> Mfw I misinterpreted the task as a snaking pattern not a spiral
Am I retarded?

I stand corrected then.

anyway the original lisp as defined by mc carthy doesn't have any operator defined (doesn't need to because of lambda calculus and church numeral)

>Am I retarded?
Yes, seek help.

#include
#include

enum directions { RIGHT, DOWN, LEFT, UP };

int main(int argc, char **argv)
{
if (argc != 3)
exit(EXIT_FAILURE);

int n = atoi(argv[1]);
int m = atoi(argv[2]);
if (n

>Using ASCII properties
And here I thought you'll use escape sequences.

Nah, scrap that, there's nothing about ASCII in here, I mixed up with another program.

and then in brainfuck

I was wondering why yours was the only one that made sense.
I'm and retarded. I was thinking of a spiral but looking at it from the side.

re-jiggering it right now it's not bad desu just a retard and distracted atm which is why it's taking a bit

Rate my shitty psuedo code
(Not my handwriting aheheh)
Would code it up (and try it) but comp is on the fritz

Attached: 20180515_164243.jpg (4032x3024, 2.45M)

Forgot to declare direction as 1.

Attached: 1467881417845.png (621x334, 306K)

spiral =: 4 : '(x*y) - |."1|.(|:@|. , {:@{: + >:@i.@#)^:(+:: | x-y'

>T. Brainlet

>inwards spiral pattern

What the fuck?

He's technically right. Lisp has no concept of an operator. (+ 1 2) is essentially a list with a string + and the numbers 1 and 2. In python or js, it'd be equivalent to [ "+", 1, 2 ]. The lisp eval function sees this list and concludes its a function invocation. It evaluates the "+" variable and usually it returns a function which adds together all arguments. Lisp then applies this function to the arguments 1 and 2.

In other languages, operators are literally part of their grammar and baked into the compiler itself.

Te eval function, if I understand correctly, is part of the language. Hence, the behavior to treat the "+" symbol as an operator is also part of the language, therefore yes, the operators are part of the language.

In C and other languages you can use a standard function like "printf" to output formatted output like "Number: %d". Even if it's a string (or a list, doesn't matter), the symbol "%" inside the string is also an operator. Fine, a operator that only works with formatted strings in this case, but in lisp the construct (+ 1 2) seems to work everywhere (you don't need to explicitly mention the eval function).

This took me the better part of an hour. I feel like there is some way to get rid of those break if's, but whatever. Rip into me.
public void spiral(){
int counter = 1;
int iteration = 0;
while(counter-1+iteration;j--){
spiral[j][m-1-iteration] = counter;
counter++;
}
if(counter>n*m){break;}
for(int j = m-2-iteration;j>0+iteration;j--){
spiral[iteration][j] = counter;
counter++;
}
iteration++;
}
}

>the behavior to treat the "+" symbol as an operator is also part of the language

This is false. Eval treats "+" as any other symbol, such as plus, some-variable, another+variable, *yet-another-one* and so on. + is in no way special. Your conclusion doesn't follow.

>in lisp the construct (+ 1 2) seems to work everywhere (you don't need to explicitly mention the eval function).

Because the program is already being evaluated. If your program is (+ 1 2), it's as if the interpreter was executing (eval '(+ 1 2)).

Actual software engineer here. Should I find another line of work?
static int* SpiralArray(int height, int width) {
assert(height>0&&width>0);
int* theArray = malloc(sizeof(int) * height * width);
int horizontalTravel = width;
int verticalTravel = height-1;
int counter = 1;
int currentHorizontalPosition = -1;
int currentVerticalPosition = 0;
while (true) {
for (int i = 0; i < horizontalTravel; i++) {
theArray[currentVerticalPosition * width + (++currentHorizontalPosition)] = counter++;
}
horizontalTravel--;
if (counter > height*width) break;
for (int i = 0; i < verticalTravel; i++) {
theArray[(++currentVerticalPosition) * width + currentHorizontalPosition] = counter++;
}
verticalTravel--;
if (counter > height*width) break;
for (int i = 0; i < horizontalTravel; i++) {
theArray[currentVerticalPosition * width + (--currentHorizontalPosition)] = counter++;
}
horizontalTravel--;
if (counter > height*width) break;
for (int i = 0; i < verticalTravel; i++) {
theArray[(--currentVerticalPosition) * width + currentHorizontalPosition] = counter++;
}
verticalTravel--;
if (counter > height*width) break;
}
return theArray;
}

I'm a retard and didn't include the whole thing.
public class SpiralDimensions
{
int[][] spiral;
int end;
int n;
int m;
public SpiralDimensions(int n, int m){
spiral = new int[n][m];
end = n*m;
this.n = n;
this.m = m;
}

public void spiral(){
int counter = 1;
int iteration = 0;
while(counter-1+iteration;j--){
spiral[j][m-1-iteration] = counter;
counter++;
}
if(counter>n*m){break;}
for(int j = m-2-iteration;j>0+iteration;j--){
spiral[iteration][j] = counter;
counter++;
}
iteration++;
}
}

public void printSpiral(){
for(int i = 0;i

What language is this? C#?

Anyone know APL?
Seems like a decent use-case.

You can do this cleanly by filling a single layer then recursively filling the rest

>malloc
>int*
yeah, it's c#

How are you supposed to fill anything after the first line? There is no easy way to calculate what is supposed to be where.

Straight C. I wrote a quick function to print it too:
static void Print2DArray(int* array, int height, int width) {
int numberOfEntries = height*width;
int entryLength = ceil(log10(numberOfEntries))+1; // +1 for space
char buf[entryLength*width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int currentValue = array[y*width+x];
snprintf(buf + (entryLength * x), entryLength+1, "%*d ", entryLength-1, currentValue);
}
buf[entryLength*width-1] = 0; // it got overwritten with a space above
printf("%s\n", buf);
}
}

>Eval treats "+" as any other symbol.
Not really. Any other symbol wouldn't have a function associated with it would it? If I do (a 1 2) what happens? Adds? Multiplies?

Let me put it with another example. Say I put a python interpreter in a C project, and then I hardcode a python script as a string and execute it with some call. Right at that moment my code, my project, contains lambdas, even when C does not. In fact, right now my project contains everything Python has and the python language itself. Now, if C had this "python interpreter" as part of the language, then the lamdas, and all in Python, would also be part of the C language. Case in point, eval is part of Lisp.

Actually, in my example with C, you would still have a strong distinction between the python code and the C code (though it would still be part of C), the first requires a call to be executed. But with eval, that is implicit.

I miss AoC, the threads were great fun.

Attached: 1496734730732.png (3282x2475, 3.29M)

Rotate the index maybe.
fill(matrix, start_x, end_x, start_y, end_y, i, j) {
...
fillLine(matrix, start_y+1, end_y, start_x, end_x, j, i)

Something like that.

i honestly have no idea what this means, but i use arch architect to install linux on my macbook pro so...

The power of python, ladies and Jow Forumsentlemen

>Any other symbol wouldn't have a function associated with it would it?

Depends on the state of the interpreter. Just because the Lisp comes with a default + function doesn't mean it's somehow special. You can do things like set + to - or to "kek" and it's not crazy at all: you're just setting the variable "+" to some value. If you set it to a string, it will obviously break all code that tries to use + as a function because the evaluator doesn't know how to apply arguments to strings.

>If I do (a 1 2) what happens?

Depends on what a is set to. If it evaluates to a function, the arguments will be applied to it. Otherwise an error will be raised.

>Case in point, eval is part of Lisp.

The point isn't whether eval is part of the language. The point is whether the notion of operators is part of eval. It certainly isn't. You could certainly write an eval (or extend eval with a macro) which treats "+" specially but it's not traditional Lisp semantics.

function rect (n, m) {
let arr = []
for (let i = 0; i < n; i++) {
arr.push([])
for (let j = 0; j < m; j++) {
arr[i].push(0)
}
}

let k = 0

for (let i = 0; i < n; i++) {
topRow(i, i, m - 1)
if (k >= n * m) break
rightColumn(i, m - i - 1, n - i)
if (k >= n * m) break
bottomRow(n - i - 1, m - i - 1)
if (k >= n * m) break
leftColumn(n - i - 1, i)
}

console.log(arr)

function topRow (row, n, m) {
while (arr[row][n + 1] === 0) arr[row][n++] = ++k
}

function rightColumn (row, m, n) {
while (row < n) arr[row++][m] = ++k
}

function bottomRow (row, col) {
while (arr[row][col - 1] === 0) {
arr[row][col - 1] = ++k
col--
}
}

function leftColumn (row, col) {
while (arr[row - 1][col] === 0) {
arr[row - 1][col] = ++k
row--
}
}
}

rect(7, 7)


>tfw brainlet

Attached: file.png (1366x738, 11K)

i know a solution using priority queues

oh man this was so fun.
Can't wait to get buttfucked again.

>Just because the Lisp comes with a default + function doesn't mean it's somehow special.
It does, and that's actually what makes all the difference.

var width = 4, height = 4;
var spiral = [];

var rbound = width-1,
bbound = height-1,
lbound = 0,
tbound = 1;

var x = -1, y = 0, dir = 0;
for (var i=1; i

>JavaScript
At least you're using let unlike this faggot

Close enough

Attached: Screenshot from 2018-05-15 23-02-54.png (690x644, 40K)

I appreciate the feedback and I'll try to code in a less gay way, but why would let != var for a single-scope challenge like this?

Every time I see a question worded in this way I call it an irrelevant waste of time. Then I move on in my life.

here is my ugly piece of crap, fun challenge, little more challenging than the usual lame fizzbuzz
let kurisu=function(width,height){
//create 2D array manually because Javascript, and value ranges
let arr=[];
let countdown=width*height;
let iter=1;
for(i=0;ibottom
for(b=(B2Tterm+1);b=countdown){break;}
//right->left
for(c=(L2Rterm-1);c>=R2Lterm;c--){
arr[T2Bterm][c]=iter;
iter++;
}
if(iter>=countdown){break;}
//bottom->top
for(d=(T2Bterm-1);d>B2Tterm;d--){
arr[d][R2Lterm]=iter;
iter++;
}
if(iter>=countdown){break;}
//Update bounds
L2Rterm--;
T2Bterm--;
R2Lterm++;
B2Tterm++;
}
console.log(arr);
}

kurisu(5,3);

Except this time

wimp
man up and kode

Wow, that was easy.

Attached: unknown.png (380x457, 253K)

Attached: Aesop_fox and grapes.jpg (400x394, 73K)

Scissoring.