Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Temporary Programming Challanges!
New Posts  All Forums:Forum Nav:

Temporary Programming Challanges!

post #1 of 23
Thread Starter 
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!
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
post #2 of 23
thats simple recursion. wait a second... are you trying to get us to do your homework?
Bravo
(13 items)
 
  
CPUMotherboardGraphicsRAM
X4 955 @ 3.6 Asus M479T 4870 1GB 8 GB DDR3 
Hard DriveOSPowerCase
160 GB Win 7 Antec 750w Antec 300 
  hide details  
Reply
Bravo
(13 items)
 
  
CPUMotherboardGraphicsRAM
X4 955 @ 3.6 Asus M479T 4870 1GB 8 GB DDR3 
Hard DriveOSPowerCase
160 GB Win 7 Antec 750w Antec 300 
  hide details  
Reply
post #3 of 23
Thread Starter 
Here was my solution when my instructor gave us this challenge. It is Java.

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.
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
post #4 of 23
Thread Starter 
Quote:
Originally Posted by stanrc View Post
thats simple recursion. wait a second... are you trying to get us to do your homework?
LOL Nope.
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
post #5 of 23
sneaky sneaky, you changed it haha
Bravo
(13 items)
 
  
CPUMotherboardGraphicsRAM
X4 955 @ 3.6 Asus M479T 4870 1GB 8 GB DDR3 
Hard DriveOSPowerCase
160 GB Win 7 Antec 750w Antec 300 
  hide details  
Reply
Bravo
(13 items)
 
  
CPUMotherboardGraphicsRAM
X4 955 @ 3.6 Asus M479T 4870 1GB 8 GB DDR3 
Hard DriveOSPowerCase
160 GB Win 7 Antec 750w Antec 300 
  hide details  
Reply
post #6 of 23
C++ FTW. I/O is a lot easier; and my code is significantly shorter and less taxing on resources.

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
    
CPUMotherboardGraphicsRAM
E6400 @ 3.6 GHz (450X8) GA P965 S3 7600GT 2GB OCZ Gold @ DDR 2 900 5-5-5-15 
Hard DriveOptical DriveMonitorPower
320GB Maxtor SATA2, 160 for XP, 160 for Slackware DVD+-RW EIDE 21" CRT by Sun Microsystems 680W Echostar 22A on 12V rail 
CaseMouse
Rosewill Black small Mid-T G5 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
E6400 @ 3.6 GHz (450X8) GA P965 S3 7600GT 2GB OCZ Gold @ DDR 2 900 5-5-5-15 
Hard DriveOptical DriveMonitorPower
320GB Maxtor SATA2, 160 for XP, 160 for Slackware DVD+-RW EIDE 21" CRT by Sun Microsystems 680W Echostar 22A on 12V rail 
CaseMouse
Rosewill Black small Mid-T G5 
  hide details  
Reply
post #7 of 23
Yeah perl.
If you have any question, just ask.
Its 14 lines.

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
}
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
post #8 of 23
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
It's about time!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 @3.3Ghz Gigabyte EP45-UD3P Sapphire 2900Pro Flashed to XT 4Gb Gskill 1066Mhz PK's 
Hard DriveOptical DriveOSMonitor
Seagate Barracuda 320Gb & WD Black 640Gb Lite On Vista Business and VMWare Ubuntu Acer AL2223W 22" 
KeyboardPowerCaseMouse
Ergonomic 4000 Corsair HX 620W CM 690 G5 
  hide details  
Reply
It's about time!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 @3.3Ghz Gigabyte EP45-UD3P Sapphire 2900Pro Flashed to XT 4Gb Gskill 1066Mhz PK's 
Hard DriveOptical DriveOSMonitor
Seagate Barracuda 320Gb & WD Black 640Gb Lite On Vista Business and VMWare Ubuntu Acer AL2223W 22" 
KeyboardPowerCaseMouse
Ergonomic 4000 Corsair HX 620W CM 690 G5 
  hide details  
Reply
post #9 of 23
Thread Starter 
Quote:
Originally Posted by kdbolt70 View Post
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.
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
GAMR
(15 items)
 
  
CPUMotherboardGraphicsGraphics
6600K ASUS Z170-A ASUS 970 Strix ASUS 970 Strix 
RAMHard DriveHard DriveHard Drive
Crucial Ballistix Elite Samsung 850 EVO Intel 320 6400AAKS 
Hard DriveCoolingMonitorPower
6400AAKS Cooler Master Hyper 212 EVO ASUS VG24QE EVGA SuperNova 650 G2 
CaseMouseMouse Pad
Antec 1200 Razer Abyssus 3.5G QCK+ 
  hide details  
Reply
post #10 of 23
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:
#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
It's about time!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 @3.3Ghz Gigabyte EP45-UD3P Sapphire 2900Pro Flashed to XT 4Gb Gskill 1066Mhz PK's 
Hard DriveOptical DriveOSMonitor
Seagate Barracuda 320Gb & WD Black 640Gb Lite On Vista Business and VMWare Ubuntu Acer AL2223W 22" 
KeyboardPowerCaseMouse
Ergonomic 4000 Corsair HX 620W CM 690 G5 
  hide details  
Reply
It's about time!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 @3.3Ghz Gigabyte EP45-UD3P Sapphire 2900Pro Flashed to XT 4Gb Gskill 1066Mhz PK's 
Hard DriveOptical DriveOSMonitor
Seagate Barracuda 320Gb & WD Black 640Gb Lite On Vista Business and VMWare Ubuntu Acer AL2223W 22" 
KeyboardPowerCaseMouse
Ergonomic 4000 Corsair HX 620W CM 690 G5 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Temporary Programming Challanges!