Quite easy if you closely read the question. My solution is in Python:
Spoiler:
Results 1 to 5 of 5
-
01-15-2012 02:51 AM #1
- Join Date
- Oct 2009
- Posts
- 1,017
- Rep
- 255
Interviewstreet CS 2 - Coin Tosses
02-23-2012 05:37 AM #2
Grave digging?
Here is my solution in C#. I spent about 20 minutes trying to make the code smaller but I couldn't seem to get it any smaller then what it was without making the variables names smaller of course.
I don't even think this is right. I didn't even follow the rules correctly (disqualified)
Any who, continues to flip the "Coin" until you get heads twice in a row.PHP Code:static void Main(string[] args)
{
int conHeads = 0, numThrows = 0, landHeads = 0;
while(conHeads < 2)
{
Thread.Sleep(new Random().Next(10, 75));
if (GoThrow() == true)
{
conHeads++; landHeads++;
}
else
{
conHeads = 0;
}
//conHeads = (GoThrow() == true ? conHeads += 1 : conHeads = 0);
//Wouldn't work with conHeads++... ^without times it landed on heads.
numThrows++;
}
Console.WriteLine("Number of throws: " + numThrows);
Console.WriteLine("Times landed on heads: " + landHeads);
Decimal avg = numThrows / landHeads;
Console.WriteLine("Average?: " + Math.Round(avg, 2));
Console.ReadLine();
}
static Boolean GoThrow()
{
// 0 = heads.
return (new Random().Next(0, 2)) == 0 ? true : false;
}
02-23-2012 02:35 PM #3Je suis le professeur de français à Thomas
- Join Date
- May 2010
- Location
- France, Paris
- Posts
- 1,846
- Rep
- 148
02-23-2012 08:48 PM #4
02-24-2012 09:22 PM #5
- Join Date
- Oct 2009
- Posts
- 1,017
- Rep
- 255
You are correct in that your code is incorrect
Tags for this Thread



Reply With Quote



, never thought of it xD, I like how you keep trying to make the code smaller every time and everywhere so ima try to do that too which helps you to understand programming better and learn different ways of doing different things

Bookmarks