A Counting Problem

I really love sites like Hackerrank and their programming challenges. Lately, one of them took my attention given that the simplest and standard solution didn´t work. The challenge is named “Find a String” and the idea is really simple: Count substring occurrences inside a string.

My first impression was the obvious one: “Hey, Python have this implemented”, so I tried the count method. This counts the number of times a sub string appears in a string, it is used like this:

string.count(substring)

But, the program failed. Look at this (there was another restriction, strings should have less or equal 200 characters):

If you run this, the result is 1 but the expected result is obviously 2.

My second thought was using regex library, something like this:

 

But again, the result was 1.

So, times like this is when you have to stop thinking in default language methods and libraries and start thinking in algorithmic solutions.

Here is something that happens to me when something standard is not doing what I want: I do things like a craftsman, so this is a quick approach of the function:

This might overkill the problem and there are many improvements in the code that can be done to make it faster, but for now, this would work.

So, the lesson is: always test your code, maybe some things that you think are OK because you are using the language standards can fail.

Creating a Twitter Bot II – Random Twits

 

Hello again, welcome back to this “tutorial” about creating a Twitter bot, if you missed first part, please read it here.

So, now that we can Twit, maybe we can do some interesting things with that.

Creating random Tweets

As I wrote in the last article, one problem of tweeting “Hello World” is that you can’t twit twice the same text. In the other hand, we want the program to run all the time, tweeting different things, so we will make our program twit every x seconds a random text.

First thing is to create a class that will have some common functionalities and in there create a method that will create random texts of x characters.

 

Even if the code is easy, here is a little explanation of how this works:

I have an array of characters that can be used for the text, and then a for from 0 to the number of desired characters it concatenates a string with new random character. In the end that string is returned.

Now that we have the functionality to send some random text, we can post tweets with a fixed delay. Here is one way to do that. Remember main file we have? Let’s work on that file and do something like this:

We need time library to make it post every x time. time.time() returns the time as a floating point number expressed in seconds since the epoch, in UTC. For more information about Time library look here. Next step is to make objects of class Common and Twitter and connect to our Twitter account.

Now that we are connected we enter in an infinite loop and say sleep x seconds (in the example I putted 30, but you can put what you want) and then post a random tweet of 140 characters calling the randomText method.

If you want the code, fork it, review it, do whatever you want just clone it from here

Next time we will do some changes to our profile, see who follows us, follow people, etc. so stay tuned!

 

Creating a Twitter Bot

In this series of articles I will be creating a Twitter bot in Python (because, maybe I said this before, but Python is THE language). Creating a basic bot is something really easy and at the end of this article we will have a basic bot posting tweets.

All the code up to date is in a public repository in github, so you can freely clone it from here

What we need:

  • Python (the glorious computer language) 3.X
  • Tweepy (there are other libraries, but, this one is great).
  • A Twitter account (that will be the bot’s account)
  • A Twitter app (I will explain this in the first point of the article).

Create a Twitter App

First of all you need a Twitter app, this is a way to tell Twitter guys that you will be using their API. To do that you need to go to developers link in Twitter, then search for “Manage my apps” and there you will see a “Create new App” button.

From there is just to complete a form with some basic data about the application you want to create:

  • Name of the application
  • Description
  • Website (a place where people can download, use or get info about the app).
  • Callback URL (optional, it depends if you will use it or not).

Once you save, you will have a access token, access secret token, consumer key and a secret key to access your account and do things with your bot.

Get Tweepy

Ok, now Twitter know we are going to do something with his API, so we need a library to easily access that API. We are going to use Tweepy for this tutorial but there are plenty of other libraries out there. Simply pip install it (you may need sudo on Linux)

pip install tweepy

Now we can start programming!

Basic coding

Now, lets open our favorite python (the almighty) IDE (in my case is Pycharm), and lets start

The first class I will like to create is the connector class, I will call it Twitter.py

Let me explain a little what we did here:

First I imported Tweepy so this class can use it (kind of obvious but I am here for all level of developers).

In the init part of the class I added the keys I get when I created the app in Twitter and two more variables for auth and the api manager.

The first method “connect” is self descriptive 🙂  it does the OAuth part, connecting to Twitter (if you want more info about OAuth please check this website) and sets the API manager for future use.

Now that we have this class (in future articles we will update this class, but for now it is a good start) we want to tweet something, so, lets create another class to do a main class to send a tweet (it is obvious that this will be updated as the project grows).

 

This works, but, second time you run it, it will not post your message because you cannot duplicate twits. But the idea of this first article was just to understand how you can twit something as a bot, from here you can do lots of things. For example you can do a loop to twit different things with some time in the middle.

Stay tuned, next time we will do lots of fun stuff with Tweepy!

Python Tutorial Chapter 1 -Hello World!-

python logo

 

As I said in one of my first posts, there are plenty of good reasons to program in Python and as I love this language, I will try to do tutorials, kind of  “from zero to hero”. I wish this tutorial makes you learn about Python.

 

What you will need for this tutorial:

  • Python 3
  • PyCharm (well, any other IDE will work, I just chose this one for this because I feel it is the best one).
  • Eager to learn

So, any comments, ideas, questions, whatever, just comment here or in the video I will answer.

 

P.S.: Notice that I don´t have any script for the videos, so when I press “record” I only have the idea of what I am going to talk, so I am not sure of what I am going to say at the beggining of the video.

Why Python?

print(Hello world!)

Welcome to my blog. Here my idea is to learn things, teach them, show my experience gained in lots of years in the software industry, some techie, technology, science and  I will talk about some personal things from time to time.

Even if the code here will be in lots of different languages, I will mostly show things in Python (specially Python 3). Why is that? Well… I am kind of fan of Python, but there are more reasons on why choosing or at least considering Python for development:

  1. It is easy to to create readable code: Python guides you to create clean and easy to read code while write the code.
  2. Coding speed: As it is very easy to write code, the speed on development is fast.
  3. Works almost everywhere: You write it in one machine/OS, it is highly probable that will run in other machines/OS.
  4. You want to do something? Python has the framework. With Python you can write code for the web (Django/Flask), create video games (PyGame), automate test (Selenium), even write mobile apps (Kivy).
  5. It is easy to learn: You can learn how to write code in Python in a couple of hours.
  6. There is a lot of jobs in Python.

Some people thinks that Python is kind of a toy and not a real language. Let me tell you that these people are wrong. Python is a serious thing and used in real life by top ranked companies, for  example:

  • Google: lots of things in the crawler and the earch engine are written in Python
  • Dropbox: They started with Python, they keep using it.
  • NASA.
  • Red Hat: for the installer and configuration
  • IBM
  • US Government (CIA).
  • Spotify: well, let them explain how they use it
  • And many, many more.

So, I am not telling you that oher languages are not as good as Python. Sometimes it is about how you like to code and which language fits you better and sometimes it is about what is best for what you are trying to do. For me, Python is great, ut I know lots of people that prefer Java, Ruby, C# and many more and they can give me great reasons to use them (well, someone at the office is teaching me Node.JS). The important thing at the end of the day is enjoy coding.