Data Science Exercises

In this exercise, we will practice issues related to loops and functions. In this article, the topics learned in the past will be included so if you don’t read the previous articles you should read these articles below.

1 – Function Exercise

Let’s start with function structures first, functions are used in many data science projects, the function to be created in this exercise will print the output of the desired number of coefficients, you should read the control structures and functions for this exercise.

increaseFunction <- function(count , n)
{
   a <- 0
   while(a < count)
   {
      a <- a + 1
      n <- n + 1
   }
   return(n)
}
increaseFunction(10 , 1)

The above function does the increase by the count argument, and n is the number to increment. You can change the logic of the function by tampering, for example, you can get the number of increments from the user.

2 – Loop Excercise

Loops are required for important operations such as functions, we used a loop in the example above, we will work on conditional loops in this section, you can read the article on control structures to work on this topic.

a <- 0
while(a < 10)
{
    a <- a + 1;
    if(a %% 2 == 0)
    {
        print("even number")
    }else{
        print("odd number")
    }
}

In the example above, we wrote a program that checks that the first 10 numbers are not even or odd, the operator %% gives us the remaining number, and if the remaining number is 0, it is an even number.

3 – Big Excercise

This exercise can be confusing and includes both functions and control structures. I suggest you reread before you start. This project will be in graphics so you may need to read graphics in R.

increase <- function(x , y , z)
{
    count <- 0;
    d <- c()
    while(count < y)
    {
        count <- count + 1;
        x <- x + z;
        d[count] <- x
    }
    return(d)
}

barplot(increase(1 , 365 , 0.1) , 1:365)

This project is a bit long, so let’s look at it piecemeal, let’s first examine the increase function. x is the number to increment the argument, y determines how many times the argument will increment, finally, z determines how much the function will increment.

The value of X is obtained by increasing the number 1 by the number 0.1 365 times. Y takes the value 1: 365 to divide this value.

CONGRATULATIONS, YOU FINISHED THE FIRST EXERCISE!

Leave a Reply

Your email address will not be published. Required fields are marked *