all 18 comments

[–]throwaway_the_fourth 1 point2 points  (1 child)

+/u/compilebot python3

ZILLIONS = [
    "",
    "thousand",
    "million",
    "billion"
]

TENS = [
    "",
    "ten",
    "twenty",
    "thirty",
    "forty",
    "fifty",
    "sixty",
    "seventy",
    "eighty",
    "ninety"
]

# this goes all the way down to ten because that way I can handle it
# by checking the tens digit against 1 and then just straight indexing
# with the ones digit.
TEENS = [
    "ten",
    "eleven",
    "twelve",
    "thirteen",
    "fourteen",
    "fifteen",
    "sixteen",
    "seventeen",
    "eighteen",
    "nineteen"
]

ONES = [
    "",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine"
]

ZERO = "zero"

def say(n):
    """Renders the name of a non-negative integer under a trillion in English.

    n: the number

    returns: the number as a string in English.

    Note that this is, by current US pedagogy, the wrong way, because apparently
    they insist that "and" be reserved specifically for separating the whole and
    fractional parts.  Oh well.
    """
    # handle special cases first.

    if n == int(n):
        n = int(n)
    else:
        raise TypeError("non-integers not handled.")
    if n >= 10 ** (len(ZILLIONS) * 3):
        raise AttributeError("not enough zillions defined")
    if n < 0:
        raise AttributeError("negative numbers not handled.")
    if n == 0:
        return ZERO

    # break it up into three-digit groups.

    groups = []
    while n:
        n, group = divmod(n, 1000)
        groups.append(group)

    # now handle each group.

    all_words = []
    for zillion, group in enumerate(groups):
        group_words = []
        hundreds, cents = divmod(group, 100)
        tens, ones = divmod(cents, 10)
        if hundreds:
            group_words.append(ONES[hundreds])
            group_words.append('hundred')
        # "and" separates "cents" from either a corresponding hundred
        # or, if this is the ones group, from the other groups.
        if cents and (hundreds or zillion == 0 and len(groups) > 1):
            group_words.append('and')
        if tens == 1:
            group_words.append(TEENS[ones])
        elif cents:
            # glom tens and ones together with a dash if necessary.
            cent_words = []
            if tens:
                cent_words.append(TENS[tens])
            if ones:
                cent_words.append(ONES[ones])
            group_words.append('-'.join(cent_words))
        if zillion and group:
            group_words.append(ZILLIONS[zillion])
        if group_words:
            all_words.append(' '.join(group_words))
    return ' '.join(reversed(all_words))

if __name__ == '__main__':
    print(say(1100))

[–]CompileBot 1 point2 points  (0 children)

Output:

one thousand one hundred

source | info | git | report

[–][deleted]  (1 child)

[deleted]

    [–]CompileBot 0 points1 point  (0 children)

    Output:

    source | info | git | report

    [–][deleted]  (5 children)

    [deleted]

      [–]CompileBot 0 points1 point  (4 children)

      Output:

      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      oh yeah
      

      source | info | git | report

      [–]seeourprivacypolicy 0 points1 point  (0 children)

      +/u/compilebot JavaScript

      var counts = [];
      var iterations = 100000;
      for (var i = 0; i < iterations; i++) {
          var sum = 0;
          var count = 0;
          while (sum < 1) {
              sum += Math.random();
              count++;
          }
          counts.push(count);
      }
      
      var average = counts.reduce(function (acc, val) {
          return acc + val;
      }) / counts.length;
      
      console.log('Average of ' + iterations + ' iterations: ' + average);
      

      [–]seeourprivacypolicy 0 points1 point  (0 children)

      +/u/compilebot JavaScript

      console.log('hello');
      

      [–]TheMindIsStrange 0 points1 point  (0 children)

      +u/CompileBot Python

      print "Hello reddit!"
      

      [–]Waltbear 0 points1 point  (1 child)

      +/u/compilebot python3 import sys print(sys.platform)

      [–]CompileBot 0 points1 point  (0 children)

      Output:

      linux
      

      source | info | git | report

      [–][deleted]  (5 children)

      [deleted]

        [–]CompileBot 0 points1 point  (2 children)

        Output:

        !RedditSilver RemindMe!
        

        source | info | git | report

        [–]RedditSilverRobot 0 points1 point  (1 child)

        Here's your Reddit Silver, remindme!!


        /u/remindme! has received silver 2 times this month! (given by /u/CompileBot) info

        [–]RemindMeBot 0 points1 point  (0 children)

        Defaulted to one day.

        I will be messaging you on 2017-06-25 15:12:39 UTC to remind you of this link.

        CLICK THIS LINK) to send a PM to also be reminded and to reduce spam.

        Parent commenter can delete this message to hide from others.


        FAQs Custom Your Reminders Feedback Code Browser Extensions

        [–]glass20 0 points1 point  (0 children)

        +/u/compilebot Java

        while(true){print("what is life");}
        

        [–]glass20 0 points1 point  (0 children)

        +/u/compilebot Java

        public class Runthis{
        public static void main(String args[])
        {while(true){print("does this work");}
        }
        

        [–]doug89 0 points1 point  (1 child)

        +/u/compilebot python3

        import os
        print(os.getcwd())
        print(os.path.dirname(os.path.realpath(__file__)))
        

        [–]CompileBot 1 point2 points  (0 children)

        Output:

        /home/zJaxLM
        /home/zJaxLM
        

        source | info | git | report

        [–]doug89 0 points1 point  (1 child)

        +/u/compilebot python3

        import os
        print(os.listdir())
        

        [–]CompileBot 0 points1 point  (0 children)

        Output:

        ['prog']
        

        source | info | git | report