Appearance
question:def minRemovals(S: str) -> int: Given a string S consisting of lowercase letters, find the minimum number of characters to be removed from S to make each character appear an even number of times. >>> minRemovals("aabbcc") 0 >>> minRemovals("abcde") 3 from solution import minRemovals def test_all_even(): assert minRemovals("aabbcc") == 0 def test_all_odd(): assert minRemovals("abcde") == 5 def test_mixed_odd_even(): assert minRemovals("aabccde") == 3 def test_single_character(): assert minRemovals("a") == 1 def test_empty_string(): assert minRemovals("") == 0 def test_large_input(): large_input = "a" * 50000 + "b" * 50001 assert minRemovals(large_input) == 1
answer:def minRemovals(S): This function returns the minimum number of characters to be removed from the input string S to make each character appear an even number of times. from collections import Counter # Count occurrences of each character char_count = Counter(S) # Find characters that appear an odd number of times odd_count_chars = sum(1 for count in char_count.values() if count % 2 != 0) # To make an odd count to even, we need to remove 1 character return odd_count_chars
question:def merge_meetings(meetings): Merge overlapping meeting times. :param meetings: A list of tuples where each tuple contains the start and end time of a meeting in 'HH:MM' format. :return: A list of tuples with consolidated meeting times. >>> merge_meetings([('09:00', '10:30'), ('11:00', '12:00'), ('09:15', '10:00'), ('12:30', '14:00'), ('14:00', '15:00')]) [('09:00', '10:30'), ('11:00', '12:00'), ('12:30', '15:00')] >>> merge_meetings([('08:00', '09:15'), ('09:00', '10:30'), ('10:15', '11:30'), ('11:45', '13:00')]) [('08:00', '11:30'), ('11:45', '13:00')]
answer:def merge_meetings(meetings): Merge overlapping meeting times. :param meetings: A list of tuples where each tuple contains the start and end time of a meeting in 'HH:MM' format. :return: A list of tuples with consolidated meeting times. # Helper function to convert time in 'HH:MM' format to minutes def to_minutes(time): hours, minutes = map(int, time.split(':')) return hours * 60 + minutes # Helper function to convert minutes back to 'HH:MM' format def to_time(minutes): hours = minutes // 60 minutes = minutes % 60 return f"{hours:02d}:{minutes:02d}" # Convert meeting times to minutes for easier comparison and sorting meetings_in_minutes = [(to_minutes(start), to_minutes(end)) for start, end in meetings] # Sort meetings by start time (and by end time if start times are the same) meetings_in_minutes.sort() merged_meetings = [] for start, end in meetings_in_minutes: if not merged_meetings or merged_meetings[-1][1] < start: merged_meetings.append((start, end)) else: merged_meetings[-1] = (merged_meetings[-1][0], max(merged_meetings[-1][1], end)) # Convert merged meeting times back to 'HH:MM' format return [(to_time(start), to_time(end)) for start, end in merged_meetings]
question:class FileSystem: def __init__(self): self.files = {} self.timestamp = 0 def _increment_timestamp(self): self.timestamp += 1 def _update_modtime(self, filename): self._increment_timestamp() self.files[filename]["modtime"] = self.timestamp def create(self, filename): if filename not in self.files: self.files[filename] = {"content": "", "modtime": None} self._update_modtime(filename) def write(self, filename, data): if filename in self.files: self.files[filename]["content"] = data self._update_modtime(filename) def read(self, filename): if filename in self.files: return self.files[filename]["content"] return "File not found" def delete(self, filename): if filename in self.files: del self.files[filename] def modtime(self, filename): if filename in self.files: return self.files[filename]["modtime"] return "File not found" def execute_operations(n, operations): Execute a series of file system operations and return the results of READ and MODTIME operations. Parameters: - n (int): Number of operations. - operations (List[str]): A list of operations to be executed on the file system. Returns: - List[Any]: The results of READ and MODTIME operations in the order they appear. >>> execute_operations(8, ["CREATE file1", "WRITE file1 hello", "CREATE file2", "WRITE file2 world", "READ file1", "READ file2", "MODTIME file1", "DELETE file1"]) ["hello", "world", 2] fs = FileSystem() output = [] for operation in operations: parts = operation.split() if parts[0] == "CREATE": fs.create(parts[1]) elif parts[0] == "WRITE": filename = parts[1] data = " ".join(parts[2:]) fs.write(filename, data) elif parts[0] == "READ": result = fs.read(parts[1]) output.append(result) elif parts[0] == "DELETE": fs.delete(parts[1]) elif parts[0] == "MODTIME": result = fs.modtime(parts[1]) output.append(result) return output import pytest def test_sample_input(): n = 8 operations = [ "CREATE file1", "WRITE file1 hello", "CREATE file2", "WRITE file2 world", "READ file1", "READ file2", "MODTIME file1", "DELETE file1" ] expected_output = ["hello", "world", 2] assert execute_operations(n, operations) == expected_output def test_file_not_found_read(): n = 1 operations = ["READ non_existent_file"] expected_output = ["File not found"] assert execute_operations(n, operations) == expected_output def test_file_not_found_modtime(): n = 1 operations = ["MODTIME non_existent_file"] expected_output = ["File not found"] assert execute_operations(n, operations) == expected_output def test_create_and_delete_file(): n = 5 operations = [ "CREATE file1", "DELETE file1", "READ file1", "MODTIME file1" ] expected_output = ["File not found", "File not found"] assert execute_operations(n, operations) == expected_output def test_multiple_write_operations(): n = 5 operations = [ "CREATE file1", "WRITE file1 data1", "WRITE file1 data2", "READ file1", "MODTIME file1" ] expected_output = ["data2", 3] assert execute_operations(n, operations) == expected_output
answer:class FileSystem: def __init__(self): self.files = {} self.timestamp = 0 def _increment_timestamp(self): self.timestamp += 1 def _update_modtime(self, filename): self._increment_timestamp() self.files[filename]["modtime"] = self.timestamp def create(self, filename): if filename not in self.files: self.files[filename] = {"content": "", "modtime": None} self._update_modtime(filename) def write(self, filename, data): if filename in self.files: self.files[filename]["content"] = data self._update_modtime(filename) def read(self, filename): if filename in self.files: return self.files[filename]["content"] return "File not found" def delete(self, filename): if filename in self.files: del self.files[filename] def modtime(self, filename): if filename in self.files: return self.files[filename]["modtime"] return "File not found" def execute_operations(n, operations): fs = FileSystem() output = [] for operation in operations: parts = operation.split() if parts[0] == "CREATE": fs.create(parts[1]) elif parts[0] == "WRITE": filename = parts[1] data = " ".join(parts[2:]) fs.write(filename, data) elif parts[0] == "READ": result = fs.read(parts[1]) output.append(result) elif parts[0] == "DELETE": fs.delete(parts[1]) elif parts[0] == "MODTIME": result = fs.modtime(parts[1]) output.append(result) return output
question:def count_unique_substrings_of_length_n(s: str, n: int) -> int: Returns the number of unique substrings of length exactly n in the input string s. >>> count_unique_substrings_of_length_n("abc", 2) 2 >>> count_unique_substrings_of_length_n("aaaaa", 1) 1 >>> count_unique_substrings_of_length_n("short", 10) 0 >>> count_unique_substrings_of_length_n("another", 8) 0 >>> count_unique_substrings_of_length_n("", 1) 0 >>> count_unique_substrings_of_length_n("a", 1) 1 >>> count_unique_substrings_of_length_n("b", 2) 0 >>> count_unique_substrings_of_length_n("abcabc", 3) 3 >>> count_unique_substrings_of_length_n("abcdef", 3) 4 >>> count_unique_substrings_of_length_n("yyyyy", 2) 1
answer:def count_unique_substrings_of_length_n(s, n): Returns the number of unique substrings of length exactly n in the input string s. if n > len(s): return 0 substrings = set() for i in range(len(s) - n + 1): substrings.add(s[i:i+n]) return len(substrings)