Solution for long repeat on checkio

I figured I’d share my solution for Long Repeat on Checkio. I got up this morning and had a few moments to solve. Did some research and found the groupby method from Itertools… there was an example online on how to use so I drew largely from that.

[pastacode lang=”python” manual=”from%20itertools%20import%20groupby%0A%0A%0Adef%20long_repeat(line)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20groups%20%3D%20groupby(line)%0A%20%20%20%20%20%20%20%20result%20%3D%20%5B(k%2C%20sum(1%20for%20_%20in%20v))%20for%20k%2C%20v%20in%20groups%5D%0A%20%20%20%20%20%20%20%20occ%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20_%20in%20result%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20k%2C%20v%20%3D%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20occ.append(v)%0A%20%20%20%20%20%20%20%20return%20max(occ)%0A%20%20%20%20except%3A%0A%20%20%20%20%20%20%20%20return%200%0A%0A%0Aif%20__name__%20%3D%3D%20’__main__’%3A%0A%20%20%20%20%23%20These%20%22asserts%22%20using%20only%20for%20self-checking%20and%20not%20necessary%20for%20auto-testing%0A%20%20%20%20assert%20long_repeat(‘sdsffffse’)%20%3D%3D%204%2C%20%22First%22%0A%20%20%20%20assert%20long_repeat(‘ddvvrwwwrggg’)%20%3D%3D%203%2C%20%22Second%22%0A%20%20%20%20assert%20long_repeat(%22%22)%20%3D%3D%200%0A%20%20%20%20assert%20long_repeat(%22abababaab%22)%20%3D%3D%202%0A%20%20%20%20print(‘%22Run%22%20is%20good.%20How%20is%20%22Check%22%3F’)” message=”” highlight=”” provider=”manual”/]

MongoDB, standard library, considering rolling my own blog again…

As you well know, I started out rolling my own blog and couldn’t finish due to being an extreme n00b. Now that I’ve made fairly significant progress with my FlaskCasts site I am slightly considering transferring all of this over to a strictly Python blog….but….IDK. We will see.

I got hung up on a couple of things tonight trying to get my flask casts site up off of the ground. First off I really got hung up on name vs id in my form fields. Apparently Flasks Request isn’t too fond of having both a name and an ID…and playing around with Bootstrap the ID tag was automatically placed so I was stuck for several minutes…about an hour..kind of sucked. Again…SOMETHING SMALL! But…I just wrote about it…so all the while I kept telling myself IT HAS TO BE SOMETHING SMALL AND STUPID….ALWAYS IS.

[pastacode lang=”python” manual=”%20%20%20%20%40staticmethod%0A%20%20%20%20def%20update(post_id%2C%20title%2C%20content)%3A%0A%20%20%20%20%20%20%20%20mongo.db.posts.update(%7B’_id’%3A%20post_id%7D%2C%20%7B’%24set’%3A%20%7B%22title%22%3A%20title%2C%20%22content%22%3A%20content%7D%7D)” message=”” highlight=”” provider=”manual”/]

Then I ran into an issue trying to update by post…I forgot the $set (loss for a word here) and ran into some issues…luckily solved em all.

Next up…the create method. This one will be a little tricky because Ive designed the site to show posts by the slug for SEO reasons…so I won’t be able to have duplicate slugs in the database. The slugs are generated with the object/Post itself based on the title….however…I’m leaving myself the option of changing the title/content if need be. This is a simple site so…a lot of common functionality really isn’t required but its fun to think through the possibilities.


Lets do some Checkio…

30 minutes later


and it beat me…lol


working on it,…..


Preparing for the future…

Basically all I am doing is focusing on my future, by learning new skills in the present. It’s easy to get sidetracked and think of new and interesting ideas, but staying focused is the key. One of the things I have been doing is browsing Upwork projects to see what people are looking for in developers/projects. I figure if I can sift through the postings and do my best to learn as much as I can about the technologies people are paying for, my chances of being successful when I am ready to bid. When will that be? I plan on trying to roll out several of my own projects as portfolio pieces and build up a solid amount of content and once I can find a project that I am 100% confident I can fit into my schedule and accomplish I will bid. My goal is 1 year.

I’ve also had a few mobile app ideas float through my head, so learning about REST-ful API’s is on the agenda in the coming months.

As far as my FlaskCasts site…I made a few minor adjustments today. Found a couple of minor bugs I need to work out once I get a minute. Plan to work on it quite a bit tomorrow evening.

Thanks for reading!

Learn by doing

It has become clear to me that the best way to learn something new is by getting your hands dirty and working on something. You can follow tutorials and watch someone else code a project, and follow along. You will definitely learn concepts, but the best way to learn this stuff is by tackling something on your own.  What kind of project should I build? You might ask.

I guarantee there is something that you have always thought would be a cool app or idea…even if it is something really small and trivial. Perhaps you just want a basic blog…which is what I am working on with FlaskCasts.

Today I worked out some login functionality using some of the concepts and methodology I picked up from a Udemy course I just took. This kept me from having to use some sort of extension that may provide way more functionality than I need. This also forced me to get my hands dirty and work out little bugs in the code as they came up.

Little bugs can be very frustrating and can somethings be disheartening. You should keep this in mind, from my experience 99% of the bugs you spend a ton of time on are so trivial that when you find them you feel stupid. One thing is for certain, you will likely remember that mistake going forward and will develop your code in such that you will not make the same mistake again. This might be new habits, or a change in the way you think about things….which the latter is most powerful.

For instance today I kept getting a 404 error on my login form…I couldn’t find the reason for the error…I realized that flasks request.form[‘variable’] is looking for the name=”variable” attribute in the form…not id=”variable”….stupid mistake, but I learned something new and will remember this going forward.  A lot of times it’s just small stuff like this that you learn from and in the end will make you a better developer.


I tinkered with the site a little here and there but nothing ground breaking today. I am just slowly piecing it together and hope to release: 9/6/17 with a couple of videos.

I love, it’s a cool  site that provides you with puzzles to solve in Python. Great learning tool. I embarked on The Angles of a Triangle today.

Never was all that great at trig but I love learning python so I tackled this one. Had help from this site:

And a library I found called Triangle Solver… 

Checkio doesn’t support external packages but I was able to dig through the code and pull out what I needed to solve this one.  Here is my solution:

[pastacode lang=”python” manual=”from%20math%20import%20pi%2C%20acos%0A%0A%0Adef%20sss(a%2Cb%2Cc)%3A%0A%20%20%20%20degree%20%3D%20pi%20%2F%20180%0A%20%20%20%20assert%20a%20%2B%20b%20%3E%20c%20and%20b%20%2B%20c%20%3E%20a%20and%20c%20%2B%20a%20%3E%20b%0A%20%20%20%20A%20%3D%20acos((b**2%20%2B%20c**2%20-%20a**2)%20%2F%20(2%20*%20b%20*%20c))%0A%20%20%20%20B%20%3D%20acos((c**2%20%2B%20a**2%20-%20b**2)%20%2F%20(2%20*%20c%20*%20a))%0A%20%20%20%20C%20%3D%20180%20-%20(A%2Fdegree)%20-%20(B%2Fdegree)%0A%20%20%20%20return%20A%2Fdegree%2C%20B%2Fdegree%2C%20C%0A%0A%0Adef%20checkio(a%2C%20b%2C%20c)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20A%2C%20B%2C%20C%20%3D%20sss(a%2C%20b%2C%20c)%0A%20%20%20%20%20%20%20%20result%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20round(A)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20round(B)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20round(C)%5D%0A%20%20%20%20%20%20%20%20return%20sorted(result)%0A%20%20%20%20except%3A%0A%20%20%20%20%20%20%20%20return%20%5B0%2C%200%2C%200%5D%0A%0A%0A%23These%20%22asserts%22%20using%20only%20for%20self-checking%20and%20not%20necessary%20for%20auto-testing%0Aif%20__name__%20%3D%3D%20’__main__’%3A%0A%20%20%20%20assert%20checkio(4%2C%204%2C%204)%20%3D%3D%20%5B60%2C%2060%2C%2060%5D%2C%20%22All%20sides%20are%20equal%22%0A%20%20%20%20assert%20checkio(3%2C%204%2C%205)%20%3D%3D%20%5B37%2C%2053%2C%2090%5D%2C%20%22Egyptian%20triangle%22%0A%20%20%20%20assert%20checkio(2%2C%202%2C%205)%20%3D%3D%20%5B0%2C%200%2C%200%5D%2C%20%22It’s%20can%20not%20be%20a%20triangle%22%0A%20%20%20%20assert%20checkio(5%2C%204%2C%203)%20%3D%3D%20%5B37%2C%2053%2C%2090%5D” message=”” highlight=”” provider=”manual”/]


Today while working on the flask casts site, I decided it would be a good idea to generate a url slug. This is extremely important for search engine optimization. I thought about writing my own class to do this, then thought it might get ugly…so I searched for a package. I found Slugify:

Pretty simple to use. When creating the Post object I just modified my __init__ to the following:

[pastacode lang=”python” manual=”%20%20%20%20def%20__init__(self%2C%20title%2C%20content%2C%20author%2C%20created%3DNone)%3A%0A%20%20%20%20%20%20%20%20self.title%20%3D%20title%0A%20%20%20%20%20%20%20%20self.slug%20%3D%20slugify(title)‘%25A%20%25x%20%40%20%25H%3A%25M%3A%25S’)%20%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20created%20is%20None%20else%20created” message=”” highlight=”” provider=”manual”/]

Now I can pass the slug to a url like:

[pastacode lang=”python” manual=”%40home.route(‘%2Fpost%2F%3Cstring%3Aslug%3E’)%0Adef%20post(slug)%3A%0A%20%20%20%20post%20%3D%20Post.get_post(slug)%0A%20%20%20%20author%20%3D%20User.get_user(post%5B’author’%5D)%0A%20%20%20%20return%20render_template(‘home%2Fpost.html’%2C%20post%3Dpost%2C%20author%3Dauthor%5B’fullname’%5D)” message=”” highlight=”” provider=”manual”/]

Thanks for reading!


So the concept escaped me for a couple of days. I actually had considered abandoning my MongoDB trials in favor of a SQL based system. What I discovered was that I am more persistent in finding a solution to my problem than I originally thought. I stumbled upon this Gist:

This is a very simple and basic Pagination class written for MongoEngine. Flask already has an extension for MongoEngine with this already built in. After looking at the code I realized all this is doing is passing an iterable to the Pagination class and then building links in the template. So I decided I would type it out line by line so I could get a better grasp on what was happening in the code.

So now I have Flask-PyMongo communicating with my database and was able to build the models in such a way that makes more sense to me at this stage of my learning.

For instance, my Post model looks like this:

[pastacode lang=”python” manual=”class%20Post(object)%3A%0A%0A%0A%20%20%20%20def%20__init__(self%2C%20title%2C%20content%2C%20author%2C%20created%3DNone)‘%25A%20%25x%20%40%20%25H%3A%25M%3A%25S’)%20%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20created%20is%20None%20else%20created%0A%0A%20%20%20%20def%20__repr__(self)%3A%0A%20%20%20%20%20%20%20%20return%20%22%3CPost%20%7B%7D%3E%22.format(self.title)%0A%0A%20%20%20%20def%20json(self)’title’%3A%20self.title%7D%2C%20self.json()%2C%20upsert%3DTrue)%0A” message=”” highlight=”” provider=”manual”/]

MongoEngine allows reference fields so you can actually store the author object in the post document…which is kind of cool becuase you could do something like this from the template So I was a little stuck on trying to find a way to do this using PyMongo until I read this post:

It explains a more efficient way of doing this. In my case I am referencing the user by the author variable, so I have set my User class up so that _id is a unique username and will reference the user by the username.

See the User class:

[pastacode lang=”python” manual=”class%20User(object)%3A%0A%0A%20%20%20%20def%20__init__(self%2C%20_id%2C%20email%2C%20password%2C%20fullname)” message=”” highlight=”” provider=”manual”/]

So using the get_user() method I can get the fullname…

LEARNING A LOT! and having a lot of fun!

Also found this library…for generating fake data…VERY useful

Change of plans…sort of…kind of…

I am currently reconsidering my options as far as the database for FlaskCasts. As a newbie to Flask and Python, I am starting to see that MongoDB may be more challenging to use than I had initially thought. However, I’m not one to give up so soon. A couple of things I am confused about stand out:

  1. Implementing Pagination for the list of videos on the front page.
  2. Sorting those in descending order.

Coming from an SQL background this isn’t so complicated and in fact a lot of the SQLAlchemy based libraries have a pagination method built in.

Right now I am looking at the Flask-PyMongo extension to see if this will make things any easier. So far I have used plain PyMongo (on my blog attempt I mentioned earlier) and wrote a lot of the DB methods myself. I’ve already came across YouTube videos showing how to sort with this library so I think I may be able to use this and roll my own pagination.

After a little more research, I found Flask-MongoEngine. With just a little tinkering I was able to implement a very basic pagination. So I am pretty happy with that. This library seems to be well supported so I may just stick with this.

[pastacode lang=”python” manual=”from%20flask%20import%20Flask%2C%20request%2C%20Blueprint%2C%20render_template%0Afrom%20flask_mongoengine%20import%20*%0Afrom%20.models%20import%20Post%0A%0Aapp%20%3D%20Flask(__name__)%0Aapp.config.from_object(‘config’)%0Adb%20%3D%20MongoEngine(app)%0A%0A%40app.route(‘%2F’)%0Adef%20hello_world()%3A%0A%20%20%20%20page%20%3D%20int(request.args.get(‘page’)%20or%201)%0A%20%20%20%20posts%20%3D%20Post.objects.order_by(‘-created’)%0A%20%20%20%20paginated_posts%20%3D%20Pagination(posts%2C%20page%3Dpage%2C%20per_page%3D2)%0A%20%20%20%20return%20render_template(‘index.html’%2C%20paginated_posts%3Dpaginated_posts)” message=”” highlight=”” provider=”manual”/]

This is my view. Basically it checks to see if a page variable is being passed through. Gets the posts in descending order. Passes the posts to the Pagination class that is built in with the current page and showing only 2 entries per page. Then it renders the template with the paginated_posts.

[pastacode lang=”python” manual=”!’hello_world’)%20%7D%7D%0A” message=”” highlight=”” provider=”manual”/]

I’d never really messed wtih a Macro in Jinja before this so I need to do a little more studying to get a grasp on what is happening here.  But…it works, and is much easier to roll out than anything else I think I could have done.

The models are a bit confusing as these classes inherit from the Document class…and I have not yet worked with anything like this, I’ve written my own classes. I am going to play around with the concepts a little more to get confortable. Here is what they look like right now:

[pastacode lang=”python” manual=”import%20datetime%0Afrom%20mongoengine%20import%20*%0A%0A%0Aclass%20User(Document)%3A%0A%20%20%20%20email%20%3D%20EmailField()%0A%20%20%20%20password%20%3D%20StringField()%0A%20%20%20%20fullname%20%3D%20StringField()%0A%0A%0Aclass%20Post(Document)%3A%0A%20%20%20%20title%20%3D%20StringField()%0A%20%20%20%20content%20%3D%20StringField()%0A%20%20%20%20author%20%3D%20ReferenceField(User)%0A%20%20%20%20created%20%3D%20DateTimeField(” message=”” highlight=”” provider=”manual”/]

Going to work on this some more tomorrow. Very happy to have solved this pagination issue however.


Flask application structure…

One of the beautiful things about Flask is the freedom to do whatever the hell you want. Unfortunately, for me as a novice, this can be a huge disadvantage at the same time. Not knowing all of the best practices can cause some unnecessary headache. Lucky for me there are a lot of blog entries out there to help get this off the ground.

Blueprints is a flask feature that allows you to organize your app in a modular way. Each “section” will act almost as if it is it’s own application. I took a screenshot of my current directory structure to show you what it looks like. So the project itself would be considered a repository,

and will end up on github at some point as I make more solid plans.

Right now I just wanted to get something running even if it is technically just a simple and basic “Hello World.”

Currently the file contains:

[pastacode lang=”python” manual=”’DEBUG’%5D)%0A” message=”” highlight=”” provider=”manual”/]

When ran, it then goes to the flaskcasts directory since it is a module and runs the code in

[pastacode lang=”python” manual=”from%20flask%20import%20Flask%0A%0Aapp%20%3D%20Flask(__name__)%0A%0Aapp.config.from_object(‘config’)%0A%0Afrom%20.views.home%20import%20home%0Aapp.register_blueprint(home)” message=”” highlight=”” provider=”manual”/]

This will go to the home blueprint and execute that code:

[pastacode lang=”python” manual=”from%20flask%20import%20Blueprint%0A%0Ahome%20%3D%20Blueprint(‘home’%2C%20__name__)%0A%0A%40home.route(‘%2F’)%0Adef%20index()%3A%0A%20%20%20%20return%20%22Welcome%20to%20the%20Index%22″ message=”” highlight=”” provider=”manual”/]

The only other file is and it basically holds the DEBUG=True boolean only.


Structurally, for a very basic application that is only going to feed videos from a YouTube or similar service, this should suffice.

The one thing…

Had a long drive for work today. I usually listen to a lot of different podcasts on various topics. Lately my favorites are “Talk Python to Me” and the like… I listened “The SaaS podcast” because one of my goals from learning to code in Python is to develop a product, release it, and start earning supplemental income. Podcasts are great because not only will you learn things and absorb a lot of data, you find inspiration. The host kept talking about the book “The One Thing” by Gary Keller. Since I had 3 credits on Audible, I decided, why not! So far it is a GREAT book.

The main idea is to pick 1 thing, and focus on it with everything you have. Build habits by struggling with the difficulties of doing that 1 thing for 66 days on average and it becomes easy. So, I asked myself. What is the one thing that I can do today that will propel me toward my goal of becoming a successful freelancer and entrepreneur? The answer that came to mind is, Blog about coding and code. Talk about the things I am learning in a blog post and share with the world. Set aside some time to document, almost in a diary like format the things that I am working on and learning.

This will propel me in my coding career because the habit of blogging will force me to find things to write about. The process of finding things to write about will help me to learn new concepts and force me to produce code to share. Even on days when my normal job has sucked out all of my energy and I just don’t have it in me to write a line of code, I can talk about ways to find inspiration. I can talk about the feeling that comes from building something that works and solves a problem for someone. I can think about how satisfying it is to finally solve a bug in my code.

So, this is my blog. I am going to update this site regularly with information that will build confidence and keep me on track to success. I plan to work on some tutorials tomorrow and will write about my thoughts.