STEAM-GRUPP
Kickass Programmers quprogs
STEAM-GRUPP
Kickass Programmers quprogs
1
SPELAR
7
ONLINE
Grundades
6 november 2015
Språk
Engelska
makka 3 apr, 2016 @ 23:45
Code Golf -- Print N Squared
Write a function that takes in a non-negative integer N and prints out or returns a hollow ASCII-art square whose sides are made of N copies of the number N. Link to full challenge, details and unit tests here[codegolf.stackexchange.com]

An example for if N were 10, the output would be:

10101010101010101010 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10101010101010101010

Discuss and leave solutions in the thread.
Senast ändrad av makka; 3 apr, 2016 @ 23:46
< >
Visar 1-4 av 4 kommentarer
makka 4 apr, 2016 @ 0:00 
C++14, 156 chars
#define f for(i=0;i++<n;c<<t); [](string t){auto&c=cout;int n=stoi(t),i;f c<<'\n';for(i=0;++i<n-1;c<<t,c.width(~-n*size(t)+1),c.fill(0),c<<t+'\n');if(n-1)f}

This one is pretty cool though it took a long time of fidgeting to get it to work. Unpacked:
#define f for ( i = 0; i++ < n; c << t ); [](string t) { auto& c = cout; int n = stoi(t), i; f // print first row c << '\n'; // kind of annoying but no way to get rid of (yes I tried // c << '\n'+t instead of c << t+'\n') for ( i = 0; ++i < n - 1; ) { c << t; // output the number // then we we get the width of necessary spaces c.width(~-n*size(t)+1); // Equivalent to (n-1)*size(t) + 1, but we save // two characters because ~- takes precedence over // the multiplication c.fill(0); // fill with spaces, ' ' == 0 c << t+'\n'; } if ( n-1 ) f // This if statement is dissapointing }

And like always... to run it you need to use [](string t) { ... }("10");

C++14, 162 chars
The above was my first attempt at the solution and took about 20 minutes. This next one took close to an hour, but unfortunately was not as good as my first attempt. It's a lot more sane, but 6 more chars, if it were not for edge cases 1 & 0 this could match my first solution. I figured I'd post it anyways since I spent so much time on it.
#define f for(i=-2;i++<n;c<<t);c<<'\n'; [](string t){auto&c=cout;int n=stoi(t)-2,i;auto g=t;g.append(abs(n)*size(t),' ');g+=t+'\n';f;for(i=0;i++<n;c<<g);if(n+1)f;}

and unpacked:

[](string t) { auto&c = cout; int n = stoi(t)-2,i; auto g = t; // creating "num ... num" // this abs(n) is very unfortunate, blame edge cases 1 & 0 g.append(abs(n)*size(t),' '); g += t+'\n'; f; for ( i = 0; i++ < n; c << g); if ( n+1 ) f; }
Senast ändrad av makka; 4 apr, 2016 @ 0:03
birb 4 apr, 2016 @ 10:56 
Rust, 141 137 chars

|i|{let f=||{for _ in 0..i{print!("{}",i)}println!("")};f();if i>1{for _ in 0..i-2{println!("{}{0:1$}",i,i.to_string().len()*(i-1))}f()}}

Unpacked:
|i| { let f = || { for _ in 0..i { print!("{}",i) } println!("") }; f(); if i>1 { for _ in 0..i-2 { println!("{}{0:1$}",i,i.to_string().len()*(i-1)) } f() } }
Senast ändrad av birb; 4 apr, 2016 @ 11:15
mingmingrr 5 apr, 2016 @ 11:37 
Python 3, 102 chars
s=str(n);print(''.join('\r'+s*n if n==0 or i%n==0 else '\n'+s+' '*len(s)*(n-2)+s for i in range(n+1)))
Unpacked and refactored:
s = str(n) x = '' for i in range(n + 1): if n == 0 or i % n == 0: # carriage return here clears the current line x += '\r' + s * n else: x += '\n' + s + ' ' * len(s) * (n - 2) + s print(x)
Python 3, 60 chars
I found out later someone had this code almost letter for letter, grrr...
Does not support edge cases 1 or 0.
s=str(n);m=n-2;print(s*n+('\n'+s+' '*len(s)*m+s)*m+'\n'+s*n)
Unpacked:
s = str(n) m = n - 2 print(s * n + ('\n' + s + ' ' * len(s) * m + s) * m + '\n' + s * n)
makka 6 apr, 2016 @ 13:21 
Python is cheating ;)
< >
Visar 1-4 av 4 kommentarer
Per sida: 1530 50