Problem of the Day
A new programming or logic puzzle every Mon-Fri

Lowercase A's

Happy Monday!

Over the weekend there was a discussion of bad programmers in the work force over on Hacker News. One comment suggested that several employed engineers couldn't figure out the number of lowercase A's in a string. So today's challenge is to find the number of lowercase A's in this post.

Note: you don't have to count the A's in the html for the link to the comment.

Permalink: http://problemotd.com/problem/lowercase-as/

Comments:

  • Kevin Benton - 9 years, 9 months ago

    I got 15

    from HTMLParser import HTMLParser
    import requests
    
    # create a subclass and override the handler methods
    class POTDParser(HTMLParser):
        def __init__(self):
            HTMLParser.__init__(self)
            self.inproblem = False
            self.problemdata = []
            self.number_of_open_divs = 0
    
        def handle_starttag(self, tag, attrs):
            for key, val in attrs:
                if key == 'id' and val == 'content':
                    self.inproblem = True
            if self.inproblem and tag == 'div':
                self.number_of_open_divs += 1
    
        def handle_endtag(self, tag):
            if self.inproblem and tag == 'div':
                self.number_of_open_divs -= 1
            if self.number_of_open_divs < 1:
                self.inproblem = False
    
        def handle_data(self, data):
            if self.inproblem:
                self.problemdata.append(data)
    
        def get_data(self):
            return ' '.join(self.problemdata)
    
    body = requests.get('http://www.problemotd.com/problem/lowercase-as/')
    parser = POTDParser()
    parser.feed(body.text)
    
    print len(filter(lambda x: x=='a', parser.get_data()))
    

    reply permalink

  • asheehan - 9 years, 9 months ago

    Here is the main logic in Ruby: ``` input = "It is, regrettably, not the case that all people who work as software engineers can e.g. code a for loop which counts the numbers of lower-case As in a string. This is true even if you spot them the syntax for a for loop and finding the Nth character in a string. It is equally true if you allow them to complete the task in isolation, at their own computer, given an arbitrary amount of time to complete it.A"

    findChar = 'a' counter = 0 i = 0 while (i = input.index(findChar, i+1)) and (i < input.length) do counter += 1 end

    p counter ```

    reply permalink

  • asheehan - 9 years, 9 months ago

    shoot, formatted properly

    input = "It is, regrettably, not the case that all people who work as software engineers can e.g. code a for loop which counts the numbers of lower-case As in a string. This is true even if you spot them the syntax for a for loop and finding the Nth character in a string. It is equally true if you allow them to complete the task in isolation, at their own computer, given an arbitrary amount of time to complete it.A"
    
    findChar = 'a'
    counter = 0
    i = 0
    while (i = input.index(findChar, i+1)) and (i < input.length) do
        counter += 1
    end
    
    p counter
    

    reply permalink

  • nrobinson - 9 years, 9 months ago

    I got 11

    var count = 0;
    
    var text = "Over the weekend there was a discussion of bad programmers in the work force over on Hacker News. One comment suggested that several employed engineers couldn't figure out the number of lowercase A's in a string. So today's challenge is to find the number of lowercase A's in this post."
    
    for(var i = 0; i < text.length; i++) {
      if (text.charCodeAt(i) === "a".charCodeAt(0)) {
        count++;
      }
    }
    

    reply permalink

  • Zekaykay - 9 years, 9 months ago

    <?php $text = '... Here is the string ...';

    $result = substr_count($text,'a');

    echo $result;

    reply permalink

  • Jt - 9 years, 9 months ago

    JavaScript, I got 15.

    //Short answer: postText.match(/a/g).length
    
    //Long answer
    this.window.onload = function () {
      var postText = 'Happy Monday! Over the weekend there was a discussion of bad programmers in the work force over on Hacker News. One comment suggested that several employed engineers couldn\'t figure out the number of lowercase A\'s in a string. So today\'s challenge is to find the number of lowercase A\'s in this post. Note: you don\'t have to count the A\'s in the html for the link to the comment.';
      var regexPattern = /a/g;
      this.document.writeln(postText);
      this.document.writeln("<br />");
      this.document.writeln("Count of A\'s: " + postText.match(regexPattern).length);
      this.document.writeln("<br />");
      this.document.writeln(postText.replace(regexPattern, '<b>a</b>'));
    };
    

    reply permalink

  • Anonymous - 9 years, 9 months ago

    I don't feel like scraping the text off the webpage, so I'm just copying and pasting into my crude Python solution.

    postText = '''Lowercase A's
    
    Happy Monday!
    
    Over the weekend there was a discussion of bad programmers in the work force over on Hacker News. One comment suggested that several employed engineers couldn't figure out the number of lowercase A's in a string. So today's challenge is to find the number of lowercase A's in this post.
    
    Note: you don't have to count the A's in the html for the link to the comment.'''
    
    print postText.count('a')
    
    #simple proof A is not counted
    print "aA".count('a')
    

    Perhaps if I'm motivated later, I'll make a nicer version that doesn't have hard coded text. For now, I want to get caught up again.

    reply permalink

Content curated by @MaxBurstein