Read sections 6.1-6.6 in chapter 6 of the textbook Explorations in Computing.
def print_sum_of_each_row(matrix)
sum = 0
for row in 0..matrix.length do
for col in 0..matrix[row].length do
sum = sum + matrix[row][col]
end
print sum
print "\n"
end
end
Explain his errors and correct the function. NOTE: There are 3 errors in his function.
Algorithm 1: Park the cars one next to another, in order of the departure times. He puts a piece of paper inside each car with its departure time. When someone comes to pick up their car, he goes to the first car parked in the row and drives it up to the owner.Algorithm 2: Park the cars in any available space. On a piece of paper inside the car, he writes the departure time for that car and the number of the parking spot for the car that will depart next in time. He also keeps track of the first car that will be leaving on a piece of paper. When someone comes to pick up their car, he goes to the car on the piece of paper and drives it up to the owner, keeping the piece of paper from inside that car so he knows where the next departing car is.
def mystery(word)
n = word.length
stack = []
for i in 0..n-1 do
x = word[i]
stack.push(x)
end
new_word = ""
for i in 0..n-1 do
y = stack.pop
new_word += y.chr
end
return word == new_word
end
Run this function using irb and call this function with
various strings containing single words.
Describe what this function is computing in one sentence.
HINT: To figure out the function's purpose, you will need
to read through the algorithm that is coded in the function
to get clues as to what it is doing overall.
Also, give 3 examples of words that will result in this function returning true and 3 examples of words that will result in this function returning false.
(Ruby information: If word is a string, then word[i] gives us the ASCII code for the ith character in the string. If y is the ASCII code of a character, then new_word += y.chr appends the character with ASCII code y on to new_word.)
def h(string, table_size)
k = 0
for i in 0..string.length-1 do
k = string[i] + k * 256
end
return k % table_size
end
Recall that string[i] returns the ASCII code of the ith character of string. Here are the ASCII codes for the lowercase letters:
a b c d e f g h i j k l m 97 98 99 100 101 102 103 104 105 106 107 108 109 n o p q r s t u v w x y z 110 111 112 113 114 115 116 117 118 119 120 121 122
60 83 29 55 13 91 37 76 44
60 83 29 55 13 91 37 76 44
NOTE: For this problem, you will probably need to show the max-heap after each individual element is inserted so you don't get lost.
vertices = ["New York", "Chicago", "Los Angeles", "Dallas", "Atlanta"]
edges = [ [0, 4, 5, 6, 2],
[4, 0, 10, 3, 1],
[5, 10, 0, 7, 9],
[6, 3, 7, 0, 8],
[2, 1, 9, 8, 0] ]