def test_empty_string(self): input = "\"\"" print(input) result = MemoryString.countCharacters(input) print(result) self.assertEqual(0, result['memoryCharacters']) self.assertEqual(2, result['codeCharacters'])
def test_escaped_ascii_string(self): input = "\"\\x27\"" print(input) result = MemoryString.countCharacters(input) print(result) self.assertEqual(1, result['memoryCharacters']) self.assertEqual(6, result['codeCharacters'])
def test_abc_string(self): input = "\"abc\"" print(input) result = MemoryString.countCharacters(input) print(result) self.assertEqual(3, result['memoryCharacters']) self.assertEqual(5, result['codeCharacters'])
def test_escaped_quote_string(self): input = "\"aaa\\\"aaa\"" print(input) result = MemoryString.countCharacters(input) print(result) self.assertEqual(7, result['memoryCharacters']) self.assertEqual(10, result['codeCharacters'])
from memoryString import MemoryString # compare the length of the string in code to the length of the string in memory: # string is always quoted: "string" # string can contain: # escaped quotes \" # escaped slashes \\ # single ascii characters expressed as \x{0-f}{0-f} # "" is 2 characters of code and 0 characters in memory # "aaa\"aaa" is 10 characters of code and 7 characters of memory totalCodeLength = 0 totalMemoryLength = 0 f = open('input.txt','r') for line in f: trimmed = line.strip() result = MemoryString.countCharacters(trimmed) #text = "{l} {code} {memory}".format(l=trimmed, code=result['codeCharacters'], memory=result['memoryCharacters']) #print(text) totalCodeLength += result['codeCharacters'] totalMemoryLength += result['memoryCharacters'] print(totalCodeLength) print(totalMemoryLength) print(totalCodeLength - totalMemoryLength)