Do not use map(); filter(); enumerate(); max(); min(); set(); reversed()
Use of set, tuple, or dictionary data types
Creating an Array during IB Exam
size =int(input())# Set size of the arrayarray = [Nonefor _ inrange(0, size)]for i inrange(0, size): array[i]=input()
This code mimics the creation fixed size arrays.
Explanation
Variable size is inputted to limit the size of the array
Variable array is initialized using list comprehension to create a list in python with size many empty spaces for future inputted value
Empty spaces are using the None keyword for empty values
the for loop iterates size many times to input values at index i
If inputting numeric values please type cast the input() function in the for loop
Find the maximum/minimum value in an array
This is assuming we can't use max() nor min()
# let array represent an array of valuesn =len(array)# Set n to length of given array# Initialize variables to hold the first valuesmallest = array[0]largest = array[0]# Logic: compare against the variable above, and update if necessaryfor i inrange(1, n):if array[i]< smallest: smallest = array[i]if array[i]> largest: largest = array[i]# If you only need either max or min, you can remove the unneeded if statement# This can also be put into a function/sub-program if needed
Find the maximum/minimum value's index in an array
This is where we find the index value of our max and min values
# let array represent an array of valuesn =len(array)# Set n to length of given array# Initialize variables to hold the first valuesmallest = array[0]largest = array[0]smallest_location =0# smallest index holderlargest_location =0# largest index holder# Logic: compare against the variable above, and update if necessaryfor i inrange(1, n):if array[i]< smallest: smallest = array[i] smallest_location = iif array[i]> largest: largest = array[i] largeest_location = i
Calculate the average from an array of values
# let array represent an array of valuesn =len(array)# Set n to length of given arraytotal_sum =0# initialize total_sum variablefor i inrange(0, n): total_sum = total_sum + array[i]average = total_sum / n
Linear search an array to find a target value
deflinear_search(array,target):# let array represent an array of values# let target the value we search for n =len(array)# Set n to length of given arrayfor i inrange(0, n):if array[i]== target:return i# end of forreturn-1# let -1 be an error code for not found# end of linear search
Binary search an array to find a target value
defbinary_search(array,target):# let array represent an array of values that are sorted# let target the value we search for n =len(array)# Set n to length of given array left =0 right = n -1while left <= right: mid = (left + right) //2# specify that // means a floor divisionif array[mid]< target: left = mid +1elif array[mid]> target: right = mid -1else:return mid# end of whilereturn-1# let -1 be an error code for not found# end of linear search
How to sort an array of data using Bubble Sort
# This algorithm sorts in ascending order# let array represent an array of valuesn =len(array)# Set n to length of given arrayswapped =True# initialized to true to start our algorithmwhile swapped ==True: swapped =Falsefor i inrange(1, n): left = array[i-1] right = array[i]if left > right:# for descending order change > to < array[i]= left array[i-1]= right swapped =True# end of inner for# end of outer while
How to sort connected arrays using bubble sort
# This algorithm sorts in ascending order# let array represent an array of values# let array2 represent another array of values# this behaviour is common for situation where:# one array has names# the other array has values for the names# often called "parallel arrays"# they connect values from different arrays by using consistent index valuessize =len(array)# Set size to length of given arraysize2 =len(array2)# Set size2 to length of other arrayswapped =True# initialized to true to start our algorithmwhile swapped ==True: swapped =Falsefor i inrange(1, n): left = array[i-1] right = array[i]if left > right:# for descending order change > to < array[i]= left array[i-1]= right swapped =True# Since the first array swapped values, swap the other array temp = array2[i] array2[i]= array2[i-1] array2[i-1]= temp# end of if# end of inner for# end of outer while
Determine the difference between one array and another
We are trying to output a new array/collection that determines which values in one of the array does not appear in the other
# let array1 represent an array# let array2 represent another array# let diff represent an array that holds the non-shared valuesa1_size =len(array1)a2_size =len(array2)for i inrange(0, a1_size): found =Falsefor j inrange(0, a2_size):if array1[i]== array2[j]: found =Truebreak# end of inner forif found ==False: diff.append(array1[i])# end of outer for# We must also do this again except starting from array2for j inrange(0, a2_size): found =Falsefor i inrange(0, a1_size):if array1[i]== array2[j]: found =Truebreak# end of inner forif found ==False: diff.append(array2[j])
Access individual digits from a number from right to left without String manipulation
# let num be an integer# our goal is to get the individual digits# starting from the "ones" column# Example: 1234 outputs # 4# 3# 2# 1while num >=0: current_digit = num %10 num = num //10# floor division of 10print(current_digit)