Login to TechLifeForum X
Board Index Go to Reboot.Pro
  • Account Login
  • Register
TechLifeForum
  • Home
  • Members
  • Awards
  • Rules
  • Help
  • Donate
  • Live IRC
TechLifeForum / Programming and Development / General Programming Discussion v
« Previous 1 ... 4 5 6 7 8 ... 13 Next »
/  Programming Challenge #7[Advanced Search]
« Previous 1 2
Reply to thread
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

[Announcement] Programming Challenge #7

07-06-2012, 05:48 AM
Post: #11
RDCA Offline
Senior Member
***
Posts: 278
Joined: Jun 2011
Reputation: 9
Youtube Video Award
RE: Programming Challenge #6
Application Name: Roman Numeral Converter.
Language Used: C++
Description: Converts the wrong way, still need to fix something. Maybe one of you can find it. Large numbers work to a certain extent, but then they flop. I am sure I will figure it out tomz lol.
Level of Completion: 3 except for I made a mistake somewhere. Plus it is the wrong damn way lol.
Preview:
Spoiler (Click to View)
Source:

Code: (SELECT ALL CODE)
#include

using namespace std;

int main()
{
    //Just using this as a test number, going to set the value with cin later.
    int numberToBeConverted;
    cin >> numberToBeConverted;


    //Roman numerals, and there corresponding values
     int romanNumeralValues [] = {1, 5, 10, 50, 100, 500, 1000};
     char  romanNumerals [] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};


//Looping through each value, to determine the next numeral needed if any.
for (int r = 6; r > -1; r--){


if (numberToBeConverted != 0){  //Check to see if we need to do anything at all.




        //Checking to see if the same Roman numeral repeats more than three times, if so then write the current value, and a larger value after it.
        if (numberToBeConverted / romanNumeralValues[r] > 3){
            if (r == 6 && numberToBeConverted % romanNumeralValues[r] == 0){
            int timesToPrint = numberToBeConverted / 1000;

            for (int d = 0; d < timesToPrint; d++){

                cout << "M";
            }

                numberToBeConverted-= timesToPrint * 1000;

            }else{

            cout << romanNumerals[r];
            cout << romanNumerals[r + 1];
            numberToBeConverted-= (romanNumeralValues[r + 1] - romanNumeralValues[r]);
            }

        }
        else{

        //Number of times we need to print the same Roman numeral, which cannot be over three.

        int counter = numberToBeConverted / romanNumeralValues[r];
        numberToBeConverted-= counter * romanNumeralValues[r];

        for (int i = 0; counter > i; counter--){

            cout<< romanNumerals[r];
       }




    }

}
}
}


Virus Scan: No download link.
Download: nop.
Find
Reply
07-06-2012, 09:56 AM
Post: #12
KoBE Offline
¯\_(ツ)_/¯
*******
Administrators
Posts: 4,156
Joined: Jun 2011
Reputation: 47
RE: Programming Challenge #6
Looking through the code it looks pretty good. How do the larger numbers flop? I may attempt to go from numeric to Roman Numeral too.

-> TechLifeForum on Facebook<- 
-> TechLifeForum on YouTube<-
-> TechLifeForum on Twitter<-

Tech.Reboot.Pro
WWW Find
Reply
07-06-2012, 01:57 PM (This post was last modified: 07-06-2012 02:20 PM by RDCA.)
Post: #13
RDCA Offline
Senior Member
***
Posts: 278
Joined: Jun 2011
Reputation: 9
Youtube Video Award
RE: Programming Challenge #6


I'll try to fix it later. Just woke up.


Edit: looool, I love sleeping. I looked at the code, and fixed it immediately.

Code: (SELECT ALL CODE)
#include

using namespace std;

int main()
{
    //Just using this as a test number, going to set the value with cin later.
    int numberToBeConverted;
    cin >> numberToBeConverted;


    //Roman numerals, and there corresponding values
     int romanNumeralValues [] = {1, 5, 10, 50, 100, 500, 1000};
     char  romanNumerals [] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};


//Looping through each value, to determine the next numeral needed if any.
for (int r = 6; r > -1; r--){


if (numberToBeConverted != 0){  //Check to see if we need to do anything at all.




        //Checking to see if the same Roman numeral repeats more than three times, if so then write the current value, and a larger value after it.
        if (numberToBeConverted / romanNumeralValues[r] > 3){
            if (r == 6){
            int timesToPrint = numberToBeConverted / 1000;

            for (int d = 0; d < timesToPrint; d++){

                cout << "M";
            }

                numberToBeConverted-= timesToPrint * 1000;

            }else{

            cout << romanNumerals[r];
            cout << romanNumerals[r + 1];
            numberToBeConverted-= (romanNumeralValues[r + 1] - romanNumeralValues[r]);
            }

        }
        else{

        //Number of times we need to print the same Roman numeral, which cannot be over three.

        int counter = numberToBeConverted / romanNumeralValues[r];
        numberToBeConverted-= counter * romanNumeralValues[r];

        for (int i = 0; counter > i; counter--){

            cout<< romanNumerals[r];
       }




    }

}
}
}




Alright, now time to right the converse of my function. Maybe I should try to do it in ASM. Would be good practice, and we don't have ant examples on this forum I don't think.





Edited the thread, and iostream went to town adding itself everywhere. D:
Find
Reply
08-08-2012, 07:25 PM (This post was last modified: 08-18-2012 05:02 AM by Epixors.)
Post: #14
Epixors Offline
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 405
Joined: Aug 2012
Reputation: 6
Spam ProtectionVisual Basic School Graduate
RE: Programming Challenge #7
Application Name: PHPRomanNumeral
Language Used: PHP
Description: Convert roman to numeral and numeral to roman. Decided to do a simple webform for this one, and wanted to add something because I wasn't challenged by the roman to numeral. Not that the other thing was much of a challenge, but hey, still fun :D
Level of Completion: 3++ (over 1 million, I tested that, and can also do it the other way around)
Preview: img1.uploadscreenshot.com/images/orig/8/22010023778-orig.jpg (Can't post clickable links yet, seemingly includes images)
Source: It's a bit sketchy, could be better but I was writing this in a rush.

Code: (SELECT ALL CODE)
<?php

/** Roman to Numeral
Author:Epixors
For: http://tech.reboot.pro/showthread.php?tid=2930
Note: This required me to dig into my memory and look up the roman system
Level: 3++
**/


$TRANSLATION = array('M' => 1000,'CM' => 900,'CD' => 400,'D' => 500,'C' => 100,'XC' => 90,'L' => 50,'XL' => 40,'X' => 10,'IX' => 9,'V' => 5,'IV' => 4,'I' => 1);


//In here are also some checks if the input submitted is valid, you should understand that
if(isset($_POST['submit'])){

if($_POST['val'] != ''){
//So after I studied the roman stuff I found out how to replace roman values like IV, IX with easy to use roman values (IIII,VIIII,etc.)
$annoying_roman = array("IV","IX","XL","XC","CD","CM","XC");
$hooray_roman = array("IIII","VIIII","XXXX","LXXXX","CCCC","DCCCC","LXL");


$MODE = $_POST['method'];

if($MODE == "R2N"){

$ROMANSTUFF = $_POST['val'];


//This makes for valid stuff along the way of editing it and much easier to edit this in a for loop
//First thing to do is change the roman value

$ROMANSTUFF = str_replace($annoying_roman,$hooray_roman,trim($ROMANSTUFF)); //Replace it and make sure there are no pesky spaces in the input


//And now the magic happens, after replacing every char with something easy for the script

$SUM = 0; //This will hold the eventual output

for($x = 0;$x < strlen($ROMANSTUFF);$x++){

if($TRANSLATION[$ROMANSTUFF[$x+1]] > $TRANSLATION[$ROMANSTUFF[$x]]){
switch($ROMANSTUFF[$x]){
  case 'I':
   $SUM -= 1;
  break;
  case 'V':
   $SUM -= 5;
  break;
  case 'X':
   $SUM -= 10;
  break;
  case 'L':
   $SUM -= 50;
  break;
  case 'C':
   $SUM -= 100;
  break;
  case 'D':
   $SUM -= 500;
  break;
  case 'M':
   $SUM -= 1000;
  break;
}
} else {
switch($ROMANSTUFF[$x]){
  case 'I':
   $SUM += 1;
  break;
  case 'V':
   $SUM += 5;
  break;
  case 'X':
   $SUM += 10;
  break;
  case 'L':
   $SUM += 50;
  break;
  case 'C':
   $SUM += 100;
  break;
  case 'D':
   $SUM += 500;
  break;
  case 'M':
   $SUM += 1000;
  break;
}
}
}

echo "The answer is: ".$SUM;

} else if($MODE == "N2R"){

//In here we essentially do the same thing, but in reverse, we take all the numbers, convert them to roman values, make them properly and stuff.
//But due to me being lazier I'll add some extra thingies to the translation arrayt o make everything proceed well
//Well, lets just get the basic info first. And yes, I call this a translation...
//We do it in this order because when looping through the values we want to take the biggest value first.

$NUMBER = $_POST['val'];
  
$ROMANOUTPUT = "";    

foreach($TRANSLATION as $key => $value){
while($NUMBER >= $value){
  $NUMBER -= $value;
  $ROMANOUTPUT .= $key;
}
}



$ROMANOUTPUT = str_replace($hooray_roman,$annoying_roman,$ROMANOUTPUT);


echo "The answer is: ".$ROMANOUTPUT;


} else {
echo "OMG, non-existing mode! Hacks?";
}
} else {
echo "Enter some value!";
}
}

echo "<form method='POST'>
Method:<select name='method'>
<option value='R2N'>Roman to Number</option>
<option value='N2R'>Number to Roman</option>
</select><br/>
Input: <input type='text' name='val'/>
<input type='submit' name='submit'/>
</form>";


?>


Please don't mind my comments, I like to comment on them without acting really serious.
Virus Scan: virustotal.com/file/a300a54ae872e61984a5156f0df23f759fe1e935be4c0bd0139924ee3c1f138d/analysis/1344467525/ (Can't post clickable links yet)
Download: filehost.ws/vf15uiga0ogj (Can't post clickable links yet, outdated so just copy the PHP code please)

EDIT: Updated the PHP Code, I did something wrong but that's correct now, for those of you wondering why the converting from number to roman displays something like 99 as XCIX instead of IC, it's because that actually is correct. The download is outdated, but the code is updated, sorry for that, but I don't have time to upload it atm.
Find
Reply
08-08-2012, 07:36 PM
Post: #15
AceInfinity Online
∞ IҊϜIɴITϵ ☭
*****
Microsoft MVP
Posts: 8,611
Joined: Jun 2011
Reputation: 60
RE: Programming Challenge #7
Nice, a PHP submission for the challenge :) Keep up the good work.



Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer


Development Site: aceinfinity.net
WWW Find
Reply
08-08-2012, 07:39 PM
Post: #16
Epixors Offline
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 405
Joined: Aug 2012
Reputation: 6
Spam ProtectionVisual Basic School Graduate
RE: Programming Challenge #7
Thanks Ace :) Isn't the best it could be but it's good enough for me currently, am in a bit of a lazy mode ;)
Find
Reply
08-08-2012, 11:56 PM
Post: #17
KoBE Offline
¯\_(ツ)_/¯
*******
Administrators
Posts: 4,156
Joined: Jun 2011
Reputation: 47
RE: Programming Challenge #7
Seems as though you forgot to include the form to enter the data... :P

-> TechLifeForum on Facebook<- 
-> TechLifeForum on YouTube<-
-> TechLifeForum on Twitter<-

Tech.Reboot.Pro
WWW Find
Reply
08-09-2012, 04:04 AM (This post was last modified: 08-09-2012 05:18 AM by Epixors.)
Post: #18
Epixors Offline
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 405
Joined: Aug 2012
Reputation: 6
Spam ProtectionVisual Basic School Graduate
RE: Programming Challenge #7
Didn‘t I copy that? My mistake xD I‘ll edit it when ‘m on my comp.

EDIT: Updated.
Find
Reply
« Previous 1 2
Reply to thread


Possibly Related Threads...

Thread: Author Replies: Views: Last Post
  Sphere Online Judge (SPOJ) - Programming Challenges AceInfinity 0 5 Today 05:36 PM
Last Post: AceInfinity
  ACM-ICPC - Programming Competition KoBE 16 430 Today 05:17 PM
Last Post: AceInfinity
  Programming Challenge Site - Al Zimmermann's AceInfinity 2 175 04-23-2013 01:56 AM
Last Post: AceInfinity
  TreasureChest Challenge AceInfinity 2 172 04-16-2013 12:27 AM
Last Post: AceInfinity
  Lawnmower Programming Problem - Google AceInfinity 1 179 04-14-2013 05:27 PM
Last Post: AceInfinity

  • View a Printable Version
  • Send this Thread to a Friend
  • Subscribe to this thread
Forum Jump:


Users browsing this thread
1 Guest(s)
Youtube Facebook Twitter Digg
Return to Top
All content © copyright TechLifeForum
Powered By MyBB, © 2002-2013 MyBB Group
Designed by ThemeFreak
Mobile Version