# Temporary Programming Challanges!

2582 Views 22 Replies 8 Participants Last post by  shinya
I'll start a series of programming challenges starting at easy difficulty moving on towards much harder difficulties. Feel free to try and do them. Post your solutions when you have them. You can use any language you prefer. Please comment your code for others to learn from.

7/18/2007 -- Challenge #1 -- Level 1 ! Novice

Write a program that takes an odd integer and character as an input and outputs a diamond of the character where the center row of the diamond is the amount of characters specified by the integer.

For example. If the input is "5" and "*" .

It would output: Where the .'s are spaces.

....*....
..***..
*****
..***..
....*....

The goal is to write the application in as few lines as possible. You are not allowed to just write print statements that print each line.

You have until Friday night to solve. This should be a very simple task. Enjoy!
1 - 20 of 23 Posts
thats simple recursion. wait a second... are you trying to get us to do your homework?
See less See more
Here was my solution when my instructor gave us this challenge. It is Java.

Code:
Code:
``````import java.util.Scanner;
public class Mumupwn {
public static void main(String[] args) {
System.out.println("Enter an ODD number: ");
Scanner s1 = new Scanner(System.in);
int size = s1.nextInt();
System.out.println("Enter a single character: ");
String icon = s1.next();
String[] sa = new String[size];
StringBuilder sb = new StringBuilder();
String space = " ";
char[] ca = space.toCharArray();
char s = ca[0];
int midpoint = size / 2 + 1;
for ( int i = 0; i < size; i++ ){sb.append(icon);}
sa[midpoint - 1] = sb.toString();
int fLetter = 0;
int lLetter = size - 1;
int i = 1;
while ( i < midpoint){
sb.setCharAt(fLetter, s);
sb.setCharAt(lLetter, s);
sa[midpoint - 1 - i] = sb.toString();
sa[midpoint - 1 + i] = sb.toString();
fLetter++;
lLetter--;
i++;
}
for ( int j = 0; j < size; j++){System.out.println(sa[j]);}
}
}  //31 Lines - Non Optimized with 1 import statement.``````
See less See more
Quote:
 Originally Posted by stanrc thats simple recursion. wait a second... are you trying to get us to do your homework?
LOL Nope.
See less See more
2
sneaky sneaky, you changed it haha
C++ FTW. I/O is a lot easier; and my code is significantly shorter and less taxing on resources.

Code:
Code:
``````#include<iostream>
#include<string>
using namespace std;
int main() {
int a; string b; int i; int j;
cout << "Enter an ODD number";
cin >>a;
cout << "Enter a character";
cin >>b;
string c[a]; int d;
d=int(a/2)+1;
for(j=0; j<a; j++) { c[j]=" "; } //or c[j]="." if you want the dot as a space...
for(i=0; i<a; i++)
{
c[d]=b; c[d+i]=b; c[d-i]=b;
for (j=0; j<a; j++) {cout << c[j];}
cout <<"\
";
}
system("PAUSE"); //only for Windows
return 0;
}``````
21 lines, many short or just { lines

edit: of course, to use even less resource that string array could be declared as a char array; made it better
See less See more
Yeah perl.
If you have any question, just ask.
Its 14 lines.

Code:
Code:
``````#!/usr/bin/env perl

(\$num, \$char, \$count = 1, \$flag = 0); # variables
print "Enter an odd number => ";
chomp(\$num = <>); # chomp gets rid of \
char
print "Enter a character => ";
chomp(\$char = <>);
while (1) {
print (" " x (int(\$num + 1 / 2) - \$count), "\$char " x \$count,"\
");
if (\$count < \$num and \$flag == 0) { \$count += 2; } # Triangle's top
elsif (\$count >= \$num) { \$count-= 2 and \$flag = 1; } # switch from top to bottom
else { \$count-= 2; } #  Triangle's bottom half
last if (\$count <= 0 and \$flag == 1); # exit loop if condition true
}``````
See less See more
1
might whip this up real quick later... I'm supposed to be testing my code

Nice job to the guys that've finished it.

Edit: I also think your example is incorrect in the first post Decompiled. It looks like it has one to many '*'s in the center row. Not a huge deal, just thought I'd compile you're example, decompiled
See less See more
2
Quote:
 Originally Posted by kdbolt70 might whip this up real quick later... I'm supposed to be testing my code Nice job to the guys that've finished it. Edit: I also think your example is incorrect in the first post Decompiled. It looks like it has one to many '*'s in the center row. Not a huge deal, just thought I'd compile you're example, decompiled
Thanks.. I fixed it.
See less See more
3
Well Hobo, you set the mark, and I set out to match it in a "not so elegant" language, C++. Having a math minor definetly helped in this one
It's all one big math problem. 90% of my code is setting up the stupid thing, the math itself is contained in one flipping line.

Code:
Code:
``````#include<iostream>
using namespace std;
int main(){
int n; char inChar;
cout << "enter an odd number, followed by a space, then a character" << endl;
cin >> n >> inChar; //reads in both the integer and character
for (int i = 1; i <= n; i++){              //I'm not a fan of initializing For counters to 1,
for (int j = 1; j <= n; j++){     //But this makes the math easier
if (j <= abs((n-(2*i-1))/2) || j > (n-(abs((n-(2*i-1))/2)))){cout << " ";} //yeah, don't ask
else {cout << inChar;}
} cout << endl;  // start up the next line
}
}``````
there ya go. 14 lines. And some of 'em are just '}'s.

Edit: Commenting for you folks
See less See more
I was working on a different math based approach (using the sum of the x,y coords). It worked great for the top half, but I had to use a different alg for the bottom half. I ran out of time this morning before I could re-work it. Nice job.

All of the solutions are nice. However from a professional standpoint there has to be a cross between elegance, functionality and readability. The number of lines in a compilied language doesn't matter. Certainly minimizing loops and extra processing is important, but raw size doesn't really matter. Some of the solutions appear to be obfuscated in order to minimize the "size" of the application. In the professional setting you need to focus on readability a little more. Self documenting code via explanatory variable names is often very helpful. Try not to get caught up solely in the length of size of the code. Remember what you write isn't what is processed, it all goes through an interpreter and is compiled down. Same theory for variable names goes with consistant line control and tabbing. Just makes it easier for the rest of your team if you write code that doesn't need to be reformatted to read easier.

Otherwise, kudos for the work.
See less See more
I had to revise mine a bit.

8 lines

Code:
Code:
``````#!/usr/bin/env perl
(\$num, \$char, \$count); # variables
print "Enter an odd number follow by a character => ";
(\$num, \$char) = split(" ", <STDIN>); # Getting input
for (\$i = 1; \$i <= \$num; \$i += 2){ # Top half of triangle
print (" " x (int(\$num + 1 / 2) - \$i), "\$char " x \$i,"\
"); }
for (\$count = \$num - 2; \$count >= 0; \$count -= 2){ # Bottom half of triangle
print (" " x (int(\$num + 1 / 2) - \$count), "\$char " x \$count,"\
"); }``````
See less See more
2
I completely agree BFRD! It was posed to me this way so that students would have a goal to post the shortest code possible. Definitely a good point for those who are not classically instructed in computer science.
Quote:
 Originally Posted by BFRD I was working on a different math based approach (using the sum of the x,y coords). It worked great for the top half, but I had to use a different alg for the bottom half. I ran out of time this morning before I could re-work it. Nice job. All of the solutions are nice. However from a professional standpoint there has to be a cross between elegance, functionality and readability. The number of lines in a compilied language doesn't matter. Certainly minimizing loops and extra processing is important, but raw size doesn't really matter. Some of the solutions appear to be obfuscated in order to minimize the "size" of the application. In the professional setting you need to focus on readability a little more. Self documenting code via explanatory variable names is often very helpful. Try not to get caught up solely in the length of size of the code. Remember what you write isn't what is processed, it all goes through an interpreter and is compiled down. Same theory for variable names goes with consistant line control and tabbing. Just makes it easier for the rest of your team if you write code that doesn't need to be reformatted to read easier. Otherwise, kudos for the work.
Heh, I know what you mean. I only squished things a bit to get down to functioning on 14 lines. I'd like to think my actual code is stylistically decent, this was just a poor example because our #1 focus was lines used. I'm actually fairly notorious for spacing everything out nicely, often having longer than normal files. I guess there's a difference between onlince competition for fun and professional level code.

And its interesting to think of it in a coordinate system. I was almost going to break it down into binary to see if I could find a simple solution, until I figured out the math behind it. I'll look forward to seeing what you have if you do get it done!
See less See more
Quote:
 Originally Posted by BFRD I was working on a different math based approach (using the sum of the x,y coords). It worked great for the top half, but I had to use a different alg for the bottom half. I ran out of time this morning before I could re-work it. Nice job. All of the solutions are nice. However from a professional standpoint there has to be a cross between elegance, functionality and readability. The number of lines in a compilied language doesn't matter. Certainly minimizing loops and extra processing is important, but raw size doesn't really matter. Some of the solutions appear to be obfuscated in order to minimize the "size" of the application. In the professional setting you need to focus on readability a little more. Self documenting code via explanatory variable names is often very helpful. Try not to get caught up solely in the length of size of the code. Remember what you write isn't what is processed, it all goes through an interpreter and is compiled down. Same theory for variable names goes with consistant line control and tabbing. Just makes it easier for the rest of your team if you write code that doesn't need to be reformatted to read easier. Otherwise, kudos for the work.
I'd have to agree. I think readability is more important than size.
When I write something for a course, I use a lot of space and comments in my code. In the end I'm happy with the time I put into it to add comments and try to work out good logic. They look nothing the packed mess of code, like the two examples I posted.
See less See more
Quote:
 Originally Posted by dangerousHobo I had to revise mine a bit. 8 lines Code: Code: ``````#!/usr/bin/env perl (\$num, \$char, \$count); # variables print "Enter an odd number follow by a character => "; (\$num, \$char) = split(" ", ); # Getting input for (\$i = 1; \$i <= \$num; \$i += 2){ # Top half of triangle print (" " x (int(\$num + 1 / 2) - \$i), "\$char " x \$i,"\ "); } for (\$count = \$num - 2; \$count >= 0; \$count -= 2){ # Bottom half of triangle print (" " x (int(\$num + 1 / 2) - \$count), "\$char " x \$count,"\ "); }``````

Ugh, nice job. Can't compete with that in a C based language

Edit: yipee 400 posts!
See less See more
3
OMG, I can write it all on the same line in C by never hitting return!

See less See more
Quote:
 Originally Posted by Muhahahaha OMG, I can write it all on the same line in C by never hitting return!

And I came in here hoping another challenge was posted...
See less See more
3
See the new thread... Apparently nobody's attempted it yet =\\
1 - 20 of 23 Posts