Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › The Programming Challenge Master Thread
New Posts  All Forums:Forum Nav:

The Programming Challenge Master Thread - Page 6

post #51 of 263
Quote:
Originally Posted by Waffleboy View Post

Here's a recursive solution (just for fun) written in Java:
Code:
public class stars
{
        public static String printStars(String character, int max)
        {
                System.out.println(character);
                if (character.length() < max)
                {
                        printStars(character + character.substring(0, 1), max--);       
                }
                System.out.println(character);
                return character;
        }

        public static void main(String[] args)
        {
                printStars("*", 5);
        }
}

I'm trying to think of a good way to not print the max number of stars twice. Obviously I could have an if statement around the first println, but I'd rather have a better solution. Any suggestions?

Edit: formatting

My solution uses recursion and only prints the max line once.
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #52 of 263
Quote:
Originally Posted by superhead91 View Post

My solution uses recursion and only prints the max line once.

^ that

In fact I'm not entirely sure how you'd solve this without recursion, short of hardcoding each line.
post #53 of 263
Code:
x = int(raw_input("What's your maximum?"))
for j in range(1,x+1):
    print j*"*"
while x>1:
    x = x-1
    print x*"*"

Learning Python, is there anyway to count DOWN in a for loop?


EDIT:

Changed mine to make sure input is good...
Code:
x = int(raw_input("What's your maximum? "))
while x<=0:
    print "That number isn't sane!"
    x = int(raw_input("What's your maximum? "))
for j in range(1,x+1):
    print j*"*"
while x>1:
    x = x-1
    print x*"*"

Edited by Jaffa Cakes! - 11/25/11 at 2:58am
lulwut
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K GigaByte P67A-UD3P-B3 Sapphire Radeon HD5850 1GB 4x2GB Corsair 1600MHz 
Hard DriveOptical DriveOSMonitor
1x 1TB & 1x 250GB Lite-On SATA DVD-RW Windows 7 Professional x64 Dell SP2309W @ 2048x1152 
KeyboardPowerCaseMouse
Saitek Eclipse II Corsair HX620w CM-690 Logitech MX518 
  hide details  
Reply
lulwut
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K GigaByte P67A-UD3P-B3 Sapphire Radeon HD5850 1GB 4x2GB Corsair 1600MHz 
Hard DriveOptical DriveOSMonitor
1x 1TB & 1x 250GB Lite-On SATA DVD-RW Windows 7 Professional x64 Dell SP2309W @ 2048x1152 
KeyboardPowerCaseMouse
Saitek Eclipse II Corsair HX620w CM-690 Logitech MX518 
  hide details  
Reply
post #54 of 263
Thread Starter 
Hey guys good job doing the challenge!

I have gone over most of your codes and they seem fine.

As suggested by a member above, I will try to make a spreadsheet that includes all of you guys who have done the challenge, the language(s) you used and any other thing that I may consider appropriate.

I'll be posting the next challenge, hopefully, by tomorrow so stay tuned. It will obviously be slightly harder; also, as suggested by someone, I will try to incorporate an extra goal within that task which maybe something like focussing on better efficiency, brevity, commenting, good structuring (dividing it into modules of methods so that it is easy to understand and customise), etc.


Keep me posted with your feedback.

And those guys who have voted for contribution by helping me organise this challenge may PM me or post there suggestions here.

Thank you.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #55 of 263
Quote:
Originally Posted by superhead91 View Post

My solution uses recursion and only prints the max line once.

Ah yup, I only skimmed the other solutions and missed that.
Quote:
Originally Posted by Plan9 View Post

In fact I'm not entirely sure how you'd solve this without recursion, short of hardcoding each line.

I'm not quite sure what you mean by this...most solutions here did not use recursion. For example, here it is iteratively. I used an array because I don't think anyone else did it like this, and I wanted to do a slightly different solution:
Code:
public class stars2
{
        public static void main(String[] args)
        {
                int max = 5;
                String character = "*";
                String[] printing = new String[max * 2 - 1];
                String currentLine = "";
                
                for (int i = 1; i <= max; i++)
                {
                        for (int j = 0; j < i; j++)
                        {
                                currentLine += character;                               
                        }
                        printing[i - 1] = currentLine;
                        printing[printing.length - i] = currentLine;
                        currentLine = "";
                }

                for (int i = 0; i < printing.length; i++)
                {
                        System.out.println(printing[i]);
                }
        }
}
Workstation
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel i7 920 c0 @ 3.50 Asus Sabertooth X58 Nvidia gtx 570 Nvidia gtx 210 
RAMHard DriveOptical DriveOptical Drive
12 GB (Patriot 4GB DDR3 1600 Mhz + G.Skill Ripj... OCZ Vertex II 60GB + x2 WD 1TB + WD 500 GB Lite-On DVD Burner LG Blu Ray Burner 
OSMonitorMonitorMonitor
Windows 8 Professional x64, Arch Linux x64 Samsung 22 inch 1920x1080 60Hz Asus 23 inch 1920x1080 IPS Acer 19 inch 1600x900 
KeyboardPowerCaseMouse
Logitech g11 Corsair 750 Watt NZXT Tempest Razer deathadder 3500 dpi 
Mouse PadAudioAudio
OCZ Audigy SE Sony MDR-V6 
  hide details  
Reply
Workstation
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel i7 920 c0 @ 3.50 Asus Sabertooth X58 Nvidia gtx 570 Nvidia gtx 210 
RAMHard DriveOptical DriveOptical Drive
12 GB (Patriot 4GB DDR3 1600 Mhz + G.Skill Ripj... OCZ Vertex II 60GB + x2 WD 1TB + WD 500 GB Lite-On DVD Burner LG Blu Ray Burner 
OSMonitorMonitorMonitor
Windows 8 Professional x64, Arch Linux x64 Samsung 22 inch 1920x1080 60Hz Asus 23 inch 1920x1080 IPS Acer 19 inch 1600x900 
KeyboardPowerCaseMouse
Logitech g11 Corsair 750 Watt NZXT Tempest Razer deathadder 3500 dpi 
Mouse PadAudioAudio
OCZ Audigy SE Sony MDR-V6 
  hide details  
Reply
post #56 of 263
Improved my version and included the command line parameters:
http://ideone.com/HefW8
Code:
public class Main {
    public static void main(String[] args) {
        int count;
        String symbol;

        if (args.length != 2) {
            count = 5;
            symbol = "#";
        } else {
            count = Integer.parseInt(args[0]);
            symbol = args[1];
        }
        
        for (int i=1;i<count*2;i++) {
            int print = (count-Math.max(count-i, i-count))%count;
            if (print == 0) 
                print = count;
            for (int j=0;j<print;j++)
                System.out.print(symbol);
            System.out.println();
        }
    }
}
Footrest
(17 items)
 
server
(17 items)
 
HTPC
(12 items)
 
CPUMotherboardGraphicsRAM
2 x Xeon L5520 Supermicro X8DTL-i HD4550 Samsung 6x4GB ECC 
Hard DriveHard DriveCoolingOS
3 x 2TB WD Green 160GB WD Blue Hyper 212+ and EVO ESXi 
OSOSOSPower
unRaid pfSense Ubuntu CX430v2 
CaseOtherOther
CM Elite 331 TP-Link TG-3269 Sil3512 Sata controller 
CPUMotherboardRAMHard Drive
i3-2100T Asus P8H61-I Geil Black Dragon 1333 2x2GB PXE Network boot 
Optical DriveCoolingOSMonitor
OEM Slim DVD Burner Scythe Big-Shuriken OpenElec Panasonic TX-P42G30E 42" HDTV 
KeyboardCase
Lenovo n5901a Silverstone SG-05 
  hide details  
Reply
Footrest
(17 items)
 
server
(17 items)
 
HTPC
(12 items)
 
CPUMotherboardGraphicsRAM
2 x Xeon L5520 Supermicro X8DTL-i HD4550 Samsung 6x4GB ECC 
Hard DriveHard DriveCoolingOS
3 x 2TB WD Green 160GB WD Blue Hyper 212+ and EVO ESXi 
OSOSOSPower
unRaid pfSense Ubuntu CX430v2 
CaseOtherOther
CM Elite 331 TP-Link TG-3269 Sil3512 Sata controller 
CPUMotherboardRAMHard Drive
i3-2100T Asus P8H61-I Geil Black Dragon 1333 2x2GB PXE Network boot 
Optical DriveCoolingOSMonitor
OEM Slim DVD Burner Scythe Big-Shuriken OpenElec Panasonic TX-P42G30E 42" HDTV 
KeyboardCase
Lenovo n5901a Silverstone SG-05 
  hide details  
Reply
post #57 of 263
Quote:
Originally Posted by Waffleboy View Post

Ah yup, I only skimmed the other solutions and missed that.
I'm not quite sure what you mean by this...most solutions here did not use recursion. For example, here it is iteratively. I used an array because I don't think anyone else did it like this, and I wanted to do a slightly different solution:
Code:
public class stars2
{
public static void main(String[] args)
{
int max = 5;
String character = "*";
String[] printing = new String[max * 2 - 1];
String currentLine = "";

for (int i = 1; i <= max; i++)
{
for (int j = 0; j < i; j++)
{
currentLine += character;
}
printing[i - 1] = currentLine;
printing[printing.length - i] = currentLine;
currentLine = "";
}
for (int i = 0; i < printing.length; i++)
{
System.out.println(printing[i]);
}
}
}

Oh I think I get what you mean - my code does the same too then smile.gif (except rather than creating an array in memory, I'm using one of Perls string functions - but it amounts to the same that I'm not iterating through the length of the char)
post #58 of 263
Worked out how to count down in a for loop, changed mine a bit.
Code:
x = int(raw_input("What's your maximum? "))
while x<=0:
    print "That number isn't sane!"
    x = int(raw_input("What's your maximum? "))
for j in range(1,x+1):
    print j*"*"
for k in range(x-1,0,-1):
    print k*"*"
lulwut
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K GigaByte P67A-UD3P-B3 Sapphire Radeon HD5850 1GB 4x2GB Corsair 1600MHz 
Hard DriveOptical DriveOSMonitor
1x 1TB & 1x 250GB Lite-On SATA DVD-RW Windows 7 Professional x64 Dell SP2309W @ 2048x1152 
KeyboardPowerCaseMouse
Saitek Eclipse II Corsair HX620w CM-690 Logitech MX518 
  hide details  
Reply
lulwut
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K GigaByte P67A-UD3P-B3 Sapphire Radeon HD5850 1GB 4x2GB Corsair 1600MHz 
Hard DriveOptical DriveOSMonitor
1x 1TB & 1x 250GB Lite-On SATA DVD-RW Windows 7 Professional x64 Dell SP2309W @ 2048x1152 
KeyboardPowerCaseMouse
Saitek Eclipse II Corsair HX620w CM-690 Logitech MX518 
  hide details  
Reply
post #59 of 263
Any ideas on when the next challenge will be out?
i7 on the cheap
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 970 Gigabyte X58A-UD7 evga GTX470 Corsair Vengence 
Hard DriveOptical DriveOSMonitor
Intel X25-M 160GB LG GBW-H20L Windows 7 Ultimate Acer GD235HZ 23.6" 120HZ 
KeyboardPowerCaseMouse
Logitech G15 Rocketfish 900W 80+ Silver Corsair Obsidian 800D Cyborg Rat 7 
  hide details  
Reply
i7 on the cheap
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 970 Gigabyte X58A-UD7 evga GTX470 Corsair Vengence 
Hard DriveOptical DriveOSMonitor
Intel X25-M 160GB LG GBW-H20L Windows 7 Ultimate Acer GD235HZ 23.6" 120HZ 
KeyboardPowerCaseMouse
Logitech G15 Rocketfish 900W 80+ Silver Corsair Obsidian 800D Cyborg Rat 7 
  hide details  
Reply
post #60 of 263
Thread Starter 
Quote:
Originally Posted by Dman View Post

Any ideas on when the next challenge will be out?

Sorry for the late update but I will try to post the new challenge within the next 12 or so hours. thumb.gif

Anyone willing to do the spreadsheet for me. tongue.gif
Edited by {Unregistered} - 12/3/11 at 8:19am
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  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 › The Programming Challenge Master Thread