示例#1
0
def bottles_of_beer(start=99, takedown=1):
    """Print the 'ninety-nine bottles of beer' song with arbitrary numbers.

    This function prints the famous beer bottle song. You can make the song start at any number
    you want, and you can have it take down as many bottles as you want in every verse. The song
    stops when the amount of bottles left on the wall is smaller than the amount of bottles
    taken down.

    :param int start: the amount of beer bottles that are on the wall at the start of the song
                      (default 99)
    :param int takedown: the amount of beer bottles that are taken down in every verse (default 1)
    """
    verse = ("{before} bottle{bmul} of beer on the wall, {before} bottle{bmul} of beer\n"
             "you take {takedown} down, pass {them} around,\n"
             "{after} bottle{amul} of beer on the wall.\n")
    takedown_ = ei.EnhancedInt(takedown).as_words()
    for ib in range(start, 0, -takedown):
        if ib < takedown:
            break
        verse_ = verse.format(
            before=ei.EnhancedInt(ib).as_words(),
            bmul = ('s', '')[ib==1],
            takedown=takedown_,
            them=('it', 'them')[takedown > 1],
            after=ei.EnhancedInt(ib-takedown).as_words(),
            amul = ('s', '')[ib-takedown==1],
        )
        print(verse_)
示例#2
0
def run():
    """Execute the challenges.031e module."""
    nr1 = utils.get_input('First base 26 number (A - Z): ').upper()
    nr2 = utils.get_input('Second base 26 number (A - Z): ').upper()
    nr1 = ei.EnhancedInt([map_ltrs2nrs[char] for char in nr1], 26)
    nr2 = ei.EnhancedInt([map_ltrs2nrs[char] for char in nr2], 26)
    result = ''.join([map_nrs2ltrs[digit] for digit in (nr1 * nr2).digits])
    print("Multiplication of both numbers: {}".format(result))
    def test___str__(self):
        """Test method :meth:`plugins.enhancedint.EnhancedInt.__str__`

        **Tested:**

        - The returned string is correct for a number with base smaller than or equal to 10.
        - The returned string is correct for a number with base higher than 10.
        """
        str_ = enhancedint.EnhancedInt(123, base=4)
        self.assertEqual(str_.__str__(), '123 (b4)')

        str_ = enhancedint.EnhancedInt('12:36:78', base=100)
        self.assertEqual(str_.__str__(), '12:36:78 (b100)')
    def test_as_words(self):
        """Test method :meth:`plugins.enhancedint.EnhancedInt.as_words`

        **Tested:**

        - The returned string is correct.
        """
        int_ = enhancedint.EnhancedInt('125:63:7:0', base=150)
        expected = ("four hundred twenty-three million two hundred ninety-three thousand "
                    "five hundred fifty")
        self.assertEqual(int_.as_words(), expected)

        int_ = enhancedint.EnhancedInt(0)
        expected = 'zero'
        self.assertEqual(int_.as_words(), expected)
示例#5
0
def run():
    """Execute the challenges.046e module."""
    while True:
        nr = utils.get_input("Give me a number ('q' to quit) > ").lower()
        if nr == 'q':
            break
        nr = int(nr)
        print(ei.EnhancedInt(nr).convert(2), ';', population_count(nr))
    def test_base10(self):
        """Test method :meth:`plugins.enhancedint.EnhancedInt.base10`

        **Tested:**

        - The returned integer is correct.
        """
        int_ = enhancedint.EnhancedInt('125:63:7:0', base=150)
        self.assertEqual(int_.base10(), 423293550)
    def test_convert(self):
        """Test method :meth:`plugins.enhancedint.EnhancedInt.convert`

        **Tested:**

        - The returned converted enhanced integer is correct.
        """
        int_ = enhancedint.EnhancedInt('125:63:7:0', base=150)
        result = int_.convert(6)
        self.assertEqual(result.digits, [1, 1, 0, 0, 0, 0, 3, 5, 2, 2, 1, 0])
        self.assertEqual(result.base, 6)
    def test___mul__(self):
        """Test method :meth:`plugins.enhancedint.EnhancedInt.__mul__`

        **Tested:**

        - The returned object is an enhanced integer.
        - The returned enhanced integer is correct.
        """
        int1 = enhancedint.EnhancedInt(25)
        int2 = enhancedint.EnhancedInt(3)
        mul = int1*int2
        self.assertEqual(type(mul), enhancedint.EnhancedInt)
        self.assertEqual(mul.digits, [7, 5])
        self.assertEqual(mul.base, 10)

        int1 = enhancedint.EnhancedInt(25, base=6)
        int2 = enhancedint.EnhancedInt('125:63:7:0', base=150)
        mul = int1*int2
        self.assertEqual(type(mul), enhancedint.EnhancedInt)
        self.assertEqual(mul.digits, [3, 1, 5, 0, 0, 1, 5, 0, 1, 4, 0, 5, 0])
        self.assertEqual(mul.base, 6)
    def test___valid__(self):
        """Test method :meth:`plugins.enhancedint.EnhancedInt.__valid__`

        **Tested:**

        - The returned boolean is correct.
        """
        int_ = enhancedint.EnhancedInt('12:56:78', base=80)
        self.assertTrue(int_.__valid__())

        int_.base = 75
        self.assertFalse(int_.__valid__())
示例#10
0
def population_count(nr):
    """Return the given number's bitstring population count.

    The population count of a number's bitstring is the amount of set bits in the number's
    bitstring. This implementation uses conversion from base 10 to base 2 using the EnhancedInt
    class, after which the sum of the base 2 number's digits is returned. It's probably not the
    fastest way to do it, but it works fast even for a 1938 bit case (see the fourth example).

    :param int nr: the number of which to calculate its bitstring's population count
    :return: the population count
    :rtype: int

    Example::

        >>> population_count(0)
        0
        >>> population_count(23)
        4
        >>> population_count(123456789)
        16
        >>> population_count(12448057941136394342297748548545082997815840357634948550739612798732309975923280685245876950055614362283769710705811182976142803324242407017104841062064840113262840137625582646683068904149296501029754654149991842951570880471230098259905004533869130509989042199261339990315125973721454059973605358766253998615919997174542922163484086066438120268185904663422979603026066685824578356173882166747093246377302371176167843247359636030248569148734824287739046916641832890744168385253915508446422276378715722482359321205673933317512861336054835392844676749610712462818600179225635467147870208)
        19
    """
    return sum(ei.EnhancedInt(nr).convert(2).digits)
    def test___init__(self):
        """Test method :meth:`plugins.enhancedint.EnhancedInt.__init__`

        **Tested:**

        - The attributes of an enhanced integer are correct after initialization with an integer.
        - The attributes of an enhanced integer are correct after initialization with an integer
          in a base smaller than 10.
        - The attributes of an enhanced integer are correct after initialization with a string.
        - The attributes of an enhanced integer are correct after initialization with a list
          of integers.
        - A TypeError is raised when initializing an enhanced integer with an integer and a base
          larger than 10.
        - A ValueError is raised when initializing an enhanced integer with digits higher than
          its base.
        """
        int_ = enhancedint.EnhancedInt(123)
        self.assertEqual(int_.digits, [1, 2, 3])
        self.assertEqual(int_.base, 10)

        int_ = enhancedint.EnhancedInt(123, base=5)
        self.assertEqual(int_.digits, [1, 2, 3])
        self.assertEqual(int_.base, 5)

        int_ = enhancedint.EnhancedInt('12:25:0', base=26)
        self.assertEqual(int_.digits, [12, 25, 0])
        self.assertEqual(int_.base, 26)

        int_ = enhancedint.EnhancedInt([124, 0, 26], base=125)
        self.assertEqual(int_.digits, [124, 0, 26])
        self.assertEqual(int_.base, 125)

        with self.assertRaises(TypeError):
            int_ = enhancedint.EnhancedInt(123, base=15)

        with self.assertRaises(ValueError):
            int_ = enhancedint.EnhancedInt('125:26:0', base=125)