Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge #3
New Posts  All Forums:Forum Nav:

Programming Challenge #3

post #1 of 57
Thread Starter 
Thank you to all the people who contributed with their ideas for the programming challenge.

Out of the suggestions I received, I would like to use the one given by Jtvd78.

THE CHALLENGE:

The program has to take a number (as an integer or a string) as input (or it can be coded in if you want) and return a string consisting of the number with commas inserted into the appropriate locations.

For example:

INPUT:
Code:
1009000

OUTPUT:
Code:
1,009,000

As always, the code can be in any language as long as you have the algorithm right.

I am pretty sure you understand the challenge. This one seems fairly simple but I believe it is of the correct level of difficulty for the 3rd challenge.

As we progress, future challenges will gradually increase in difficulty.

EDIT: This challenge may be easy but you should not use any built-in methods to do this automatically (like ARandomOwl did in his post). Quite obviously, it will be useless. Please actually code it yourself.

NOTE : From now on, all challenges will be posted in separate threads so that they are easy to organize and can be simply linked to in the master thread.

Also, I am including a new "feature" in the challenge this time which will also be present in all future challenges:
After all submissions have been taken in, everyone can vote for what in their opinion is the best code in terms of the simplicity (and/or efficiency) of its basic algorithm. Each challenge will have a winner with the highest number of votes and will receive a single point (shown in the spreadsheet in the master thread) for each win. If you think this is a bad idea, then simply post your opinion here.
Edited by {Unregistered} - 12/28/11 at 12:52pm
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 #2 of 57
Quote:
Originally Posted by {Unregistered} View Post

I am pretty sure you understand the challenge.

Actually, I don't. I have no idea what "appropriate locations" are for commas.

Ah, after reading your post another few times, I realise you mean thousand separators. It would help if your example was correct tongue.gif
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
post #3 of 57
PHP Solution :D (Click to show)
Code:
<?php
        $num = 1009000;
        echo number_format($num);
?>
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
post #4 of 57
Thread Starter 
Quote:
Originally Posted by ARandomOWl View Post

Actually, I don't. I have no idea what "appropriate locations" are for commas.
Ah, after reading your post another few times, I realise you mean thousand separators. It would help if your example was correct tongue.gif

LOL tongue.gif Forgive me, it's almost 2.00 am here. Fixed it.
Quote:
Originally Posted by ARandomOWl View Post

PHP Solution :D (Click to show)
Code:
<?php
        $num = 1009000;
        echo number_format($num);
?>

Now that I come to think of it, I guess this challenge is a bit too easy but your solution is definitely unacceptable. Anyone can use a built-in subroutine but you should try coding it yourself. I think I should mention this in the first post.
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 #5 of 57
sounds like a fun little challenge...i already have the algorithm in my head...i'll do it in c++ when i have time.
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #6 of 57
Quote:
Originally Posted by {Unregistered} View Post

LOL tongue.gif Forgive me, it's almost 2.00 am here. Fixed it.
Now that I come to think of it, I guess this challenge is a bit too easy but your solution is definitely unacceptable. Anyone can use a built-in subroutine but you should try coding it yourself. I think I should mention this in the first post.

biggrin.gif Ok, I did a proper one smile.gif Just a quick "thinking on the fly".
PHP Code (Click to show)
Code:
<?php
        $num = 1009000;
        $output = '';
 
        $num = strrev($num);
        for($i=0; $i < strlen($num); $i++){
                if ($i % 3 == 0 && $i != 0) $output .= ',';
                $output .= $num{$i};
        }
 
        echo strrev($output);
?>
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
post #7 of 57
Python solution! (Click to show)
Code:
numb = input('input number: ')
x = len(numb)
numb = list(numb)

while(True):
    x = x - 3
    if x < 1:
        break
    numb.insert(x, ',')
    
numb = ''.join(numb)
numb = str(numb)
print(numb)

Quote:
input number: 10000000000000000000
10,000,000,000,000,000,000

Edited code and now output is correct smile.gif
Edited by nikolauska - 12/28/11 at 1:29pm
post #8 of 57
Quote:
Originally Posted by nikolauska View Post

Python solution! (Click to show)
Code:
numb = input('input number: ')
test = len(numb)
numb = list(numb)
x = 1
while(True):
    if test > x:
        numb.insert(x, ',')
    else:
        break
    x = x + 4
numb = ''.join(numb)
numb = str(numb)
print(numb)
Quote:
input number: 100000000000000000
1,000,000,000,000,00000

something is wrong with your output there
Patawic's Rig
(14 items)
 
  
CPUMotherboardGraphicsRAM
Thuban 1055T Gigabyte 880GMA-UD2H GIGABYTE GeForce GTX 460 1GB OC Corsair 4gb 1333mhz DDR3 
Hard DriveOptical DriveCoolingOS
Samsung F4 LG CH10LS20 Bluray CoolerMaster Hyper Z600 Windows 7 
MonitorKeyboardPowerCase
Dell P4211H x 2 Logitech G110 OCZ 750W Fatal1ty HAF 912 
  hide details  
Reply
Patawic's Rig
(14 items)
 
  
CPUMotherboardGraphicsRAM
Thuban 1055T Gigabyte 880GMA-UD2H GIGABYTE GeForce GTX 460 1GB OC Corsair 4gb 1333mhz DDR3 
Hard DriveOptical DriveCoolingOS
Samsung F4 LG CH10LS20 Bluray CoolerMaster Hyper Z600 Windows 7 
MonitorKeyboardPowerCase
Dell P4211H x 2 Logitech G110 OCZ 750W Fatal1ty HAF 912 
  hide details  
Reply
post #9 of 57
Looks fun, maybe I'll do this tonight! Some recursion on the input string and checking if commas are included at index modulus 3 might work. Cheers.
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 57
Decided to work backwards with it. Entertaining. CPP (Click to show)
Code:
#include <iostream>
#include <string>
using namespace std;

int main(){
  string input, output;
  cout << "Input: ";
  getline(cin,input);
  int length = input.length();
  for(int i=1;i<=length;i++){
    if((i-1)%3 == 0 && (i-1)!=0) output = "," + output;
    output = input[length-i] + output;
  }
  cout << output;
}

Compiled with GCC, though some people seem to receive an error. They both work for me, but the above code has the suggested fix, the code in the picture is the original.
proofvmi.png

Edited by Zinxe - 12/29/11 at 2:33pm
Mine
(19 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K 4.0GHz Quad-Core Asus Z170 PRO GAMING ATX LGA1151 EVGA GeForce GTX 1070 8GB SC Gaming ACX 3.0 G.Skill Ripjaws V Series 16GB (2 x 8GB) DDR4-3200 
Hard DriveHard DriveHard DriveHard Drive
PNY CS1311 480GB SSD Barracuda 500 WD 1000 WD 1000 
CoolingOSMonitorMonitor
Thermalright TRUE Spirit 140 POWER 73.6 CFM Windows 10 Acer S277HK 27" 4K Acer 23" 1080p 
MonitorKeyboardPowerCase
Acer 23" 1080p Razer Black Widow Ultimate EVGA SuperNOVA G2 550W 80+ Gold Corsair 330R Titanium Edition ATX 
Mouse
Logitech G303 Daedalus Apex 
  hide details  
Reply
Mine
(19 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K 4.0GHz Quad-Core Asus Z170 PRO GAMING ATX LGA1151 EVGA GeForce GTX 1070 8GB SC Gaming ACX 3.0 G.Skill Ripjaws V Series 16GB (2 x 8GB) DDR4-3200 
Hard DriveHard DriveHard DriveHard Drive
PNY CS1311 480GB SSD Barracuda 500 WD 1000 WD 1000 
CoolingOSMonitorMonitor
Thermalright TRUE Spirit 140 POWER 73.6 CFM Windows 10 Acer S277HK 27" 4K Acer 23" 1080p 
MonitorKeyboardPowerCase
Acer 23" 1080p Razer Black Widow Ultimate EVGA SuperNOVA G2 550W 80+ Gold Corsair 330R Titanium Edition ATX 
Mouse
Logitech G303 Daedalus Apex 
  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 › Programming Challenge #3