My internet friend had an interesting assignment for his programming midterm, so I thought I'd share it with you guys and see how creative we can get with our solutions.
Use whatever language you like; cookies for participants.
The Problem
Assume the user gives you a numeric input ranging from 1 to 7.
When the input is 1, print the following:
*********** ********* ******* ***** *** *
Values greater than one should generate multiples of the pattern, ending with the one above, but always stacked symmetrically.
For example, 3 should print the following:
*********** *********** *********** ********* ********* ********* ******* ******* ******* ***** ***** ***** *** *** *** * * * *********** *********** ********* ********* ******* ******* ***** ***** *** *** * * *********** ********* ******* ***** *** *
For bonus points, also print the reverse:
*********** *********** *********** ********* ********* ********* ******* ******* ******* ***** ***** ***** *** *** *** * * * *********** *********** ********* ********* ******* ******* ***** ***** *** *** * * *********** ********* ******* ***** *** * * *** ***** ******* ********* *********** * * *** *** ***** ***** ******* ******* ********* ********* *********** *********** * * * *** *** *** ***** ***** ***** ******* ******* ******* ********* ********* ********* *********** *********** ***********
My Solution
I used C#. I wrote the tests first, as I do with most programming tasks, especially those with detailed data manipulation. The first pass was completely imperative, with a ton of loops and counters all shoving stuff into a buffer, but once I had something working I boiled it down to its essence and LINQified it.
Source code below.
Edit: added second parameter.
./triforce.php [count [size]]
Nice.
Wrote this one in C, my usual choice for everything. Thought of using python, but meh.
Does the inverse one as well
I like this version better:
Anyone gonna make a 1-liner Perl script to do it?
Mine is in python and uses combinators. Not the shortest..
New user here.
My four-line solution in Python 2 (or 3?), doesn't do the reverse:
def triforce(num): for i in range(num - 1, -1, -1): for x in range(5, -1, -1): print (' ' * (6 * (num - 1 - i))) + ' '.join(('%s%s%s' % (' ' * (5 - x), '*' * ((2 * x) + 1), ' ' * (5 - x)) for m in range(0, i + 1))) triforce(int(input()))
Not sure how to do the fancy code tags, but that works, I guess .
Edit: fixed.
The bar has been raised!
This is my most compact solution, using a string extension for String.Repeat().
Edit: Removed prompts.
Now, what does that say about Python and C# ;P.
They both let you take the long way or the short way?
n/m
Perl one-liner, does not do reverse. Takes a single argument on the commandline.
(EDIT: The following prints it reversed as well.
)
Can be run as:
/usr/bin/perl -e 'map { print "".(" " x (6*int($_/6))), ((" " x ($_%6)).("*" x (11-2*($_%6))).(" " x ($_%6))." ") x ($ARGV[0]-int($_/6)), "\n"; } ((0 .. (6*$ARGV[0])-1));' 3
... What?
Now, what does that say about Python and C# ;P.
Modded my solution to look slightly more like yours*, just because it looks eerily similar for two languages you probably wouldn't place next to each other at the dinner table.
*Looks almost exactly the same if you run it through a C# interpreter to avoid the boilerplate stuff!
I'll try a Ruby solution tomorrow if I feel like it.
Obligatory whitespace solution.
(Only 7 lines)
Haskell:
c++:
One line of Python:
print('\n'.join('\n'.join(((' ' * (6 * n)) + ' '.join(('%s%s%s' % (' ' * (5 - x), '*' * ((2 * x) + 1), ' ' * (5 - x)) for m in range(i + 1)))) for x in range(5, -1, -1)) for n, i in enumerate(range(int(input()) - 1, -1, -1))))
Doesn't print the reverse, at least yet
Edit: Online! The stuff with input() is to fake it when there is no input() allowed. http://codepad.org/ZwE4KsP3
All I keep thinking is newfags can't triforce.
newfags can't triforce [knowyourmeme.com].
Well, truth be told, I seen the original post about 1 hour after it started, and began working on a plain old C implementation. About 15 minutes after that, I saw the "Now, what does that say about Python and C#" quote, and was somewhat flummoxed. Anyway, during the course of this I was informed that I no longer had a job, which torqued my gourd for quite a few hours, so I gave up entirely. But in the end, there have been a few posts well beyond what I'm capable of, so kudos to you . Other subjects and areas of interest notwithstanding of course...
I seen the original post
You what?
Load "triforce",8,1
Run
I seen the original post
I first saw the original post?
Concurrent version lolol:
1000 iterations (and reverse)
Single-threaded: 1.175s
Multi-threaded: 0.669s
var iterations = Int32.Parse(Console.ReadLine()); var reverse = Boolean.Parse(Console.ReadLine());
That's just ugly. It would be easier to use args.
Both Parse methods are going to throw if the line isn't parse-able anyway so if you were going for compact you could just ignore an args.Length check and get similar behavior for incorrect usage.
triforce :- triforce(3). triforce(N) :- triforce(N,6). triforce(N,Size) :- triforce(N,Size,true). triforce(N,Size,Reverse) :- triforce(N,Size,Reverse,0). triforce(N,Size,Reverse,Indent) :- N>0, triforce(N,Size,Reverse,Indent,0). triforce(0,_Size,_Reverse,_Indent). triforce(N,Size,Reverse,Indent,Row) :- Row<Size, Off is 1+Row*2, On is Size*2-Off, Indent2 is Indent+1, Row2 is Row+1, tfScan(N,Indent,On,Off), triforce(N,Size,Reverse,Indent2,Row2), (Reverse, tfScan(N,Indent,On,Off); true). triforce(N,Size,Reverse,Indent,Size) :- N2 is N-1, triforce(N2,Size,Reverse,Indent). tfScan(N,Indent,On,Off) :- N>0, tfRun(' ',Indent), tfRun('*',On), N2 is N-1, tfScan(N2,Off,On,Off). tfScan(0,_Indent,_On,_Off) :- nl. tfRun(C,M) :- M>0, write(C), M2 is M-1, tfRun(C,M2). tfRun(_C,0).
Can be invoked with a query such as
?- triforce. ?- triforce(3). ?- triforce(3,6). ?- triforce(3,6,true).
where the 3 is the number of triangles, the 6 is the size of each one, and the Boolean is whether to print the reverse Triforce.
All I keep thinking is newfags can't triforce
▲
▲ ▲
My program briefly output
▲
▲ ▲
at one stage during its development. Much hilarity ensued.
All I keep thinking is newfags can't triforce
Newfags can't from i in Enumerable.Range(0, iterations) from j in Enumerable.Range(0, 6) select repeat(" ", 6 * i) + repeat(repeat(" ", j) + repeat("*", 11 - j * 2) + repeat(" ", j + 1), iterations - i);
Newfags can't from i in Enumerable.Range(0, iterations) from j in Enumerable.Range(0, 6) select repeat(" ", 6 * i) + repeat(repeat(" ", j) + repeat("*", 11 - j * 2) + repeat(" ", j + 1), iterations - i);
You win. I'm not sure what you win, but you definitely win.
Did you seriously write that in Prolog?
Yes
And what's more, a female friend got me into it
Oh wow.
Oh no, the Interwebs! Hide!
A bit late, but the compiler had a bug, which is fixed now.
The Modula-2 version:
Just saw this challenge, not planning on doing it in full yet, but the simplest way I find to generate a triangle is to think of it as a square, and then increasingly "sculpt" out the edges.
int end = 11; int begin = 0; for(int i = 0; i < 6; i++) { /*six rows*/ for(int k = 0; k < begin; k++) { std::cout << " "; } for(int j = 0; j < end; j++) { std::cout << "*"; } std::cout << "\n"; end -= 2; begin++; }
Well, this is pretty much done. Good job, everybody!