A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

Interviewstreet CS 2 - Coin Tosses

1.5k views · started by Artificial ·
#1
Interviewstreet CS 2 - Coin Tosses


Quite easy if you closely read the question. My solution is in Python:
Spoiler
import sys

def probHeads(n):
if n == 0:
return 0
d = pow(2,n)
sum = 0
for x in range(1, n+1):
sum += d * x / pow(2,x)
sum += d * x / pow(2,x)

return sum

def solve(n, m):
solution = probHeads(n) - probHeads(m)
return solution if solution > 0 else 0

amount = 0
while True:
line = raw_input()
if amount == 0:
limit = line
else:
(n,m) = line.split(" ")
print "%.2f" % solve(n,m)
amount += 1

if amount == limit:
sys.exit(0)
#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)
        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;
}

Any who, continues to flip the "Coin" until you get heads twice in a row.
#3
Chad wrote:
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)
        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;
}

Any who, continues to flip the "Coin" until you get heads twice in a row.


wow that is cool :D , 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 :)
#4
I ended up getting the code down to a little under 10 lines, but I didn't think it was correct so I didn't post it. That's excluding the brackets.
#5
You are correct in that your code is incorrect :p