Skip to content

Instantly share code, notes, and snippets.

@lw13377
Created June 5, 2024 08:50
Show Gist options
  • Save lw13377/3b8d5adb0abeabda28f74fdf8216011d to your computer and use it in GitHub Desktop.
Save lw13377/3b8d5adb0abeabda28f74fdf8216011d to your computer and use it in GitHub Desktop.
lesson 10 homework
# Homework Lesson 10 - Workshop - Homework
# READ CAREFULLY THE EXERCISE DESCRIPTION AND SOLVE IT RIGHT AFTER IT
################################################################################
### When solving coding challenges, think about the time complexity (Big O). ###
################################################################################
# Challenge 1
# Multiplication of a three-digit number
#
# A program gets a three-digit number and has to multiply all its digits.
# For example, if a number is 349, the code has to print the number 108, because 3*4*9 = 108.
#
# Hints:
# Use the modulus operator % to get the last digit.
# Use floor division to remove the last digit
def multiplication_of_three(number):
result = 1
for i in range(3):
result *= (number % 10)
number = number // 10
return result
print(multiplication_of_three(349))
# ---------------------------------------------------------------------
# Challenge 2
# Sum and multiplication of even and odd numbers.
#
# You are given an array of integers. Your task is to calculate two values: the sum of
# all even numbers and the product of all odd numbers in the array. Please return these
# two values as a list [sum_even, multiplication_odd].
#
# Example
# For the array [1, 2, 3, 4]:
#
# The sum of all even numbers is 2 + 4 = 6.
# The product of all odd numbers is 1 * 3 = 3.
# The function should return the list [6, 3].
def sum_even_and_product_odd(arr):
sum_even = 0
product_odd = 1
for i in arr:
if i % 2 == 0:
sum_even += i
else:
product_odd *= i
return sum_even, product_odd
print(sum_even_and_product_odd([1, 2, 3, 4]))
# ---------------------------------------------------------------------
# Challenge 3
# Invert a list of numbers
#
# Given a list of numbers, return the inverse of each. Each positive becomes a negative,
# and the negatives become positives.
#
# Example:
# Input: [1, 5, -2, 4]
# Output: [-1, -5, 2, -4]
def invert_list(arr):
inverted_arr = []
for i in arr:
inverted_arr.append(i * -1)
return inverted_arr
print(invert_list([1, 2, -3, 4, -5]))
# ---------------------------------------------------------------------
# Challenge 4
# Difference between
#
# Implement a function that returns the difference between the largest and the
# smallest value in a given list.
# The list contains positive and negative numbers. All elements are unique.
#
# Example:
# Input: [3, 5, 7, 2]
# Output: 7 - 2 = 5
def max_diff(arr):
# Check if the list is empty
# If it is, return 0 as there's no difference to be calculated
if len(arr) == 0:
return 0
new_arr = sorted(arr)
return new_arr[-1] - new_arr[0]
print(max_diff([4, 2, 21, 6, 5]))
# If the list is not empty,
# proceed with the rest of the code.
# Your code here
# ---------------------------------------------------------------------
# Challenge 5
# Sum between range values
# You are given an array of integers and two integer values, min and max.
# Your task is to write a function that finds the sum of all elements in the
# array that fall within the range [min, max], inclusive.
#
# Example:
# arr = [3, 2, 1, 4, 10, 8, 7, 6, 9, 5]
# min_val = 3
# max_val = 7
#
# Output: 25 (3 + 4 + 5 + 6 + 7)
#
# Hint: Iterate through each number (num) in the array (arr) and check if the current number falls within the range [min_val, max_val].
def sum_between_range(arr, min_val, max_val):
total = 0
sorted_arr = sorted(arr)
for i in sorted_arr:
if min_val <= i <= max_val:
total += i
return total
print(sum_between_range([3, 2, 1, 4, 10, 8, 7, 6, 9, 5], 3, 7))
##### LAST TWO PRACTICES FROM TODAYS LESSON THAT WE DIDNT FINISH
def is_almost_palindrome(word):
for letter in range(len(word)):
current = word[:letter] + word[letter + 1:]
if current == current[::-1]:
return True
return False
print(is_almost_palindrome("rakdar"))
############
def buy_and_sell_stock(prices):
max_profit = 0
for i in range(len(prices) - 1):
if prices[i+1] > prices[i]:
max_profit = max_profit + prices[i+1] - prices[i]
return max_profit
print(buy_and_sell_stock([12, 15, 18, 9, 7, 12, 10, 14]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment