SKUPINA SLUŽBY STEAM
Kickass Programmers quprogs
SKUPINA SLUŽBY STEAM
Kickass Programmers quprogs
0
VE HŘE
7
ONLINE
Založena
6. listopadu 2015
Jazyk
Angličtina
Všechny diskuze > General Discussion > Detaily tématu
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.
Naposledy upravil makka; 3. dub. 2016 v 23.46
< >
Zobrazuje se 14 z 4 komentářů
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; }
Naposledy upravil makka; 4. dub. 2016 v 0.03
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() } }
Naposledy upravil birb; 4. dub. 2016 v 11.15
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)
Python is cheating ;)
< >
Zobrazuje se 14 z 4 komentářů
Na stránku: 1530 50

Všechny diskuze > General Discussion > Detaily tématu