Hello Guys!!...
It has been more than a week that I didn't post the next tutorial. So, here it is.....
Before proceeding further, please visit these tutorials (link provided below), if you want to start from beginning.
Session 1
Session 2
Session 3
Question:
Given an array of ints, return True if 6 appears as either the first or last element in the array. The array will be length 1 or more.
Check for these cases:
first_last6([1, 2, 6]) → True
first_last6([6, 1, 2, 3]) → True
first_last6([13, 6, 1, 2, 3]) → False
Solution:
def first_last6(nums):
if nums[0] == 6 or nums[len(nums)-1] == 6:
print("True")
else:
print("False")
first_last6([1, 2, 6]) #→ True
first_last6([6, 1, 2, 3]) #→ True
first_last6([13, 6, 1, 2, 3]) #→ False
#----------------------------------------#
#----------------------------------------#
Question:
Given an array of ints, return True if the array is length 1 or more, and the first element and the last element are equal.
Check for these cases:
same_first_last([1, 2, 3]) → False
same_first_last([1, 2, 3, 1]) → True
same_first_last([1, 2, 1]) → True
Solution:
def same_first_last(nums):
if len(nums) >= 1 and nums[0] == nums[len(nums)-1]:
print("True")
else:
print("False")
same_first_last([1, 2, 3]) #→ False
same_first_last([1, 2, 3, 1]) #→ True
same_first_last([1, 2, 1]) #→ True
#----------------------------------------#
#----------------------------------------#
Question:
Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.
Check for these cases:
make_pi() → [3, 1, 4]
Solution:
import math
def make_pi():
arr = []
num = int(math.pi * 100)
arr1 = []
# Append the array with
while num > 0:
arr.append(num % 10)
num = int(num/10)
# Reverse the array
start = 0
end = len(arr)-1
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -=1
print(arr)
make_pi() #→ [3, 1, 4]
#----------------------------------------#
#----------------------------------------#
Question:
Given 2 arrays of ints, a and b, return True if they have the same first element or they have the same last element. Both arrays will be length 1 or more.
Check for these cases:
common_end([1, 2, 3], [7, 3]) → True
common_end([1, 2, 3], [7, 3, 2]) → False
common_end([1, 2, 3], [1, 3]) → True
Solution:
def common_end(arr1, arr2):
if arr1[0] == arr2[0] or arr1[len(arr1)-1] == arr2[len(arr2)-1]:
print("True")
else:
print("False")
common_end([1, 2, 3], [7, 3]) #→ True
common_end([1, 2, 3], [7, 3, 2]) #→ False
common_end([1, 2, 3], [1, 3]) #→ True
#----------------------------------------#
#----------------------------------------#
Question:
Given an array of ints length 3, return the sum of all the elements.
Check for these cases:
sum3([1, 2, 3]) → 6
sum3([5, 11, 2]) → 18
sum3([7, 0, 0]) → 7
Solution:
def sum3(arr):
sum = 0
for num in arr:
sum += num
print(sum)
sum3([1, 2, 3]) #→ 6
sum3([5, 11, 2]) #→ 18
sum3([7, 0, 0]) #→ 7
#----------------------------------------#
#----------------------------------------#
Question:
Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
Check for these cases:
rotate_left3([1, 2, 3]) → [2, 3, 1]
rotate_left3([5, 11, 9]) → [11, 9, 5]
rotate_left3([7, 0, 0]) → [0, 0, 7]
Solution:
def rotate_left3(arr):
temp = 0
temp = arr[0] # store the 1st no. in the array
for i in range(len(arr)):
if i+1 < len(arr): # because when i = 2, arr[i+1] i.e. arr[3], which is invalid.
arr[i] = arr[i+1]
arr[len(arr)-1] = temp # assign the temp variable to the last index
print(arr)
rotate_left3([1, 2, 3]) #→ [2, 3, 1]
rotate_left3([5, 11, 9]) #→ [11, 9, 5]
rotate_left3([7, 0, 0]) #→ [0, 0, 7]
#----------------------------------------#
#----------------------------------------#
Question:
Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.
Check for these cases:
reverse3([1, 2, 3]) → [3, 2, 1]
reverse3([5, 11, 9]) → [9, 11, 5]
reverse3([7, 0, 0]) → [0, 0, 7]
Solution:
def reverse3(arr):
temp = 0 # temporary variable
if len(arr) % 2 == 0:
# even length of the array
for i in range(int(len(arr)/2)):
temp = arr[i]
arr[i] = arr[len(arr)-1-i]
arr[len(arr)-1-i] = temp
else:
# odd length of the array
for i in range(int((len(arr)-1)/2)):
temp = arr[i]
arr[i] = arr[len(arr)-1-i]
arr[len(arr)-1-i] = temp
print(arr)
reverse3([1, 2, 3]) #→ [3, 2, 1]
reverse3([5, 11, 9]) #→ [9, 11, 5]
reverse3([7, 0, 0]) #→ [0, 0, 7]
reverse3([7, 12, 0, 0]) #→ [0, 0, 7]
#----------------------------------------#
#----------------------------------------#
Question:
Given an array of ints length 3, figure out which is larger, the first or last element in the array, and set all the other elements to be that value. Return the changed array.
Check for these cases:
max_end3([1, 2, 3]) → [3, 3, 3]
max_end3([11, 5, 9]) → [11, 11, 11]
max_end3([2, 11, 3]) → [3, 3, 3]
Solution:
def max_end3(arr):
temp = 0 # temporary variable
temp = arr[0]
for i in range(int((len(arr)-1)/2)): # iterate from 0
if arr[i] <= arr[len(arr)-1-i]:
temp = arr[len(arr)-1-i]
for i in range(len(arr)):
arr[i] = temp
print(arr)
max_end3([1, 2, 3]) #→ [3, 3, 3]
max_end3([11, 5, 9]) #→ [11, 11, 11]
max_end3([2, 11, 3]) #→ [3, 3, 3]
#----------------------------------------#
#----------------------------------------#
Question:
Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.
Check for these cases:
sum2([1, 2, 3]) → 3
sum2([1, 1]) → 2
sum2([1, 1, 1, 1]) → 2
Solution:
def sum2(arr):
sum = 0
if len(arr) == 0:
sum = 0
elif len(arr) < 2:
sum = arr[0]
else:
for i in range(2):
sum += arr[i]
print(sum)
sum2([1, 2, 3]) #→ 3
sum2([1, 1]) #→ 2
sum2([1, 1, 1, 1]) #→ 2
#----------------------------------------#
#----------------------------------------#
Question:
Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements.
Check for these cases:
middle_way([1, 2, 3], [4, 5, 6]) → [2, 5]
middle_way([7, 7, 7], [3, 8, 0]) → [7, 8]
middle_way([5, 2, 9], [1, 4, 5]) → [2, 4]
Solution:
def middle_way(arr1, arr2):
arr3 = [] #initialize the 3rd array
mid_val1 = 0
mid_val2 = 0
# Case 1: odd length
if len(arr1) % 2 != 0 and len(arr2) % 2 != 0:
mid_val1 = arr1[int((len(arr1)-1)/2)] # assigning the mid value of arr1
mid_val2 = arr2[int((len(arr1)-1)/2)] # assigning the mid value of arr2
arr3.append(mid_val1)
arr3.append(mid_val2)
print(arr3)
# Case 2: even length
else:
print("FALSE!! \nThe arrays are not of desired length")
middle_way([1, 2, 3], [4, 5, 6]) #→ [2, 5]
middle_way([7, 7, 7], [3, 8, 0]) #→ [7, 8]
middle_way([5, 2, 9], [1, 4, 5]) #→ [2, 4]
#----------------------------------------#
#----------------------------------------#
Question:
Given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more.
Check for these cases:
make_ends([1, 2, 3]) → [1, 3]
make_ends([1, 2, 3, 4]) → [1, 4]
make_ends([7, 4, 6, 2]) → [7, 2]
Solution:
def make_ends(arr):
arr_new = []
arr_new.append(arr[0])
arr_new.append(arr[len(arr)-1])
print(arr_new)
make_ends([1, 2, 3]) #→ [1, 3]
make_ends([1, 2, 3, 4]) # → [1, 4]
make_ends([7, 4, 6, 2]) #→ [7, 2]
#----------------------------------------#
#----------------------------------------#
Question:
Given an int array length 2, return True if it contains a 2 or a 3.
Check for these cases:
has23([2, 5]) → True
has23([4, 3]) → True
has23([4, 5]) → False
Solution:
def has23(arr):
bool = False
for num in arr:
if num == 2 or num == 3:
bool = True
print(bool)
has23([2, 5]) #→ True
has23([4, 3]) #→ True
has23([4, 5]) #→ False
#----------------------------------------#
#----------------------------------------#