Question 13:
Create a function that takes a Roman numeral as its argument and returns its value as a numeric decimal integer. You don't need to validate the form of the Roman numeral.
Modern Roman numerals are written by expressing each decimal digit of the number to be encoded separately, starting with the leftmost digit and skipping any 0s. So 1990 is rendered "MCMXC" (1000 = M, 900 = CM, 90 = XC) and 2008 is rendered "MMVIII" (2000 = MM, 8 = VIII). The Roman numeral for 1666, "MDCLXVI", uses each letter in descending order.
Example: solution('XXI'); // should return 21
Help: Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1,000
Pseudocode:
function roman_to_integer(roman_numeral):
create a dictionary roman_numeral_map with keys as Roman numerals and values as their corresponding integer values
initialize an empty variable value to store the final integer representation of the Roman numeral
for each character in the roman_numeral string:
get the current integer value of the character from roman_numeral_map
if the current character is followed by another character with a higher integer value:
subtract the current integer value from the value variable
else:
add the current integer value to the value variable
return the value variable
Code Implementation in Python.
def roman_to_integer(roman_numeral):
roman_numeral_map = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
value = 0
for i in range(len(roman_numeral)):
current_value = roman_numeral_map[roman_numeral[i]]
if i + 1 < len(roman_numeral) and current_value < roman_numeral_map[roman_numeral[i + 1]]:
value -= current_value
else:
value += current_value
return value
# Example usage
print(roman_to_integer('XXI')) # Output: 21
Question 14
There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.
Example stringList = [‘ab’, ‘ab’, ‘abc’] queryList = [‘ab’, ‘abc’, ‘bc’]
There are 2 instances of 'ab ', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results = [2,1,0].
Function Description Your function must return an array of integers representing the frequency of occurrence of each query string in stringList.
Your function should also have the following parameters: stringList[n] - an array of strings to search queries[q] - an array of query strings Returns int[q]: an array of results for each query
Answer
class StringCounter:
def __init__(self, string_list, query_list):
self.string_list = string_list
self.query_list = query_list
self.query_counts = {}
for query in self.query_list:
self.query_counts[query] = 0
def count_occurrences(self):
for string in self.string_list:
for query in self.query_list:
if string == query:
self.query_counts[query] += 1
def get_results(self):
results = []
for query in self.query_list:
results.append(self.query_counts[query])
return results
if __name__ == "__main__":
string_list = ['ab', 'ab', 'abc']
query_list = ['ab', 'abc', 'bc']
string_counter = StringCounter(string_list, query_list)
string_counter.count_occurrences()
results = string_counter.get_results()
print(results)
Explanation
Definition of the class StringCounter that takes two lists as input: string_list and query_list.
The __init__ method initializes the class attributes and the count_occurrences() method counts the occurrences of each query string in the string_list.
The get_results() method returns a list of integers representing the frequency of occurrence of each query string.
The if __name__ == "__main__": block creates an instance of the StringCounter class, calls the count_occurrences() method, and prints the results returned by the get_results() method.