Thursday, August 22, 2024

Demonstration Coding: Python Coding (Simple Problem Solving - 1)

Illustration of Python problem solving.

    Hello there! Good evening. Today, I’ve got a little bit of Python code to share with you. It's all about problem-solving! Honestly, I sometimes doubt my readiness for coding interviews—failure is always a possibility if I don’t try first, right? So, before that happens, I’ve decided to take some time to practice, even with the simplest of tests. Yes, we’re talking about problem-solving exercises. Let’s dive into the code!


Problem 1: Sum of an Array

You are given a list of integers. Write a function that returns the sum of all even numbers in the list.

Solve this:
Input: [1, 2, 3, 4, 5, 6]
Output: 12

Answer:

def returns_sum_of_even_number(list_: list) -> int:
'''Returns the sum of all even numbers in the list.'''
totalNum = 0
for num in list_:
if (num % 2) == 0:
totalNum += num
return totalNum
numbers = [1, 2, 3, 4, 5, 6]

print(returns_sum_of_even_number(numbers))

In the given example, the input list is [1, 2, 3, 4, 5, 6]. The even numbers in the list are 2, 4, and 6. Adding these together gives us 12, which is the correct result returned by the function.


Problem 2: Find Maximum Number

Write a function that takes a list of numbers and returns the largest number.

Solve this:

Input: [45, 23, 67, 89, 12]
Output: 89

Answer:

def find_maximum_number(nums: list) -> int:
'''Finds the maximum value in the list and returns maximum value.'''
maxNum = nums[0]
for num in nums:
if num > maxNum:
maxNum = num
return maxNum # To be simple you can returns max(nums)


nums = [45, 23, 67, 89, 12]
print(find_maximum_number(nums))

Above example, the input list is [45, 23, 67, 89, 12]. The code correctly identifies 89 as the maximum value and returns it.

Alternatively using built-in max function like this max(nums) in concise way to achieve the same result.

 

Problem 3: Count Vowels in a String

Write a function that counts the number of vowels (a, e, i, o, u) in a string. And write another function that counts each of found vowels (a, e, i, o, u) in a string.

Solve This:

Input: "Hello World! Today we're learning Python!"
Output: 11

Answer:

def vowels_counter(string: str) -> str:
'''Counts the number of vowels (a, e, i, o, u) in a string.'''
vowels = 'aiueo'
counter = 0
for value in string.lower():
if value in vowels:
counter += 1
return counter

Solve This:

Input: "Hello World! Today we're learning Python!"
Output:
a = 2
i = 1
u = 0
e = 4
o = 4

Answer:

def vowels_counter_two(string: str) -> dict:
'''Counts the number of each vowels (a, e, i, o, u) in a string.'''
vowels = {v: 0 for v in 'aiueo'}
for value in string.lower():
if value in vowels:
vowels[value] += 1
return vowels


Problem 4: Reverse a String

Write a function that takes a string and returns the string in reversed order.

Solve this:

Input: "Hello there! What\'s up! It\'s been late."
Output: ".etal neeb s'tI !pu s'tahW !ereht olleH"

Answer:

def reverse_string(string: str) -> str:
'''Returns the string reversed.'''
return string[::-1]

[::-1] the slice notation effectively extracts the entire string in reverse order, starting from the end and moving towards the beginning. This reversed string is then returned by the function.


Problem 5: FizzBuzz

Write a function that prints the numbers from 1 to 100. But for multiples of 3, print "Fizz" instead of the number, and for multiples of 5, print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz".

Solve This:

1. Numbers divisible by 3: Print "Fizz".
2. Numbers divisible by 5: Print "Buzz".
3. Numbers divisible by both 3 and 5: Print "FizzBuzz".

Answer:

def fizz_buzz() -> str:
'''function that prints the numbers from 1 to 100. But for multiples
of 3, print "Fizz" instead of the number, and for multiples of 5,
print "Buzz". For numbers which are multiples of both 3 and 5, print
"FizzBuzz".'''
number = 101
for num in range(1, number):
if (num % 3 == 0) and (num % 5 == 0):
print("FizzBuzz")
elif (num % 3 == 0):
print("Fizz")
elif (num % 5 == 0):
print("Buzz")
else:
print(num)

fizz_buzz()

The code correctly identifies multiples of 3 and 5, printing "Fizz", "Buzz", or "FizzBuzz" as appropriate. Therefore, it's producing the expected output.


Problem 6: Fibonacci Number

A Fibonacci number is a series of numbers in which each  Fibonacci number is obtained by adding the two preceding numbers. It means that the next number in the series is the addition of two previous numbers.

Formula:
Fn = Fn-1 + Fn-2

Solve this:

Input: fibonacci(9)
Output:
[0]
[0, 1]
[0, 1, 1]
[0, 1, 1, 2]
[0, 1, 1, 2, 3]
[0, 1, 1, 2, 3, 5]
[0, 1, 1, 2, 3, 5, 8]
[0, 1, 1, 2, 3, 5, 8, 13]
[0, 1, 1, 2, 3, 5, 8, 13, 21]

The Fibonacci sequences are: [0, 1, 1, 2, 3, 5, 8, 13, 21]

Answer:

def fibonacci(n: int) -> list:
fiboNums = []
a, b = 0, 1
count = 0

while count < n:
fiboNums.append(a)
a, b = b, a + b
count += 1

print(fiboNums)
print()
print(f"The Fibonacci sequences are: {fiboNums}")


fibonacci(9)

fibonacci(9) generates the first 9 Fibonacci numbers, which are [0, 1, 1, 2, 3, 5, 8, 13, 21]. The code correctly calculates and prints these numbers, demonstrating a proper implementation of the Fibonacci sequence algorithm.


End...

"That's all I can share with you. I hope this information will be helpful for people who need to learn. Don't worry, we'll continue learning and developing our skills. See you tomorrow and have a great day!"