GRUP STEAM
Kickass Programmers quprogs
GRUP STEAM
Kickass Programmers quprogs
1
SEDANG BERMAIN
8
ONLINE
Dibentuk
6 November 2015
Bahasa
Bhs. Inggris
Semua Diskusi > General Discussion > Rincian Topik
makka 3 Apr 2016 @ 11:45pm
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.
Terakhir diedit oleh makka; 3 Apr 2016 @ 11:46pm
< >
Menampilkan 1-4 dari 4 komentar
makka 4 Apr 2016 @ 12:00am 
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; }
Terakhir diedit oleh makka; 4 Apr 2016 @ 12:03am
birb 4 Apr 2016 @ 10:56am 
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() } }
Terakhir diedit oleh birb; 4 Apr 2016 @ 11:15am
mingmingrr 5 Apr 2016 @ 11:37am 
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 @ 1:21pm 
Python is cheating ;)
< >
Menampilkan 1-4 dari 4 komentar
Per halaman: 1530 50

Semua Diskusi > General Discussion > Rincian Topik