Search

The Next Big Thing About the Cerebellum Might Open Frontiers In Understanding The Brain

The cerebellum, which in Latin means “the little brain,” is at the hindbrain in all vertebrates. North of the cerebellum is the cerebral cortex which forms its outer layer. Formerly, due to its folding nature, the cerebellum was thought to be smaller than the cerebral cortex and so not much involved in as much activities in human behavior and cognition as the cortex. But recent research has disproved that point.


The cerebellum as earlier said was thought to be smaller because it is arranged in hundreds of folds which make it look small in surface area but researchers using ultra-high-field MRI imaging together with specialized software have found that the surface of the cerebellum is much larger than was believed. It was even found to be even bigger than the cerebral cortex. It is now said to be approximately more than 80 percent of the cerebral cortex.  

This research would now open the way for more research into the benefits of the cerebellum for human behavior and cognition. This is because since the cerebellum is bigger than the cortex, it shows that human evolution is more advanced than other vertebrates, even those closer to man like the macaque monkeys. Therefore, it will help us to understand how over the years man has adapted better than other vertebrates to the environment and has used the advantage afforded it by a bigger cerebellum to develop advanced levels of cognitive abilities.

The researchers also found that while the cerebral cortex was well arranged and the body parts they were controlling well defined, that is not the case for the cerebellum. The cerebellum receives information from disparate parts of the body in a random manner. That means, areas for coordinating the shoulder could lie side by side with areas coordinating the foot. This gives humans the advantage over other animals of coordinating different body parts all at the same time from one central location, enhancing efficiency.

Also, the researchers found that the cerebellum must have a higher role in controlling emotional responses more than was earlier thought. It is established that the cerebellum is involved in movement-related functions, but this study also delved into the study of damaged cerebellums and found that people with such challenges had problems understanding their emotions. But further research has to be done in this area.

In the coming years, with the results from the MRI study, researchers will be able to better understand how the brain works and not confine their knowledge to thinking the control of body functions is limited to specific areas of the brain. Mapping the cerebellum will be an interesting new frontier for scientific advancement and further understanding of the human body.

Expectations And Realities Of Python Lists And Dictionaries

Have you ever got stumped when you tried assessing an item in a list or dictionary because you received a result which you never expected? Some would say they found a bug. Truly, that bug rests in the fact that you did not understand the subtleties between types when using a list or dictionary.

programming can sometimes be confusing

Take this little program that shows the subtleties in accessing items in a dictionary.

The dictionary has three items, with the keys being ints and the values as strings. That is the first difference you should notice. This is just illustrative because keys of dictionaries can also be any immutable type. On line 3, I asked the interpreter to tell me if 10, int type, was a key, and if ‘10’, string type, was a key. It says True for the former and False for the latter. This is a problem I encounter while teaching on a daily basis. Many persons tend to confuse the int for the string and vice versa. They are different values.

Then comes accessing the items in the dictionary. On line four, I inserted 10 as the key to the dictionary. Note this: as key and not the index. Python reported the string, ten, as I expected. But notice what happens on the line 5 when I used the string ‘10’ as the index. Python gave a KeyError.

So, this shows that types really matters when you are accessing dictionaries. The same goes for lists.

How to find items in a list and dictionary.

Imagine we have a list of names.

When you run it, you should notice that the int 1 is not in the list. I could have used some other value, maybe ‘one’ or maybe ‘Rose’ but the "in" operator shows us whatever is in the list. One problem I have encountered with most of my students is that they don’t understand the subtleties of the in operator. What I always tell them is to consider it as going through each item in the list and looking for a match. If it returns a match, it returns True, and if not it returns False.

Now, what if we mistake an int for a string or vice versa in the list. Let’s take another list. This time of years.

If you run it, what it prints out is:


2019 in list.
2019 is not in list.

What is going on? It’s just simple. The items in the list are strings but on line 10 we gave it an int. So, when programming you have to be very careful not to be carried away and confuse an int for a string and vice versa.

What about indexing and keys.

What we expect is that when we give the interpreter something like variable_name[index] when it is an iterable, we should expect to get an item in return. But what item we get depends on the data structure we are using. If variable_name is a list, we will get an item in the list with that index. But if variable_name is a dictionary, what we get is a value of the key, index.

That gets many people confused sometimes. Well, it is all about knowing that lists and dictionaries are arranged differently. A list contains references to items of value and each item is indexed from zero. For example, let’s take our list of names again.


names = ['david', 'michael', 'daniel']

The list has three items which are referenced through an index. 'david' is at index 0, 'michael' is at index 1, and 'daniel' is at index 2. So, we can access the items through the indices. For example this command, names[0] will return david, while names[2] will return daniel.

But this is different with dictionaries. Let’s take a dictionary of ints as keys and strings as values for example.


num_dict = {10 : 'ten', 20 : 'twenty', 30 : 'thirty'}

If we run the statement num_dict[0] we will get a KeyError. You know why? Because dictionaries accesses its values based on the keys and 0 is not a key in the num_dict dictionary. So, don’t confuse this with that of lists. To access the second item in the dictionary which is ‘twenty’, we need to get it through its key, and the key is an int, 20. So we run the statement thus: num_dict[20] which returns ‘twenty’.

Note that keys of dictionaries can only be immutable types, including sets, while values for dictionaries can be of any type.

I hope you enjoyed this article. If you like receiving more solved problems like this from this blog, just subscribe and it will be sent to your inbox the moment I publish a new article.

How to find a match when you are dating floats in python

Have you ever wondered how to do things the easy way when dealing with strings? Maybe you want to match some strings and you get stomped.

For example, take this hackerrank.com challenge. The challenge says that given a string, find out if it is a float where floats have the following requirements:

1.Number can start with +, -, or . symbol. 
2.Number must contain at least one decimal symbol
3.Number must have exactly one . symbol
4.Number must not give any exceptions when converted using the float(N) expression where N is the string. 

Now, those are some requirements. We could code this literally in python but it would be unbeautiful and illogical. That is where the beauty of regular expressions come to play.

What are regular expressions?

A regular expression, sometimes denoted as regex, is a sequence of strings that defines a search pattern. For example, if you want to search for the word ‘bat’ in battered, you could use ‘bat’ as a regular expression. Therefore, for the challenge above, we would be using regular expressions to solve it simply and beautifully.

Introduction to python regex.

You can get the full details of the use of python regex at the python regex documentation page here.

But I will just briefly cover the main points.

In python, regular expressions can be composed of metacharacters and also the ability to do repetitions on characters.

First the metacharacters.

The basic metacharacters are:

  1. [ ] which is the set metacharacter. Anything within the [] character class will be included in the search. For example having [mnc] means to search for the occurrence of an m, n, or c in the original string. This metacharacter can also be a range. For example [a-c] means to match the set of characters between a and c. if denoted by [a-z] that means to match any lower case character.
  2. Complementary to the above is the complement of the set, [^ ]. This means that anything within this complementary set should not be included in the search. For example if you get [^ cab] it means when searching do not include the lower case letter c, a, or b in the search.
  3. \d is the metacharacter that says search only decimal digits. It is equivalent to [0-9]
  4. \D says match any non-digit character. It is equivalent to the complement set, [^0-9]
  5. \s says match any white space while \S is the complement that says do not match any whitespace.
  6. \w says match any alphanumeric character while \W says do not match alphanumeric characters.

Now, let’s talk about repeating matches.

There are four symbols for match repetition.

  1. * metacharacter says that the preceding character matches zero or more time. That means you can match it 0, 1, 2 etc infinite times. For example do*g will match dg, dog, doog, doooog, etc. Now you get it.
  2. + metacharacter says match the preceding character one or more times. Note its difference from *. That means it can match 1, 2, 3 etc times to infinity but can never match 0 time. Examples are for the above again, if we have do+g it will match dog, doog, dooog etc but not dg. Get it?
  3. ? metacharacter says match one or zero times. It is called the optional repeating character. For example having da?d will match dd and also match dad and nothing else.
  4. Now the last repeating character is {m,n} character. It says match the preceding character at least m number of times and at most n number of times.

Now, I believe that is what we need to start building our matching pattern.

From our challenge above we say we have a date with floats. Let’s build a matching pattern from a string that fulfils starting from +, - or . symbols. Then has at least one decimal symbol and exactly one . symbol. That gives us the following regex pattern:

pattern = r'[+\-]?[0-9]*.{1,1}\d+'

Let's explain the pattern slowly. [+\-]? says that the float could start with optional + or -. Notice that we backslashed the – because this would make python to recognize the character. If we didn’t backslash it, python would think it is a range because it is within a set metacharacter, [ ].

Next is [0-9]?. This pattern says that any digit can occur zero of more times where it starts or does not start with + or -. Notice that we left off the . character in the first space. This is because if a float starts with the period, ., character, then it would not have any digit. So, that is why the period is coming after the digit.

The .{1,1} says that we will have at least and at most 1 period character. That is exactly one. Then the pattern says that it ends with a digit. There must be at least one digit. That is what the ending pattern, \d+, means. Notice that I interchanged [0-9] and \d. I just wanted you to realize that they are the same patterns.

One tool that you can use to build your regex patterns is found at regex101.com. This is a great site for using regular expressions. It has all the tools you need to understand regular expressions.

Now that we have created the regular expressions according to the challenge above, let us test it to see that it works. To do so, I compiled the pattern and created a comparison_list for the list of strings we want to check the pattern against. Then using a for loop I went through each of the items in the list looking for matches.

You would notice that the challenge involves casting the string to a float. I did not put that into the regex because that is not the function of regex but in the try block there is a conditional that checks for that.

Here is the code for the challenge.

You could create your own test suites and check out how your skill with regular expressions. They are a fun way to code. For other methods of the python regex, you could check out this post on python re match and python re search.

You could download the script for the solution here. Enjoy your date with regular expressions and floats.

Cubing the Fibonacci sequence using a map and lambda function in python

In this article, I will show how you can use the map and lambda function. We will be using a Fibonacci sequence to demonstrate this. The task we will be carrying out is to produce a Fibonacci sequence and then apply map to it in order to cube it while utilizing a lamda function. We will be introducing each of them in turn.

First, the Fibonacci sequence.

The Fibonacci sequence was discovered by an Italian mathematician, Leonardo Fibonacci. The sequence goes in the form 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... Take a good look at the sequence and you will notice something. You will notice that from the third term, you need to sum the previous two terms to get the third term. That is a2, which is 1, is the sum of a1 and a0. The general formula for each nth term is: An= an-1 + an-2and the first two terms are given as a0 = 0 and a1 = 1.

So, let’s generate some code that will give us the Fibonacci sequence.

In the Fibonacci(num) function, we used a recursive definition for the Fibonacci function. As you can see from the last return, we called the Fibonacci function twice, asking it to sum the return values of the two previous calls.

Here is the code:

Now, we will create a list of the sum of any number of Fibonacci functions. For example, if we want to generate a list of the first n Fibonacci function, we would use the following code:

You will notice from the code above that I created a for loop that calls the Fibonacci function for each number we want to return. If you print the list, you will get the value for each Fibonacci number and all arranged in the list. For example, if we wanted to create a list of first five Fibonacci numbers the list, n_list, when printed, gives [0. 1. 1. 2. 3].

Now that we have our Fibonacci list ready. What we do next is use the map function to cube each element in the list. To carry out this cubing, I will use a lambda function.

But first, what is a map function?

A map function applies a given function to each element of an iterable such as a list, set, tuple etc. The syntax for the map function is map(function, iterable, …). This shows that the map can take more than one iterable. For example, let the function be a square function. We want to apply the square function to each element of the list [1,2,3,4].

This is how we would do it. First we write the square function and then use it as the function parameter in map, then insert the iterable as the second parameter in map. Here is the code:

The output from the above is:

[1, 4, 9, 16]

All the numbers squared. Used on its own, the map function will give you a map object. So, in order to print it out, I had to cast the map object to a list using list(map(square, the_list)). You could even cast it to a set or any iterable if you want. Note that this map function is not used in our task. It is just an illustration. The one we use will come below.

Now that we have our map function set out, what about the lambda function.

The lambda function definition

A lambda function is a simple function. It is a function having no name. You know that normally you would define functions with a name, but for lambda functions you just want to return a value without defining a function. A lambda function can take any number of arguments but it has only one line or expression that it returns. I love lambda functions whenever I want to write a one-liner function. The square function above could have been done using a lambda function.

The syntax for a lambda function is: lambda x, y : expression, where x and y are parameters. The expression is any valid operation on the parameters. You use only one line for it.

So, let’s go back to our task for today. We want to take the Fibonacci numbers list and cube every item in the list. We have already created the Fibonacci numbers list above. Now, how do we cube it combining a map and lambda function.

I will use just this one-liner.

From the lambda function above you can see that I used x as the parameter and the expression was that each x should be raised to power 3 or cubed. So, what the map function and lambda function is saying in essence is that take each item in n_list and then raise it to be power of 3 (or cube it) with map making sure we walk through each element in n_list. Map produces a map object and this object is then cast into a list using the list function and the value is referenced by cubed_fib which was then printed in the next line.

That is all for our task for today. If you want to keep on reading of other tasks I will be producing, just subscribe to this blog using the email subscription form above.

Here is the full code for the task:

For those of you who would like to have the file, you can download it here.

[Cartoon] Indiscretion: Bad taste coupled with bad judgment!

Often, we find ourselves saying or doing things that we’d regret later. When an act or speech is not acceptable in relationships, business contracts, religious gatherings etc because it or they run counter to what is acceptable by society, it is generally classed as an indiscretion.

Indiscretion in life is generally used as examples easily rather than in other areas. For example, we can all quickly remember when we young that thoughtless remark made in front of our parents or people older than we are etc. Indiscretion in other areas like business could be costlier though than any in life.

The cartoon below is one. The indiscretion could cost a marriage and relationship, including trust, respect and love.

What can you say about it? Add your suggestions and notes in the comments box below, or join me on a Facebook group, mypallys. You can join on Whatsapp by writing your phone number on the comments box below!


[Video] Top youtube video for week of April 25: It's all about The Calbuco Volcano in Chile

On Wednesday, April 22, Calbuco Volcano in Chile erupted. The last time it did so was in 1972, about 42 years ago. Thousands of people have been evacuated from Ensenada, a town in the foothills of the volcano both on the first day of its eruption and also on Thursday, when Calbuco erupted a second time.

Youtube videos was rife with Chileans posting videos of the eruption. Smoke and ash were sent to the air like a scene from an apocalyptic film. It’s so wonderful, although the catastrophe isn’t. News reports that the immediate concerns are that the ash could contaminate nearby water sources and also cause respiratory problems.

I picked one of the trending videos from youtube, this, with 5,604,176 viewers, 12,158 likes and 622 dislikes. As you watch, you’ll be left pondering what power and secrets lie beneath the depths of the earth. I was whoops! This is my mythical!

Explosión volcán calbuco by Rodrigo Barrera


Matched content