5 recruitment tasks with Python
Tags: python, recruitmant, testdev, beginners
Published: 2022-08-13 01:07:00

At this moment I'm seeking a work-study job (alternance in french) as a programmer so I have to pass lots of tests each day and I decided to show you some of them this time with Python. Here This is only one solution, there is lots of solution for each of those problems so try your own solution. 

1) Write a function that will print even numbers:

Solution 1 - while loop:

```
def evenNumbers(number):
    n=0
    while n < number:
        n += 1
        if n % 2 == 0:
            print(n, end="\t")

evenNumbers(10)
```

Solution 2 - for loop:

```
def evenNumbers2(number):
    numbers = range(0, number+1)
    for n in numbers:
        if n % 2 == 1:
            continue
        print(n, end="\t")

evenNumbers2(10)

```

![Solution 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rouf0so7sg2b9uc1go6k.png)

2) Find a prime number:

Solution:

```
def isPrimeNumber(number):
    if number > 1:
        for i in range(2,number):
            if number % i == 0:
                #print(f"{i} times {number//i} is {number}")
                #print(f"{number} is not a prime number")
                return False    
        return True
    else:
        return False    

print("7 is prime", isPrimeNumber(7))
print("9 is prime", isPrimeNumber(9))

```

![is prime number function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5lqrvdyobthazqs9blq.png)

So we know how to find if a number is prime we can use it to find a prime number in a range:

```
def allPrimeNumbers(num):
    for i in range(num + 1):
        if isPrimeNumber(i):
            print(i, end = ", ")


print("prime numbers to 100")
allPrimeNumbers(100)

```

![all prime numbers in a range of 100](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oa9134orvxqr4g5bvb3f.png)

3) How to know if a number is a palindrome:
Solution:

```
def palindrome_number (number):
    number_str = str(number) # it is necessary to change the number to string to compare each digit
    number2_str = number_str[::-1] # number2_str is renversed by [::–1] slice which starts from last sign

    if number_str == number2_str:
        print(f"{number} this is a palindrome")
    else:
        print(f"{number} isn't a palindrome")  

palindrome_number(123321)
```

![function palindrome number](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mazztuzd7yks1kof1h5i.png)


4) Fizz Buzz is one of most favorite exercises given to juniors and not only:

```
def fizz_buzz(number):
    for i in range(1, number+1):
        if i % 3 == 0 and i % 5 == 0:
            print("fizzBuzz",end=", ")
        elif i % 3 == 0:
            print ("fizz",end=", ")
        elif i % 5 == 0:
            print ("buzz",end=", ")
        else:
            print (i,end=", ")

fizz_buzz(20) 
```

![fizz_buzz function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qk2syutd6bz1t7314a5g.png)

Yes,  I know it is better and proper to use "return" than "print" 😉

5) This exercise was more difficult for me to understand: Fibonacci. Here I will show you 3 solutions:

Solution 1 - recursive function:

```
def fib_r(n):
    assert n>=0
    if n<=1: 
        return n
    else:
        return fib_r(n-1)+fib_r(n-2)

print(fib_r(10))
```

Solution 2 - iterative function

```
def fib_i(num):
    a, b = 0,1 
   
    for i in range(0, num):
        a, b = b, a + b
        print(a, end= ", ")
        
print(fib_i(10))

```

![iterative function fibonacci](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xkzg3sztq7q3t73v3jtu.png)

Solution 3 - function with generator

```
def fib_g():
    yield 0
    a=0
    b=1
    while True:
        yield b
        a,b = b,a+b
        
g=fib_g()

for i in range (11): 
    print( next(g), end = ", ")

```
These are just 5 tasks that I encountered during various recruitment tests and are only examples of solutions. If you have other suggestions, don't hesitate to leave a comment.