Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Function that shows the number of occurrences of the same digits appearing next to each other

How would I go about creating a function with an argument being an integer to return an integer showing the number of occurrences that two of the same digits are next to each other?

2 Answers

Okay, so I couldn't find the code, I typed but I redid it...


def double_digit_counter(number):
    counter = 0  # function starts with the count 0
    last_digit = None  # last_digit variable uses None
    for digit in str(number):  # the int input is converted into a string and iterated "digit by digit" in a for loop.
        if digit == last_digit:  # compares the current digit to see if it's the same as the last digit
            counter += 1  # if it is, meaning there's a double, the counter goes up by 1
        last_digit = digit  # set the last digit variable to the current digit before continuing with the loop.
    return counter  # returns the integer count of duplicates.

There's a potential flaw with this code, depending on what you mean by

the number of occurrences that two of the same digits are next to each other

For example, a triplet would be counted as 2 doubles since (33)3 and 3(33) are both counted.

I hope this works for whatever your trying to do. lol

[Moderator redacted]

kinda ghetto but gets the job done... lool

Michael Hulet
Michael Hulet
47,912 Points

Hey hamsernation (I like the name btw lol),

For future reference, it's frowned upon in the Community to post code that can be copy/pasted and work without any explanation about why it works. If you'd like, feel free to re-post your code, but be sure to tell us about why it does the trick (how it functions, etc). Thanks for helping out in the Community!

Can you post it back up and explain? I didn't get a chance to write it down and test it out.

Keeping in mind that I do pay to have access to this forum, I would appreciate if answers to questions that I post are not deleted because someone is not fond of the way something is answered. If I wanted this type of experience, I would be on stackoverflow.

Michael Hulet
Michael Hulet
47,912 Points

I believe this forum is free, and you do not need to pay to access it. You can pause your account and continue to view/ask/answer questions here. That being said, you probably shouldn't quote me on that, because I'm only a student like you, and not Treehouse staff. If you want staff to hear about your feature requests and such, you should email them. It's unlikely anybody important will see them if you post them here