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)
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))
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)
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)
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)
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))
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.
Top comments (1)
Python assignments can be quite perplexing, and looming deadlines can make it even more stressful. I've been in that situation. That's when I discovered domyhomework123.com/python . Their specialized Python assignment helps not only rescue me from tight spots but also enhanced my coding skills. If you're navigating the challenges of Python assignments, this platform is a game-changer.