GRUPO DE STEAM
Kickass Programmers quprogs
GRUPO DE STEAM
Kickass Programmers quprogs
0
JUGANDO
5
ONLINE
Fundado
6 de noviembre de 2015
Idioma
Inglés
makka 3 ABR 2016 a las 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.
Última edición por makka; 3 ABR 2016 a las 23:46
< >
Mostrando 1-4 de 4 comentarios
makka 4 ABR 2016 a las 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; }
Última edición por makka; 4 ABR 2016 a las 0:03
birb 4 ABR 2016 a las 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() } }
Última edición por birb; 4 ABR 2016 a las 11:15
mingmingrr 5 ABR 2016 a las 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 ABR 2016 a las 13:21 
Python is cheating ;)
< >
Mostrando 1-4 de 4 comentarios
Por página: 1530 50