Search

The Secrets Of Random Numbers In Python Part 1

python random numbers
 

When people think of random numbers, they think of something disorganized and that cannot be predicted. That is what I thought until I entered the world of random numbers using python. You see, random numbers do not just run arbitrarily. The name is deceptive. They are based on what are called seeds. A seed can either be provided by the programmer, and if one is not provided, python uses the system clock or what is supplied by the operating system.

If you decide to set the random number generator rather than allow the program to initialize it on its own, you call the random.seed function, supplying it with a seed that you desire. The seed has to be an int. This could be done using the code:

As you can see from running the above code where we gave the random function the same seed, the same output was produced consecutively. That is why the random function is deceptive. Why it is really random is because since the programmer most times does not provide a seed but allows it to self-generate, the random number becomes really random.

By the way, the disclaimer from the makers of the python programming language is that the functions in the random module should not be used in cryptography.

Now let’s take a ride on some of the functions included in the random module.

The functions are divided into functions that manipulate integers, sequences, and real-valued distributions. We will concentrate on the first two in this article.

What are the functions that manipulate integers.

The two functions in the random module that manipulate integers are random.randint and random.randrange. They both do the same thing. They return an integer based on a given range. The differences between them is that in random.randint the second positional argument is included in the range of numbers to be considered, while in random.randrange the range to consider stops just before the second positional argument. The syntax for both of them is random.randint(a, b) and random.randrange(start, stop[, step]). For random.randrange the stop is not included in the range that is to be considered, while in random.randint the last positional argument, b, is considered in the range to be considered. Also, note that you could step through random.randrange using the step argument. For example, you could leave out some numbers in a range using step, just as you can do using the in-built range function.

Overall, both of them serves the same function: give a random integer from a given range and conditions.

If you run the code below, you will be sure to get a random integer between 1 and 20 inclusive.

What are the functions that manipulate sequences.

The functions for manipulating sequences are four in number and they sometimes overlap. I will describe each of them and give code you can run.

random.choice(sequence):

This function returns a random element from sequence provided sequence has items in it otherwise it will raise an IndexError. I think this is simple enough. Now, the code.

Each time you run the code above, it will give you a chosen fruit. There are repeats too. So, don’t be surprised because the list of fruits is not exhaustive.

random.choices(population, weights=None, *, cum_weights=None, k=1):

This function returns a list of size k (default is 1, note) from the population which could be a sequence or a range. If you do not want to apply any probability to the items that would be returned in the list, then you would leave weights and cum_weights keyword arguments alone. But if you want to give some weight or assign probabilities to what can be chosen at random, you have to provide value for weights or cumulative weight, cum_weights. Using the list from above, let us give higher probability to apple and orange using the keyword argument, weights=[5,5,5,15,15]. I will also tell the code to chose just two items from the list of fruits at random.

If you run the code, you will see that you will have a high probability of having orange and apple in the returned list rather than the other fruits. One point to note is that weights cannot be negative.

random.shuffle(x[, random]):

This function takes as argument a sequence, x, and shuffles the items in place. You can provide an optional random function that could be used to shuffle the items. I think this is well explained using code. I will provide code that does not use the random function and one that does.

This code does not use the random function.

Each time you run it, it randomly rearranges the items in the list.

Now this one uses a random function. I want you to run it more than one time and notice the arrangement of the items.

Did you notice that the rand_function acted as a seed to the random.shuffle function? Each time it runs, it returns the list of items in the same sequence. Very cool! You can play with it. Note that the random function can be any function that returns a floating point number between 0.0 and 1.0. I used 0.09.

Now for the last function that manipulates sequences.

random.sample(population, k):

This function is used for random sampling without replacement. It returns a list of size k from the population. It is similar to the choices function but without the weights and without replacement. In random.choices, the returned list can have repeated items but not in random.sample. Now for some code.

Run the above code repeatedly and see. The sample size, k, should not be larger than the population size otherwise python will raise a ValueError.

You can use this function to generate a random list of integers from a very large sample size using the range function as the population argument using code like this: random.sample(range(1000000), 100). Very cool and very efficient.

So, that is the secret to the random function. Now that you have learned what it is and how to use it, why don’t we play with it a little.

Tomorrow, I am going to post a little game that uses the random function. So, make sure to watch out for the game.

Happy pythoning.

7 Ways To Keep Yourself Motivated While Programming

Programming to be honest is not easy. It can be hard especially when you are stuck in a problem, or even when you have a bug and you don’t know a way out of it. I once wrote a project that had 400 lines of code and eventually there was a bug in it. I had to use bisection search to go through each of the lines of code to find out the bug. That one hour of work was very demotivating. You really need to be motivated to be a programmer, even while programming in python which is one of the easiest languages around.

People often ask me: “Nnaemeka David, what do you do to be motivated? Sometimes, it gets hard and frustrating?” So, I decided to write this post on some of the things one can do to get motivated while programming.

programming motivation
 

  1. Be disciplined.

  2. According to some dictionaries, discipline is the practice of obeying rules or a code of behavior. This skill is essential in programming because if you are not disciplined, you will end up in a lot of obstacles to your programming. Disciplining frees up your mind to think about solutions, and not just to rush into putting lines of code on an editor. Discipline makes you set up your programming defensively; you plan ahead for obstacles to your programming when disciplined. Discipline involves understanding the conventions of python as a programming language and following them. They keep you motivated. The style guide for python code, or sometimes called PEP8, is a good resource to instill discipline.

    When I learned programming from Professor Eric Grimm at MIT, (online course), he kept emphasizing on doing tests and setting yourself up for defensive programming before you begin writing code. I have seen the wisdom of that as time goes by. It helps me to debug; and bugs are something you will find all the time.

    If you are not disciplined, you will find yourself looking for a needle in a haystack. That is a very frustrating endeavor.

  3. Start with the bare minimum.

  4. Often beginners to python keep asking the question: Can I learn python in 3 months? What do I do to be a data scientist in the shortest space of time? I always tell anyone that asks me that they should go with the flow. Don’t force it. If you force it and you want to learn everything in the shortest time, you will get disappointed. Start with the bare minimum. Increase your aptitude as time passes by and your confidence increases. Otherwise, when it is 3 months and you discover you have not even scratched the surface of python programming, you will lose motivation. It is similar to when someone is working on the treadmill.

    While working on the treadmill, you don’t just start all at once doing 3 hours at a go. First, you start with five minutes intervals and take breaks. Then, as your body gets used to the routine, you increase the time you spend. So it is with programming in python and any other language. My advice to anyone who wants to take a fast paced approach is that they might be setting themselves up for disappointment. Rather, you take it slowly, one step at a time. Don’t constrain yourself to a time limit, but regularity is the secret to success.

  5. Try the Pomodoro technique.

  6. The Pomodoro technique is a time management technique that encourages you to work using the time you have rather than work against it. For example, say you can spare 30 minutes for programming, what you do is you spend that 30 minutes and set a timer to alert you when the time is up. When it is up, you take a break, and when you can spare another 30 minute you continue again, setting the timer. Many programmers have confessed that the pomodoro technique helps them to be productive. It can also help you.

    As programmers, we spend a lot of time in front of the computer and burn-out can easily set in, causing you to lose motivation. With the pomodoro technique, you will never get burned out because you are pacing yourself and taking breaks that helps you to get refreshed. Pomodoro technique also helps to boost your concentration and focus. There are a lot of softwares that implement the pomodoro technique and you can use them, like the focus booster app that runs on both windows and mac.

    A programming friend of mine on a forum told me that he used the pomodoro technique to learn 3 languages in 2 months. You can try it out yourself.

  7. Set goals.

  8. There is a trite saying that if you fail to plan, you are planning to fail. I have found setting specific goals helpful in helping me to get motivated while programming. But you don’t want to be setting goals you cannot achieve like starting from zero to hero in python in one month. You would be setting up yourself for failure. Set SMART goals, that is, goals that are specific, measurable, achievable, relevant, and time-oriented. After you have achieved one goal, congratulate yourself and move on to the next. Doing this, you will find python programming, or any other programming task, very enjoyable and fun.

    When I started programming, one goal I set is to do one challenge on hackerrank.com on a daily basis. I succeeded in completing the 30 days of code challenge and also doing other challenges. On some days, I would really feel in high spirits, congratulating myself for moving on to the next level. Try it out yourself by signing up at hackerrank.com and take a challenge. If you are a new programmer, take the basic challenge. There are lots of languages to choose from at the website.

  9. Projects, Projects and lots of Projects.

  10. To boost your confidence level and set yourself up for the industry, you can never underestimate the value of doing projects. My advice is that you do projects, more projects and lots of projects. Sign up at github and look for projects to collaborate. There are even projects on github.com that accepts beginners. You could sign up for those if you are new to python programming.

    If you don’t have ideas on what projects to do that would fit your level, just Google it. You will find all sorts of projects that you can choose from on Google; from beginner to expert.

    If you keep going from tutorial to tutorial while on your programming journey, you will be disappointed in record time. You need to learn to practice. That is the secret to being motivated.

  11. Love what you are doing.

  12. You must be passionate about programming, in python or any other language, to survive in this industry. Without passion and love for coding, you would be disappointed in no time. It involves lots of screen time, sometimes it steals the time from your relationships. You could set yourself up for failure if you are doing it for the money because you will encounter a lot of obstacles in the programming journey.

    Love programming. Write a line of code every day. In fact, to be motivated, you must love doing this and if you have read this post this far, I believe you love coding and want to improve. So congratulations.

  13. Start teaching others about programming.

  14. Teaching is a way of imparting knowledge to others. While you are teaching, you are improving your ability to use python and to learn python. Remember, as you are teaching others, you are teaching yourself. You are gaining benefits for yourself that are valuable.

    Also, teaching others imparts other soft skills to you. By teaching others, you gain communication skills and presentation skills. While successfully teaching others, you increase your confidence in using python programming language and you also gain leadership skills. According to glassdoor.com, a job search site, these skills are in high demand in the programming industry.

    If you cannot be part of a class to teach, you can participate in forums to help others. Forums like python-forum.io, freecodecamp.org, and Stackoverflow.com, can be of immense help to you to get teaching opportunities.

I wish you success in your programming career.

Why Shaving Blades Become Useless After Cutting Human Hair

For a long time scientists have been fascinated with one problem when it concerns blades. Although blades are made of stainless steel and have edges that are razor-sharp, to further strengthen them they are coated with diamond-like carbon, but a material that is 50 times softer than a blade such as a human hair can be able to make the blade useless over time. From a logical point of view, this should not be the case.

 

Intrigued by this problem, the engineers at MIT’s department of Material Science and Engineering have come up with an innovative solution. These engineers concern themselves daily with exploring the microstructure of materials in order to design and make new materials that could be able to have exceptional damage-resistance properties. The lead researcher, Gianluca Roscioli, an MIT graduate student, came up with his idea when he was shaving his own hair.

After noticing that his blades tend to get dull with time after shaving, he decided to take images of the blades after each shaving activity. He took these images with a scanning electron microscope (SEM), scanning the blade’s edge in order to track how the blade wore down over time. What he discovered showed that the process is much more complex than a simple wear over time. He noticed very little wear and rounding out at the edges but instead realized that chips were being formed around certain regions of the razor’s edge. These led him to ask himself: Under what conditions do these chipping take place, and what are the ingredients for a strengthened blade to fail after shaving a material as soft as human hair?

To answer these questions conclusively he built an apparatus that was designed to fit inside an SEM and he used it to take samples of his shaving and that of his colleagues. They found that there were some conditions that might cause the edges of a blade to chip and as the chipping proceeds with time, it will cause the blade to get dull. The conditions depend on the blade’s microstructure. If the blade is heterogeneous or the microscopic structure is not uniform, the blade will be more prone to chipping. Also, the angle at which the cutting was done was found to be significant. Therefore, they found that shaving at right angles were better than lower angles. Finally, the presence of defects in the steel’s microstructure was another factor that played a role in initiating cracks on the blade’s edge. Chipping was found to be more prominent when the human hair met the blade at a weak point in the blade’s heterogeneous structure.

These conditions illustrate a mechanism that is well known in engineering - stress intensification. This is the intensification of the stress applied to a material because the structure of the material has microcracks. Once an initial microcrack has formed, the material’s heterogeneous structure enabled these cracks to easily grow to become chips. Therefore, even though the material might be fifty times stronger than what it is cutting, the heterogeneity of the material can increase the stress on it, making cracks to intensify.

The implications of this discovery is immense. It will save money and costs to the average user of shaving blades because it will offer clues on how the edges of a blade can be preserved, and give manufacturers the opportunity to make better blades or cutting materials by using more homogenous materials.

The engineers have already taken their discovery one step further. They have filed a provisional patent on a process to manipulate steel into a more homogenous form, with the hope that they could use this process to build longer-lasting, and more chip-resistant blades.

Material for this post was taken from the MIT news website.

A Microscopic View Of Python’s Lookbehind and Lookahead Regex Assertions

Any discussion on regular expressions, or regex, is not complete without taking note of the lookaround assertions. Lookaround assertion in regex are assertions that state that at the current position of the string, check whether so and so pattern exists before or after the string. Note that when doing lookarounds, the string used in the lookaround is not consumed and the current position in the string does not change.

Now we will be making use of four types of lookaround assertions in regex today. They are the positive lookbehind, the negative lookbehind, the lookahead, and lastly the negative lookahead assertion.

Python Regex with lookahead and lookbehind assertions
 

The Positive and Negative lookbehind assertions.

In lookbehind assertions, we are only looking for what precedes the current position in the string that we want to match. The pattern in the lookbehind assertion does not participate in the match, or as it is said, is not consumed in the match. It only helps in asserting that the match is true. Lookbehind can be positive or negative. In positive lookbeind assertions, we are asserting that the pattern is present before the string. In negative lookbehind assertions, we are asserting that the pattern is not present before the string.

The syntax for positive lookbehind assertion is (?<=foo) where a match is found if foo precedes the current position of the string that is to be matched and foo ends at the current position.

Let’s illustrate this with some example. For example, let’s assert some currency figures. If we have a string like ‘USD100’ and we only want to match 100. We can assert that USD should come before the number 100 with this code: (?<=USD)\d{3} which states to match a digit consisting of exactly 3 characters which is preceded by the string, USD.

The syntax for the negative lookbehind assertion is (?<!foo) which matches the current position of the string if foo is not before the string. If we could continue with the string, ‘USD100’, then to say that the EURO should not be in the string we could use the code: (?<!EURO)\d{3}. I believe by now you must understand what the pattern represents.

Now, we will go on the the second set of lookaround assertions which are the lookahead assertion and negative lookahead assertion.

The lookahead and negative lookahead assertions.

The lookahead assertions are just the opposite of the lookbehind assertions. The lookahead assertions look for the pattern or non-existence of the pattern ahead of the current position in the string.

The lookahead assertion looks for the existence of the specified pattern from the current position. The syntax of the lookahead assertion is (?=foo) where from the current position of the string we are looking ahead if foo exists. Let’s take our 'USD100' string again. If we want to do a lookahead to see if the number exists after the dollar symbol, we could use the following code: w{3}(?=\d{3}). But the 100 number is not consumed in the match, we are taking out only the USD, just that we only want to assert that the 100 comes after the USD.

The negative lookahead assertion is an opposite of the lookahead assertion. If included in a pattern, it asserts that the pattern in the assertion does not come after the current position in the string. For example, if we have the following string 'USD100' and we want to assert that it is not 'USD200' we could use the following code: \w{3}(?!200). The code states that we are matching any string that has three letters but without 200 following it literally.

So, that is what we can take from the lookaround assertions in python. Now, let’s use our knowledge to solve a problem.

Assuming you are given the following string, rabcdeefgyYhFjkIoomnpOeorteeeeet, and you want to match all substrings of the string that contain 2 or more vowels on the condition that each of the substrings must lie between two consonants and must only contain vowels. How do you go about it.

If you look at the question, it involves lookbehind and lookahead assertions. That is, a consonant must lie before the vowel (lookbehind) and must also lie ahead of the vowels (lookahead). When you understand this, your work is nearly done. Then we must denote what it means to have a consonant. That means, it must be any letter that does not lie within the set of vowels, [aeiou]. We will be doing a case-insensitive match, so we will have to raise the Ignore case flag for the regex search pattern, re.I.

Here is how the code is written:

There is nothing new in the code. I have already explained most of the code in another blog post, entitled: The Big Advantage of Understanding Python Regex Methods.

You can download the script here if you want to take a deeper look at it.

I hope you have a nice day. If you want to keep receiving python updates like this, just subscribe to the blog using your email.

A Game Changing, Innovative, Microscale E-waste Recycling Strategy For The Environment And Manufacturing.

A typical recycling process involves the following – waste is collected, sorted, cleaned and processed, then the processed waste is used to manufacture more of the same product, and the cycle is closed when consumers buy these products. The recycling process depends on the fact that these collected waste are made of the same material. That makes it possible to manufacture new products. Yet, that is not the typical case for e-waste.

E-waste, or electronic products that are no longer working, unwanted, or close to the end of their useful lives, are usually made of heterogeneous materials which cannot be readily separated. Therefore, recycling them and putting them back into the cycle does not seem too commercially profitable for the average manufacturer. Yet, these wastes have to be recycled because if they are put back into the environment, which turns out to be usually the case, the toxic materials contained in them can poison our soil, water, air, or wildlife. 

 

To solve this problem some researchers have developed a selective, small-scale microrecycling strategy which can be used to convert old printed electronic parts like circuit boards and monitors into a new type of strong metal coating. These researchers, Veena Sahajwalla and Rumana Hossain, based their research on the copper and silica that are usually the components of electronic devices. They realized that based on the properties of these compounds they could be extracted from e-waste, then combined at high temperatures, even up to 2,732 F, thereby in the process generating silicon carbide nanowires which can then be processed further to create a durable, new hybrid material that is ideal for protecting metal surfaces.

These technique is innovative and a game changer. This could reduce the number of e-waste that end up in landfills and make it profitable for recycling plants to go into recycling larger amounts of e-waste. Imagine, the typical electronic device like a laptop and a TV screen contains lots of potentially valuable substances that could be used to modify the performance of other materials or used to manufacture new reliable materials. That is what this innovation makes possible. Also, the process, which the researchers have called material microsurgery, could be used to recover a large amount of copper annually for use in industries such as electronic devices, industrial, transportation, and consumer products.

Imagine the number of jobs that could be created in the recycling industry if e-waste was taken out of the ecosystem daily. Imagine what benefits it would bring to our environment not to have to dispose of these devices in landfills where they could percolate back to our water cycle or food.

These material microsurgery technique could also be used to create durable, new hybrid materials that could be used to protect metal surfaces. Yes, and it has been tested. During laboratory experiments it was discovered that the hybrid materials when fixed to steel remain firmly entrenched and when the steel is struck with a nanoscale indenter the hybrid layer does not get detached from the steel but remains firm, showing no signs of cracking or chipping. Further, it was seen to increase the hardness of steel by about 125%.

The potential benefits of this small-scale microrecycling strategy is very high. Thanks to the innovation of these two researchers, we could have a cleaner environment free from e-wastes and making sure more electronic products are not disposed of improperly.

I included this innovation in my blog because of its high potential benefit to mankind. I think this is a problem solved and worthy of being acclaimed.

Materials for this post were taken from a press release by the American Chemical Society, ACS.

Coding Python Functions Converting To Any Base That Fits Your Fancy

Sometimes while doing maths, we are faced with converting from one base to the other. Python is good for occasions like this. That is why I love programming in python. In this post, I will describe two ways you can convert any number in base 10 to any other base.

 

First, the more than one line way.

We will use simple mathematical logic to do the base conversion. In maths when you are converting from base 10 to any other base, what you do is that you repeatedly divide the number by the base, including the quotient, and keep the remainder until the quotient gets to zero. When you get the list of remainders, you then reverse the list and you have your converted number in that base. In this simplistic example that is also what we are going to do. I will be using two built-in functions to carry out this activity.

The first function is the divmod function. The syntax of the divmod function is divmod(numerator, denominator). The denominator divides the numerator. What the function returns is a tuple consisting of the quotient and the remainder. That’s it. So, we will be using this function to repeatedly divide the number and its quotient while returning the quotient and the remainder. The quotient will be checked against a condition that it has not gotten to zero while we will be collecting the remainder in a list. Just like you do in maths. When the quotient gets to zero, the division will end.

Then the second function we will be using is the reversed function. The syntax of the reversed function is reversed(sequence). As the name implies, reversed just takes a sequence and reverses the items in the sequence. So simple.

Now that we have our two handy functions, we are ready to write code that will convert any number to any base. I will call the function we will use for this, converter.

Here is the code:

I want you to note that while appending the remainder to each remainder_digits list, I first converted them to a string. If I did not, reversed would not be able to reverse them. Also in line 6 I used the ‘’.join statement to cast the list to a string.

You can download the script for the code above from here.

That’s the logical way to go about it. Now let me introduce you on how to do it with numpy.

One liner Numpy.

Numpy is fundamental for scientific and mathematical computing in python. As you know we are dealing with mathematical things here; numpy has a handy function for handling it. But I would add that numpy is somewhat of an overkill. The simple method I introduced above for programming it in python is enough. But in case you want something that can do it in one line and faster, then you can use numpy.

First, to use numpy it has to be installed on your machine. You can use the command: pip install numpy to install it on your machine, and if it is already installed, then you are good to go. Next, on your script you need to import numpy into your script using the statement: import numpy.

We will be using the numpy.base_repr function for this conversion. The syntax of the function is numpy.base_repr(number, base=2, padding=0). The only positional argument is the number you want to convert. The next is a keyword argument and this signifies the base you want to be converting to. The default is base 2. Then the next keyword argument is the padding you want for the numbers. The default is 0, that is, don’t pad the result with leading zeros. This is in case you want to pad the results. In case you need a refresher on positional and keyword arguments, you can see this earlier post on keyword and positional arguments.

Now that we have everything set up, let us see the one line of code that can do this for us.

You can download the script for the code above from here.

That’s it. Numpy is just another easier way of doing it with more added functionality. Numpy is beautiful. Note though that this function in numpy can only convert bases from base 2 to base 36. So if you need a base that is higher than 36, you should use the first converter function instead.

If you want to keep receiving python posts like this, just subscribe to this blog using your email. Happy pythoning.

Sweat Based Health Tracker That Is Better Than Anything Known

When Fitbit first came into the market I was wowed. I told myself that was cool. I could track all sorts of things about my health and performance. Along with Fitbit came a host of other gadgets that can help you understand your health status. But they all lacked one quality – they could not replace diagnostic tests required by medical doctors.

Woman with a health tracker on wrist
 

Now, there is a product that can. It is a sweat-based flexible electronic sensing thread that can be sewn into clothing to analyze all sorts of biomarkers and give accurate diagnosis. The threads come equipped with special sensors for collecting sweat while you are walking, running, working, or even watching TV. It then has electronic components that are wirelessly connected to a smartphone for real-time data acquisition and processing. These patches are just great.

They were developed by engineers at Tufts University who saw a need in the market. Present biomarkers could only track heart rate, temperature, glucose levels, walking distance and take only gross measurements. They could not do the real work of replacing laboratory diagnosis required often by doctors when analyzing your health status. So, the engineers decided to concentrate on something that could replace blood diagnosis. They decided on sweat.

Sweat is holistic. If you can detect and track sweat you could detect and track electrolytes in the sweat, like sodium, chloride, and ammonium ions, along with other biomarkers that could be detected for a more accurate analysis while doing it at your comfort. These patches are more accurate than present trackers. They are more indicative of the human health status and could be used for athletic performance measurement, workplace safety, clinical diagnosis, and even managing chronic health conditions.

For example, take the ability to detect sodium in sweat. With these threads on clothing, one could detect when he or she is hydrated or if there was electrolytic imbalance in the body in a matter of seconds. Lactate sensitivity is built into the threads and it can be used to indicate muscle fatigue for athletes or those who do strenuous activities. Cortisol, which is a stress hormone, could be detected in sweat and it can be used to assess emotional stress as well as metabolic and immune functions. The whole range of uses of these threads when sewn into clothing are immense. Their benefits are beyond recount. Even athletes could use the patches sewn into their clothing to monitor their physical exertion and aid them in predicting performance peaks or decline during competition.

The engineers came up with a unique ability to integrate these patches into clothing using flexible threads that were coated with conductor links and these threads are different for detecting different biomarkers that are present in sweat. The threads are then connected to a miniature circuit module linked to microprocessors with wireless capability for real-time communication with a smartphone.

When the engineers were asked why they chose sweat they said it is not only convenient to collect when clothing is on, it is easily accessible, can be collected non-invasively, and it correlates with the samples that could be derived from blood. I think I would love to invest in the company that would make this product commercially viable. It would replace many diagnostic techniques that we have today because sweat is a good surrogate for diagnostic fluid such as blood samples.

During tests it was discovered that the markers were accurate to about 5 to 30 seconds, enough for the real time needs of the average health conscious individual.

This is surely an innovative twist to helping those who are health conscious and individuals with chronic health problems; not to mention athletes who need to monitor all sorts of biological markers for performance enhancement. The engineers solved a problem.

You can read the full report at the Tufts website.

Coding Python ATM Machine That Gives Only $5 And $8

 

I recently went to the bank to withdraw some money. The attendant at the ATM told me the ATM can only give out money in 1,000 naira and 500 naira notes. When he told me, I started wondering if I could be able to code a machine that gives out money based on a specified denomination. So, I told myself, what if the machine can only give out $5 and $8 change. How can I code that?

 I nearly got knocked over on my way home thinking about the problem. But since problems are to be solved, I came up with a nice solution in my head that I want to describe.

How not to go about it.

Initially, I starting thinking of using the modulo 5 or modulo 8. That was nice and easy. But I came to a caveat, if I used modulo 8, I could only get change for amounts that were definitely multiples of 8 and not of 5. If I used modulo 5, I could only get amounts that were definitely modulo 5 and not 8. That would not work out. I started thinking of the nature of the problem before going down to the solution.

The nature of the problem.

What if the machine gives out change only in $5 and then has an internal memory that keeps track of how much change it could be able to give in $8 from what remains based on the amount already given out in $5? For example. It could not give out change for $12 because after taking out $5 what remains is $7. But it could give out change for $13, or $15.

Another aspect of the problem is that if your amount is below $5, it cannot give you any change because it only has $5 and $8. So, you have to forfeit it. Too bad!

Then, I had to start giving out change based on the smaller amount which is $5. First of all, I first check if the amount is a multiple of $5. If it is, that is easy, just give out change based on the number of $5 that is required by doing integer division. But if the amount is not a multiple of $5, then we need to do a little arithmetic here.

Let’s say we can give out $21 in change. What combinations of $5 and $8 do we use? Since we are starting out with $5 as the smaller amount. We first check if $21 is a multiple of $5. No, it’s not, so the change will include $8. Let’s give 0 $5. Is the remainder a multiple of $8? No because the remainder is $21 and it is not a multiple of $8. Then let’s give out the first $5. The remainder becomes $16. Is the remainder a multiple of $8? Yes, because 2 $8s give $16. So, we have our change.

Let’s take another example. Suppose we have $28. Is $28 a multiple of $5? No. So the change will include $8. Let’s give out 0 $5. Is the remainder, the original $28 a multiple of $8? No, it is not. So we know our change will include both $5 and $8. So, let’s give out 1 $5. After giving out a single $5, we remains is $23 which is not divisible by $8. So we give out the second $5, that is, $10. The remainder is now $18 which is not divisible by $8. So we give out the next $5 which makes it 3 of $5 or $15. The remainder is $13 which is not divisible by $8. But we have not exhausted the number of $5 we can give out, so we will keep giving out $5 until we exhaust our supply. So now we give out our 4th $5, which is $20. The remainder is now $8 and it is divisible by $8. So, we have our answer. If we have $28 the change we can give using just $5 and $8 is 4 $5 and 1 $8.

Now, let’s take an example of an amount we cannot give change for you to see how the code works. Let’s take $27. First we ask if it is divisible by $5? Not it cannot, so we will also give $8. So, giving just no $5, is $26 divisible by $8? No, it is not, so if we can give out change, then it will include both $5 and $8. But can we? I said we cannot. Let’s prove it. So, we’ll calculate the number of $5 iterations we can go through in the code. That is, the maximum number of $5 we can give since we are starting with $5 and that is 5 five dollars. So, we will keep giving out $5 and comparing the remainder with $8 until we exhaust our supply of $5. $27 divided by $5 without remainder is 5. So, we will give out $5 five times. Let’s give out the first $5. The remainder is $22 and it is not divisible by $8. So we give out the second $5 to make it $10. The remainder is $17 which is not divisible by $8. So we give out the third $5 making it $15 and the remainder, $12, is not divisible by $8. So we give out our fourth $5 to make it $20. The remainder after that is $7 which is not divisible by $8. We could stop here and say that since the remainder is lesser than $7 there is not way. But the code will check one more time for the fifth $5 and that is $25 to give a remainder of $1 which is not divisible by $8 without remainder. So, we cannot give out accurate change using just $5 and $8 when the amount is $27.

Here is the code snippet which you can run from here:

There is a handy function I have included, print_change. It takes the number of $5 and $8 along with the amount we can give change in and prints out the result on the screen. It makes the printing look elegant and beautiful.

And in case you did not notice, you could see from the printed result that our $5 and $8 ATM machine can give change for any amount above $27. It can give you change for all amounts from $28 to infinity. Just lovely machine that doesn’t like the poor.

You can download the script from this link if you desire and run it on your machine.

Email Validity Check Using A Python Filter

I encountered a problem recently of how to check for email validity. When doing web scraping or handling data sets, you get to play with emails a lot and having a handy function that does this check in an efficient way would be useful. So, I decided to come up with a function that does it.

 

In this article, I will be using a filter and a list of emails to do the check. I decided on using a list of emails because it would better help you understand what I am doing. Most times, if you are scraping data, you might have to implement a file handler for it. But not to worry, converting from one to the other is easy.

What are valid emails?

Valid emails have the form: username@websitename.extension. Each of the elements have their different rules. For example, username can usually contain letters, digits, dashes or underscores. Website name can only have letters and digits while the extension is usually at least 2 and maximum 3 letters in length. So, having that we will use regular expressions, or regex, to match for valid emails. Remember earlier posts, finding a match with regular expressions and also understanding python search and match regex methods where I told you that when you have to search strings think regex first? This is a handy application.

So, this is the regex we will be using. I have modularized it in a handy function called fun.


def fun(s):
    # return True if s is a valid email, else return False
    pattern = r'^[\w-]+@[a-zA-Z0-9]+\.[a-z]{2,3}$'   
    m = re.match(pattern,s)
    if m:
        return True
    else:
        return False 

Now let me explain the regex above in case you have not read the earlier posts on regex. Just look at the value for the variable, pattern. I will be explaining each of the strings in the pattern. "^[\w-]+" is the pattern to match the username. It says start from the beginning of the string. That is why we are using match in the next line. Match one or more letters, digits, or underscores (as \w), or match one or more dashes (as -). That’s for the username validation. The next string is "@". Just match @ after the earlier username match. Now comes the website name matching string. "[a-zA-Z0-9]" states that after the @ symbol, we should look for a website name as one or more letters or digits. That’s that. Next comes "\.". What I want to match here is the single period, ., after the website name, but I have to backslash it. Without a backslash the "." symbol says match any character but with a backslash it means to match the period, ., character. Then, lastly is the end of the pattern, the extension match. I denoted this with "[a-z]{2,3}$" and what this means is that match at least 2 or at most 3 lowercase letters which are at the end of the string. Note that this must be at the end of the string. Sometimes we could get an extension like .server and that would not be a match unless it is a .com, .co, .io, .net etc.

So, that’s for validating the email. Now how does a filter come in handy to do this.

What the filter function does.

The syntax of the filter function is filter(function, iterable). What it does is to take a sequence and filters it with the help of the given function. Each item in the sequence is filtered against the function and then a list is returned of the filtered sequence. So, what we are doing here is taking each email and filtering it against the fun function using the regex pattern match. The filter code is just a one liner – just filter it using the given function.


def filter_mail(emails):
    ''' input a sequence of email string 
    returns the elements in the sequence that returns True
    when filtered for email validity.'''
    return list(filter(fun, emails))  

In the complete code, I also included a print_email function. What the print_email function does is take an email, call the filter function which returns a list of the filtered emails, sort the list and then print the list. I wanted to make the print_email function a helper function to the filter_email function but decided it might not be readable that way, so had to leave it as is.


def print_email(emails):
    filtered_emails = filter_mail(emails)
    filtered_emails.sort()
    print(filtered_emails)

So, that is it – validating emails using a filter function with regular expressions. I have included two lists, a list containing invalid email and one containing valid emails for you to run and see how it was done.

You can download the script if you want to run it on your machine.

If you have any questions, you can leave it in the comments. Also, be sure to subscribe to my blog so you can receive new articles as they are posted.

Internet Security: Is There Such A Thing As An Unbreakable Code?

For centuries, people have been searching for ways to keep information from getting into the hands of the public. Cryptography gave them an answer to that. Cryptography has been used, both in its basic and sophisticated forms to hide sensitive information. Egyptian hieroglyphics contain the first known and verified example of ancient cryptography. In our age where internet is so rampant and people want to keep their personal information private, cryptography is gaining traction. But one cycle exists for all cryptography. First, someone finds a good code and starts using it, it becomes effective for some time and eventually someone somewhere breaks the code, rendering it ineffective. Because of this, people ask: Is there such a thing as an unbreakable code?

 

Can all encryption be broken

To help them answer this question and solve it, scientists came up with the concept of one-way functions. One-way functions are functions that are easy to compute on the given inputs but hard to invert. That is, you cannot get the inputs from the output when reversing it. One-way functions could make good candidates for code that cannot be easily broken. That is, it would be close to impossible to find an algorithm that would revert the output. Unfortunately, one-way functions are just a conjecture. But that conjecture has been behind much tools that have been built in cryptography, authentication, personal identification, and other data security applications.

Getting a one-way function that is feasible has huge ramifications in the internet age. It could solve the Internet security problem for good. Industries such as banking, telecommunications, and e-commerce would be in a hurry to apply it. Yes, it has been elusive but that is not to say that there have not been candidates.

One well known candidate for one-way functions involves the multiplication and factoring of prime numbers. To get the outputs, two prime numbers are given to a function and their product is computed. This function takes a quadratic time complexity. It is really hard to factor out the prime numbers given the output although it can be done in exponential time. Another candidate is the Rabin function which gave rise to the Rabin cryptosystem on the assumption that the Rabin function is one-way.

The two candidates above can be broken though if a really good mathematician knows how to write an efficient algorithm.

This problem was what Rafael Pass, Professor of computer science at Cornell Tech wanted to tackle. He believes that if he could find a really good and valid one-way function, then all internet security problem could be solved. Internet encryption would be safe for all. According to his postulate, a good one-way function is like lighting a match. After a match is lit, you cannot get back the sticks. They are now ashes. So, a good one-way function would be an encryption scheme in which the decryption would lie only in the hands of the person who encrypted it. To get a candidate, he looked to mathematics and to a field that is unrelated to cryptography – quantifying the amount of randomness in a string of numbers, or what is known as the Kolmogorov complexity.

The Kolmogorov complexity of an object is defined as the length of the shortest computer program that can generate that object as an output. The Kolmogorov complexity of a string that has a definite pattern to it, like ababababababab, which is writing ab 7 times, can easily be computed. But what if you have some random string? asdwer2345tgdhncjmckkjkd? How do you compute the Kolmogorov complexity in an efficient manner? It has been found that the Kolmogorov complexity for such random strings is computationally close to impossible. What makes it more infeasible is computing the time bounds of such an algorithm.

Taking from this idea, Professor Pass focused his research on whether an algorithm can solve the time-bounded Kolmogorov complexity. If such an algorithm exists, his research posits, then all cryptography can be broken. On the other hand, if no efficient algorithm exists for such a time-bound Kolmogorov complexity, then one-way functions do exist and they can be found.

His research has implications for encryption schemes that are widely used in the Internet. Popular social media platforms use encryption to make their platforms more secure, banks in e-banking platforms rely on encryption being more unbreakable, and overall, we depend on making sure our internet lives are kept free from the prying public. So, Professor Pass’ theory is of great interest and only time will tell when a really good algorithm can be found based on his research that would make sure our Internet security is compromised no matter what platform we are using.

Source for this article was from Cornell University.

The Big Advantage Of Understanding Python Regex methods

Regular expressions, or regex, in python is fun. It is a very fast way to search through a string for a given pattern. Whenever I have to search and I am dealing with a string, the first thing I do is to look for a solution in regex. If you know regular expressions, so many string operations will be easy. 

My earlier post, How To Find A Match When You Are Dating Floats, explains the basic syntax and use of regex. In this post, I will highlight two functions in using regex that could confuse anyone unless they understand how they work. I will also add a third method that serves as an extension to the two. 

So, what are the two methods? They involve searching for a pattern in a string and the two methods are re.search() and re.match(). They both do the same thing: search for a pattern in a string. 

How python re.match() works.

The syntax of python re.match() is re.match(pattern, string, flags=0). What it does is take a pattern, as the first argument and a string as the second argument and search for the pattern in the string. You could add in other flags if you want to like make it search multiline or ignore string case. 

Now, the subtlety of re.match() is that it returns a match object only if the pattern is at the beginning of the string. Else, if it is not at the beginning of the string, it returns None. This is very important to remember because many unsuspecting pythonistas have found themselves thinking their pattern was wrong when their match returned None. 

Let me illustrate this with a little code. 

 

From the code above, I changed the patterns. The first pattern started from the beginning of the string, line 4, and it returned a match object when I printed the object. But the second pattern, line 10, did not start from the beginning of the string. When you ran the code, you would have noticed that it printed None for this case. 

So, always remember, re.match() searches for the pattern at the beginning of the string. 

How python re.search() works

Now, the second method for searching for patterns is re.search(). The syntax is similar to re.match() but different from re.match() because it searches for the pattern anywhere in the string. Even if the string is multiline, it would still return a match if the pattern exists in the string. But it does not return a match for all locations where the pattern can be found in the string. Rather, it returns only the first match for the pattern. 

 

If you run the code above, you can see that it both gets a match at the beginning of the string and in the middle of the string. It gets a match anywhere in the string but returns a match object that corresponds only to the first match. 

So, remember the difference between these two useful methods and don’t make the mistake of fighting your terminal trying to understand why a pattern you thought was well formed turned out not to be giving you a match object. 

The bonus python regex method. 

This is a bonus method because it is the one I use most often. It is quite different from the earlier two. Remember the earlier two only return a single match object or None where there is no match. The bonus method is python re.findall(). This method, re.findall(), will scan the string from left to right for matches and will return all the matched patterns as a list of strings. Not a match object, but a list of strings. That comes quite useful several times you might say. I just love this method. Here is some little code to illustrate this. 

 

Notice that I am using the same code but just changing the methods. 

So you can see how powerful re.findall() is. It gives you the ability to see all the matches in a list, something that re.match() and re.search() do not make possible. 

I limited this post to just the rudimentary functionalities of all three methods. You can experiment with them now that you know how they work. Make out your own code with various concepts. 

And don’t forget to subscribe to my blog so that you can get updated articles as I publish them daily. The submit textbox is at the topright.

Matched content