PDA

View Full Version : My Boredom



Echo_
04-30-2012, 04:05 PM
Since I'm bored as hell, here's a little game to play. Write a program that takes a weight and a planet and converts the weight according to how much it would be on that planet.

I wrote mine in Haskell. People should write in in different languages so that we can compare them.

module Main where

import qualified Char (toUpper)

toUpperCase :: String -> String
toUpperCase str = [Char.toUpper ch | ch <- str]

planets :: String -> Float
planets str
| planet == "MERCURY" = 0.4155
| planet == "VENUS" = 0.8975
| planet == "EARTH" = 1.0
| planet == "MARS" = 0.3507
| planet == "JUPITER" = 2.5374
| planet == "SATURN" = 1.0677
| planet == "URANUS" = 0.8947
| planet == "NEPTUNE" = 1.1794
| planet == "PLUTO" = 0.0899
| otherwise = 0.0
where planet = toUpperCase str

main :: IO ()
main = do putStrLn "Enter your weight:"
w <- getLine
let weight = read w :: Float
putStrLn "Enter a planet:"
planet <- getLine
let factor = planets planet
if factor == 0.0
then putStrLn "Unknown planet entered"
else putStrLn ("Your weight on " ++ planet ++ " is " ++ show (factor * weight))

Abu
04-30-2012, 07:42 PM
Just to let you know I really did try to write this in Pascal - failed horribly :(

Runaway
04-30-2012, 08:14 PM
This was fun :P


program PlanetWeight;

const
Planet = 'earth';
Weight = 150;

function FindWeight(Planet: string; Weight: integer): extended;
var
Factor: extended;
begin
Factor := 0;
case Lowercase(Planet) of
'mercury': Factor := 0.4155;
'venus': Factor := 0.8975;
'earth': Factor := 1.0;
'mars': Factor := 0.3507;
'jupiter': Factor := 2.5374;
'saturn': Factor := 1.0677;
'uranus': Factor := 0.8947;
'neptune': Factor := 1.1794;
'pluto': Factor := 0.0899;
end;
if (Factor = 0) then
begin
WriteLn('Planet entered incorrectly.');
Result := -1;
Exit;
end;
Result := (Factor * Weight);
end;

begin
WriteLn(IntToStr(Round(FindWeight(Planet, Weight))));
end.

Abu
04-30-2012, 08:24 PM
This was fun :P


program PlanetWeight;

const
Planet = 'earth';
Weight = 150;

function FindWeight(Planet: string; Weight: integer): extended;
var
Factor: extended;
begin
Factor := 0;
case Lowercase(Planet) of
'mercury': Factor := 0.4155;
'venus': Factor := 0.8975;
'earth': Factor := 1.0;
'mars': Factor := 0.3507;
'jupiter': Factor := 2.5374;
'saturn': Factor := 1.0677;
'uranus': Factor := 0.8947;
'neptune': Factor := 1.1794;
'pluto': Factor := 0.0899;
end;
if (Factor = 0) then
begin
WriteLn('Planet entered incorrectly.');
Result := -1;
Exit;
end;
Result := (Factor * Weight);
end;

begin
WriteLn(IntToStr(Round(FindWeight(Planet, Weight))));
end.


OMD! Mine was so similar - I just couldn't get it to compile :(

Good work though :)

Runaway
04-30-2012, 08:26 PM
OMD! Mine was so similar - I just couldn't get it to compile :(

Good work though :)

Thanks :P

Did you use extended? when an integer isn't a whole number you have to use the extended type for the script to compile it.

Abu
05-01-2012, 04:48 AM
Thanks :P

Did you use extended? when an integer isn't a whole number you have to use the extended type for the script to compile it.

:eek:

I'm tho thtupid ! :duh::duh:

Thanks I guess :redface:

ShawnjohnSJ
05-01-2012, 08:27 AM
C++, not necessarily the best code I've written though. Kinda just hacked it up.



#include <cstdlib>
#include <iostream>
#include <string>


// - Derrrrr.
#define PLANETS 9

using namespace std;

string planets[] = {"mercury", "0.4155",
"venus", "0.8975",
"earth", "1.0",
"mars", "0.3507",
"jupiter", "2.5374",
"saturn", "1.0677",
"uranus", "0.8947",
"neptune", "1.1794",
"pluto", "0.0899"
};

int main()
{
string planet;
double weight;

cout << "Enter the planet: ";
cin >> planet;
cout << "Enter your weight: ";
cin >> weight;

for(int i = 0; i < (PLANETS * 2); i += 2) {
if(planet.compare(planets[i]) == 0) {
cout << "Your weight on " << planet << " is " << (weight * atof(planets[i+1].c_str())) << endl;
return 0;
}
}
cout << "You didn't enter a correct planet!" << endl;
return 0;
}

Brandon
05-01-2012, 10:02 AM
#include <string>
#include <iostream>
#include <map>

using namespace std;

string UpperCase(string Str)
{
for (unsigned short I = 0; I < Str.length(); I++)
{
Str[I] -= 'a' - 'A' ;
}
return Str;
}

int main()
{
map<string, double> Planets;

Planets.insert(std::pair<string, double>("MERCURY", 0.4155));
Planets.insert(std::pair<string, double>("VENUS", 0.8975));
Planets.insert(std::pair<string, double>("EARTH", 1.0));
Planets.insert(std::pair<string, double>("MARS", 0.3507));
Planets.insert(std::pair<string, double>("JUPITER", 2.5374));
Planets.insert(std::pair<string, double>("SATURN", 1.0677));
Planets.insert(std::pair<string, double>("URANUS", 0.8947));
Planets.insert(std::pair<string, double>("NEPTUNE", 01.1794));
Planets.insert(std::pair<string, double>("PLUTO", 0.0899));

string Input;
cout<<"Please Enter A Planet Name: \n";
cin>> Input; Input = UpperCase(Input);

map<string, double>::iterator it = Planets.find(Input);
if (it != Planets.end())
cout<<"\n\n"<<it->first<<": "<<it->second;
else
cout<<"\n\nDefault: 0.0";

cin.get();
}

ShawnjohnSJ
05-01-2012, 10:39 AM
#include <string>
#include <iostream>
#include <map>

using namespace std;

string UpperCase(string Str)
{
for (unsigned short I = 0; I < Str.length(); I++)
{
Str[I] -= 'a' - 'A' ;
}
return Str;
}

int main()
{
map<string, double> Planets;

Planets.insert(std::pair<string, double>("MERCURY", 0.4155));
Planets.insert(std::pair<string, double>("VENUS", 0.8975));
Planets.insert(std::pair<string, double>("EARTH", 1.0));
Planets.insert(std::pair<string, double>("MARS", 0.3507));
Planets.insert(std::pair<string, double>("JUPITER", 2.5374));
Planets.insert(std::pair<string, double>("SATURN", 1.0677));
Planets.insert(std::pair<string, double>("URANUS", 0.8947));
Planets.insert(std::pair<string, double>("NEPTUNE", 01.1794));
Planets.insert(std::pair<string, double>("PLUTO", 0.0899));

string Input;
cout<<"Please Enter A Planet Name: \n";
cin>> Input; Input = UpperCase(Input);

map<string, double>::iterator it = Planets.find(Input);
if (it != Planets.end())
cout<<"\n\n"<<it->first<<": "<<it->second;
else
cout<<"\n\nDefault: 0.0";

cin.get();
}


Ahhh, I knew I was forgetting something. Case sensitivity!.. Oh well, I'm too lazy to go back and edit.

Echo_
05-01-2012, 01:47 PM
Cool solutions so far :) Here's a Python version:

planets = {
'MERCURY': 0.4155,
'VENUS' : 0.8975,
'EARTH' : 1.0,
'MARS' : 0.3507,
'JUPITER': 2.5374,
'SATURN' : 1.0677,
'URANUS' : 0.8947,
'NEPTUNE': 1.1794,
'PLUTO' : 0.0899
}

def main():
weight = float(raw_input('Enter your weight: '))
planet = raw_input('Enter a planet: ').upper()

for key, value in planets.iteritems():
if key == planet:
print 'Your weight on', planet, 'is', weight * value
break
else:
print 'Unknown planet entered'


if __name__ == '__main__':
main()
raw_input()

Echo_
05-01-2012, 02:07 PM
This is how I would have approached the Simba version since Simba doesn't have hashmaps:

const
Weight = 150;
Planet = 'Earth';

type
TPlanet = record
Name: string;
Factor: Extended;
end;

TPlanetMap = array of TPlanet;

var
Planets: TPlanetMap;

procedure SetupPlanets;
begin
SetLength(Planets, 9);

Planets[0].Name := 'MERCURY';
Planets[0].Factor := 0.4155;

Planets[1].Name := 'VENUS';
Planets[1].Factor := 0.8975;

Planets[2].Name := 'EARTH';
Planets[2].Factor := 1.0;

Planets[3].Name := 'MARS';
Planets[3].Factor := 0.3507;

Planets[4].Name := 'JUPITER';
Planets[4].Factor := 2.5374;

Planets[5].Name := 'SATURN';
Planets[5].Factor := 1.0677;

Planets[6].Name := 'URANUS';
Planets[6].Factor := 0.8947;

Planets[7].Name := 'NEPTUNE';
Planets[7].Factor := 1.1794;

Planets[8].Name := 'PLUTO';
Planets[8].Factor := 0.0899;
end;

var
I: Integer;
begin
SetupPlanets;

for I := 0 to High(Planets) do
if UpperCase(Planet) = Planets[I].Name then begin
WriteLn('Your weight on ' + Planets[I].Name + ' is ' + FloatToStr(Weight * Planets[I].Factor));
Exit;
end;

WriteLn('Unknown planet entered');
end.

I wanna see more of these :p

Kyle Undefined
05-01-2012, 03:03 PM
Here's my C# version :p




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeightOnPlanetCSharp
{
class Program
{

private static Dictionary<string, decimal> _planets = new Dictionary<string, decimal>();

public static void Main(string[] args)
{
string _planet = null;
decimal _weight = default(decimal);

_planets.Add("MERCURY", Convert.ToDecimal(0.4155));
_planets.Add("VENUS", Convert.ToDecimal(0.8975));
_planets.Add("EARTH", Convert.ToDecimal(1.0));
_planets.Add("MARS", Convert.ToDecimal(0.3507));
_planets.Add("JUPITER", Convert.ToDecimal(2.5374));
_planets.Add("SATURN", Convert.ToDecimal(1.0677));
_planets.Add("URANUS", Convert.ToDecimal(0.8947));
_planets.Add("NEPTUNE", Convert.ToDecimal(1.1794));
_planets.Add("PLUTO", Convert.ToDecimal(0.0899));

Console.Write("Please Enter a Planet Name: ");
_planet = Console.ReadLine().ToUpper();

Console.Write("Please Enter Your Weight: ");
try
{
_weight = Convert.ToDecimal(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Error, defaulting weight to 0.0");
_weight = Convert.ToDecimal(0.0);
}

foreach (KeyValuePair<string, decimal> pair in _planets)
{
if (_planet == pair.Key)
{
Console.WriteLine("Your weight on " + pair.Key + " is " + Convert.ToString(_weight * pair.Value));
}
}

Console.WriteLine();
Console.Write("Press any key to Exit...");
Console.ReadLine();
}
}
}