alright. the game is,
First: post a simple request for code thats easy for anyone to google the answer to.
(changing background color.. hello world... a button... one dimensional array)
Second: enter code to solve the problem above your post(in any language you like) in the most inefficient yet correct way possible.
first request..: print hello world
the Ridiculous code game
Started by atheium, Aug 24 2010 07:42 AM
4 replies to this topic
|
|
|
#2
Posted 24 August 2010 - 02:51 PM
<?php
interface Speak
{
public function sayIt($it);
}
class Voice implements Speak {
public function sayIt($it) {
for($i = 0; $i < strlen($it); $i++) {
echo $it[$i];
}
}
}
class SayHelloWorld extends Voice
{
public function __construct() {
$hai = new Voice();
$hai->sayIt("Hello, World!");
}
}
new SayHelloWorld();
?>
Add two numbers
#3
Posted 24 August 2010 - 03:30 PM
<?php
class checkNum {
function checkNumber() {
if(is_numeric($GLOBALS['number_one']) && is_numeric($GLOBALS['number_two'])) {
$correct = "yes it is correct";
return $correct;
} else {
$wrong = "no not correct";
return $wrong;
}
}
}
class add {
function addtwonums() {
$numbers[0] = $GLOBALS['number_one'];
$numbers[1] = $GLOBALS['number_two'];
$chk = new checkNum;
foreach($numbers as $num)
if($chk->checkNumber($num) == "no not correct") trigger_error("$num is not a correct number!", E_USER_ERROR);
$count = 0;
foreach($numbers as $num) {
$count += $num;
unset($num);
}
return $count;
}
}
$number_one = (int)5.0;
$number_two = (int)-2.0;
$chk = new add;
echo $chk->addtwonums();
?>Count odd numbers from 0 to 1,000,000
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#4
Posted 24 August 2010 - 04:00 PM
<?php
class Numbers
{
private $max = 0;
public function isEven($number)
{
$isEven = false;
for($i = 0; $i <= $this->max; $i = $i+2)
{
if($number == $i)
{
$isEven = true;
}
}
return $isEven;
}
public function isOdd($number)
{
$isOdd = false;
for($i = 1; $i <= $this->max; $i = $i+2)
{
if($number == $i)
{
$isOdd = true;
}
}
return $isOdd;
}
public function setMax($max)
{
if(!is_numeric($max))
return false;
$this->max = $max;
return true;
}
public function getMax()
{
return $this->max;
}
}
$numbers = new Numbers();
$numbers->setMax(1000000);
$sum = 0;
for($i = 0; $i < $numbers->getMax(); $i++)
if($numbers->isOdd($i))
if(!$numbers->isEven($i))
$sum = $sum + $i;
echo $sum;
?>
Get the number of characters in a string
#5
Posted 28 August 2010 - 02:27 PM
class LongItem
{
size_t length() = 0;
};
class StringCounter : LongItem
{
private:
std::vector<LongItem *> characters;
public:
StringCounter(char *zvalue)
{
char *c = zvalue;
for (char *c = zvalue; *c != '\0'; ++c)
{
LongItem *newItem = new Character(*c);
characters.push_back(newItem);
}
}
size_t length() const
{
size_t num = 0;
std::vector<LongItem *>::iterator i;
for (i = characters.begin(); i < characters.end(); ++i)
num += (*i)->length();
return num;
}
~StringCounter()
{
std::vector<LongItem *>::iterator i;
for (i = characters.begin(); i < characters.end(); ++i)
delete *i;
}
};
class Character : LongItem
{
private:
std::string value_;
public:
Character(char zvalue)
{
if (zvalue.length() != 1) throw std::invalid_argument("zvalue");
value_(1, zvalue);
}
size_t length() const
{
return value_.length();
}
};
int main()
{
StringCounter counter("Hello world!");
std::cout << "The length of the string is " << counter.length();
return 0;
}Remove every occurrence of a character from a string.


Sign In
Create Account


Back to top









