Skip to content

Instantly share code, notes, and snippets.

@Surgo
Created February 23, 2011 04:16
Show Gist options
  • Save Surgo/840009 to your computer and use it in GitHub Desktop.
Save Surgo/840009 to your computer and use it in GitHub Desktop.
SRM481 - div2 - level two
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""SRM481 - div2 - level two
This time, instead of solving an easy problem with a
known solution, you will be in charge in solving an
old problem with a solution which was unknown to
this date. The old question is whether the egg or
the chicken came first. This question has been very
difficult to answer over the ages, but a chance has
finally come: It is said that a new oracle has
appeared which knows everything.
You tried asking the question to the oracle, but the
oracle refused to answer, arguing that it has already
answered the question to n other people and is tired
of answering that question. Not to give up, you decided
to interview each of the n people. Of them, eggCount
people told you that the answer was "The egg", while
the remaining n-eggCount people claimed that the answer
was "The chicken". Confused by the results, you asked
the oracle a second time. The oracle still refused to
answer the question, but instead explained the results:
In order to hide the truth to those unworthy, the oracle
has intentionally given the wrong answer to exactly
lieCount people. On the other hand, also to avoid
sharing the secret, exactly liarCount people (not
necessarily those that were told the right answer by
the oracle) have intentionally given you the opposite
answer to the one given to them by the oracle.
You are still suspiscious that the explanation given by
the oracle is another lie. Given ints n, eggCount,
lieCount and liarCount, find out if scenarios exist such
that "The egg" or "The chicken" is the correct answer. If
there exist scenarios such that either answer is correct,
return "Ambiguous" (quotes for clarity). If only one answer
has a possible scenario, return "The egg" or "The chicken"
(quotes for clarity) depending on the answer. If neither
of the answers has a possible scenario, return "The oracle
is a lie".
Definition:
Class: ChickenOracle
Method: theTruth
Parameters: int, int, int, int
Returns: String
Method signature: String theTruth
(int n, int eggCount, int lieCount, int liarCount)
"""
class ChickenOracle(object):
"""Chicken Oracle
Constraints:
- n will be between 1 and 1000000, inclusive.
- eggCount, lieCount and liarCount will each be between 0 and n, inclusive.
>>> chickenoracle = ChickenOracle()
>>> print chickenoracle.theTruth(10, 10, 0, 0)
The egg
>>> print chickenoracle.theTruth(60, 40, 0, 3)
The oracle is a lie
>>> print chickenoracle.theTruth(60, 20, 5, 25)
The chicken
>>> print chickenoracle.theTruth(1000, 500, 250, 250)
Ambiguous
"""
def __init__(self):
pass
def theTruth(self, n, eggCount, lieCount, liarCount):
egg = False
chicken = False
if (eggCount + liarCount + lieCount - n) % 2 == 0:
e = (eggCount + liarCount+lieCount - n) / 2
if e >= 0 \
and e <= liarCount \
and e <= eggCount \
and liarCount-e <= n-eggCount:
egg = True
if (eggCount + liarCount - lieCount) % 2 == 0:
c = (eggCount + liarCount - lieCount) / 2
if c >= 0 \
and c <= liarCount \
and c <= eggCount \
and liarCount-c <= n-eggCount:
chicken = True
if egg and chicken: return "Ambiguous"
if egg: return "The egg"
if chicken: return "The chicken"
return "The oracle is a lie";
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment