I have setup the docker container to run the model evaluation.
Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,":1, "an":1, "example":1} The program should be run by calling /path/to/python_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,":1, "an":1, "example":1} The program should be run by calling /path/to/python_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import json import multiprocessing from pathlib import Path CORES = 4 def count_words(lines): """Counts the words in a list of lines. Args: lines: A list of lines from a text file. Returns: A dictionary of word counts. """ word_counts = {} for line in lines: words = line.split() for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): """Counts the words in a text file and prints the results as a JSON object.""" # Read the text file with open("/tmp/file.txt", "r") as f: lines = f.readlines() # Split the lines into chunks chunks = [lines[i::CORES] for i in range(CORES)] # Create a pool of workers pool = multiprocessing.Pool(CORES) # Count the words in each chunk results = pool.map(count_words, chunks) # Merge the results word_counts = {} for result in results: for word, count in result.items(): word_counts[word] = word_counts.get(word, 0) + count # Print the results as a JSON object print(json.dumps(word_counts)) if __name__ == "__main__": main() |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import json import multiprocessing from pathlib import Path CORES = 4 def count_words(lines): """Counts the words in a list of lines. Args: lines: A list of lines from a text file. Returns: A dictionary of word counts. """ word_counts = {} for line in lines: words = line.split() for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): """Counts the words in a text file and prints the results as a JSON object.""" # Read the text file with open("/tmp/file.txt", "r") as f: lines = f.readlines() # Split the lines into chunks chunks = [lines[i::CORES] for i in range(CORES)] # Create a pool of workers pool = multiprocessing.Pool(CORES) # Count the words in each chunk results = pool.map(count_words, chunks) # Merge the results word_counts = {} for result in results: for word, count in result.items(): word_counts[word] = word_counts.get(word, 0) + count # Print the results as a JSON object print(json.dumps(word_counts)) if __name__ == "__main__": main() |
Below is some student's code. I asked them to parallelize some code. Do not judge if it is correct, I have test cases for that. Just tell me if they tried to parallelize it. import json import multiprocessing from pathlib import Path CORES = 4 def count_words(lines): """Counts the words in a list of lines. Args: lines: A list of lines from a text file. Returns: A dictionary of word counts. """ word_counts = {} for line in lines: words = line.split() for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): """Counts the words in a text file and prints the results as a JSON object.""" # Read the text file with open("/tmp/file.txt", "r") as f: lines = f.readlines() # Split the lines into chunks chunks = [lines[i::CORES] for i in range(CORES)] # Create a pool of workers pool = multiprocessing.Pool(CORES) # Count the words in each chunk results = pool.map(count_words, chunks) # Merge the results word_counts = {} for result in results: for word, count in result.items(): word_counts[word] = word_counts.get(word, 0) + count # Print the results as a JSON object print(json.dumps(word_counts)) if __name__ == "__main__": main() Answer either 'The student passes' or 'The student fails'.
The student passes.
Testing if the previous output contains the string student passes
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import json import multiprocessing from pathlib import Path CORES = 4 def count_words(lines): """Counts the words in a list of lines. Args: lines: A list of lines from a text file. Returns: A dictionary of word counts. """ word_counts = {} for line in lines: words = line.split() for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): """Counts the words in a text file and prints the results as a JSON object.""" # Read the text file with open("/tmp/file.txt", "r") as f: lines = f.readlines() # Split the lines into chunks chunks = [lines[i::CORES] for i in range(CORES)] # Create a pool of workers pool = multiprocessing.Pool(CORES) # Count the words in each chunk results = pool.map(count_words, chunks) # Merge the results word_counts = {} for result in results: for word, count in result.items(): word_counts[word] = word_counts.get(word, 0) + count # Print the results as a JSON object print(json.dumps(word_counts)) if __name__ == "__main__": main() |
{"it": 10, "was": 10, "the": 11, "best": 1, "of": 10, "times,": 2, "worst": 1, "age": 2, "wisdom,": 1, "foolishness,": 1, "epoch": 2, "belief,": 1, "incredulity,": 1, "season": 2, "Light,": 1, "Darkness,": 1, "spring": 1, "hope,": 1, "winter": 1, "despair,": 1, "we": 4, "had": 2, "everything": 1, "before": 2, "us,": 2, "nothing": 1, "were": 2, "all": 2, "going": 2, "direct": 2, "to": 1, "Heaven,": 1, "other": 1, "way": 1}
Resulting in output: {'it': 10, 'was': 10, 'the': 11, 'best': 1, 'of': 10, 'times,': 2, 'worst': 1, 'age': 2, 'wisdom,': 1, 'foolishness,': 1, 'epoch': 2, 'belief,': 1, 'incredulity,': 1, 'season': 2, 'Light,': 1, 'Darkness,': 1, 'spring': 1, 'hope,': 1, 'winter': 1, 'despair,': 1, 'we': 4, 'had': 2, 'everything': 1, 'before': 2, 'us,': 2, 'nothing': 1, 'were': 2, 'all': 2, 'going': 2, 'direct': 2, 'to': 1, 'Heaven,': 1, 'other': 1, 'way': 1}
Testing if the previous output equals the string {'the': 11, 'it': 10, 'was': 10, 'of': 10, 'we': 4, 'times,': 2, 'age': 2, 'epoch': 2, 'season': 2, 'had': 2, 'before': 2, 'us,': 2, 'were': 2, 'all': 2, 'going': 2, 'direct': 2, 'best': 1, 'worst': 1, 'wisdom,': 1, 'foolishness,': 1, 'belief,': 1, 'incredulity,': 1, 'Light,': 1, 'Darkness,': 1, 'spring': 1, 'hope,': 1, 'winter': 1, 'despair,': 1, 'everything': 1, 'nothing': 1, 'to': 1, 'Heaven,': 1, 'other': 1, 'way': 1}
: True
I have setup the docker container to run the model evaluation.
Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,":1, "an":1, "example":1} The program should be run by calling /path/to/python_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,":1, "an":1, "example":1} The program should be run by calling /path/to/python_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from concurrent.futures import ProcessPoolExecutor, as_completed import json import os def count_words(filename, start, end): with open(filename, 'r') as f: words = f.read()[start:end].split() word_counts = {} for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): filename = '/tmp/file.txt' filesize = os.path.getsize(filename) with ProcessPoolExecutor(max_workers=4) as executor: futures = [] for i in range(4): start = i * filesize // 4 end = (i + 1) * filesize // 4 futures.append(executor.submit(count_words, filename, start, end)) for future in as_completed(futures): print(json.dumps(future.result())) if __name__ == '__main__': main() |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from concurrent.futures import ProcessPoolExecutor, as_completed import json import os def count_words(filename, start, end): with open(filename, 'r') as f: words = f.read()[start:end].split() word_counts = {} for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): filename = '/tmp/file.txt' filesize = os.path.getsize(filename) with ProcessPoolExecutor(max_workers=4) as executor: futures = [] for i in range(4): start = i * filesize // 4 end = (i + 1) * filesize // 4 futures.append(executor.submit(count_words, filename, start, end)) for future in as_completed(futures): print(json.dumps(future.result())) if __name__ == '__main__': main() |
Below is some student's code. I asked them to parallelize some code. Do not judge if it is correct, I have test cases for that. Just tell me if they tried to parallelize it. from concurrent.futures import ProcessPoolExecutor, as_completed import json import os def count_words(filename, start, end): with open(filename, 'r') as f: words = f.read()[start:end].split() word_counts = {} for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): filename = '/tmp/file.txt' filesize = os.path.getsize(filename) with ProcessPoolExecutor(max_workers=4) as executor: futures = [] for i in range(4): start = i * filesize // 4 end = (i + 1) * filesize // 4 futures.append(executor.submit(count_words, filename, start, end)) for future in as_completed(futures): print(json.dumps(future.result())) if __name__ == '__main__': main() Answer either 'The student passes' or 'The student fails'.
The student passes
Testing if the previous output contains the string student passes
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from concurrent.futures import ProcessPoolExecutor, as_completed import json import os def count_words(filename, start, end): with open(filename, 'r') as f: words = f.read()[start:end].split() word_counts = {} for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): filename = '/tmp/file.txt' filesize = os.path.getsize(filename) with ProcessPoolExecutor(max_workers=4) as executor: futures = [] for i in range(4): start = i * filesize // 4 end = (i + 1) * filesize // 4 futures.append(executor.submit(count_words, filename, start, end)) for future in as_completed(futures): print(json.dumps(future.result())) if __name__ == '__main__': main() |
{"it": 4, "was": 4, "the": 4, "best": 1, "of": 4, "times,": 2, "worst": 1, "age": 2, "wisdom,": 1, "foolish": 1}
{"ness,": 1, "it": 4, "was": 4, "the": 3, "epoch": 2, "of": 3, "belief,": 1, "incredulity,": 1, "season": 1, "Light,": 1, "th": 1}
{"e": 1, "season": 1, "of": 3, "Darkness,": 1, "it": 2, "was": 2, "the": 2, "spring": 1, "hope,": 1, "winter": 1, "despair,": 1, "we": 1, "had": 1, "everything": 1, "before": 1}
{"us,": 2, "we": 3, "had": 1, "nothing": 1, "before": 1, "were": 2, "all": 2, "going": 2, "direct": 2, "to": 1, "Heaven,": 1, "the": 1, "other": 1, "way": 1}
Resulting in output: {'it': 10, 'was': 10, 'the': 10, 'best': 1, 'of': 10, 'times,': 2, 'worst': 1, 'age': 2, 'wisdom,': 1, 'foolish': 1, 'ness,': 1, 'epoch': 2, 'belief,': 1, 'incredulity,': 1, 'season': 2, 'Light,': 1, 'th': 1, 'e': 1, 'Darkness,': 1, 'spring': 1, 'hope,': 1, 'winter': 1, 'despair,': 1, 'we': 4, 'had': 2, 'everything': 1, 'before': 2, 'us,': 2, 'nothing': 1, 'were': 2, 'all': 2, 'going': 2, 'direct': 2, 'to': 1, 'Heaven,': 1, 'other': 1, 'way': 1}
Testing if the previous output equals the string {'the': 11, 'it': 10, 'was': 10, 'of': 10, 'we': 4, 'times,': 2, 'age': 2, 'epoch': 2, 'season': 2, 'had': 2, 'before': 2, 'us,': 2, 'were': 2, 'all': 2, 'going': 2, 'direct': 2, 'best': 1, 'worst': 1, 'wisdom,': 1, 'foolishness,': 1, 'belief,': 1, 'incredulity,': 1, 'Light,': 1, 'Darkness,': 1, 'spring': 1, 'hope,': 1, 'winter': 1, 'despair,': 1, 'everything': 1, 'nothing': 1, 'to': 1, 'Heaven,': 1, 'other': 1, 'way': 1}
: False
I have setup the docker container to run the model evaluation.
Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,":1, "an":1, "example":1} The program should be run by calling /path/to/python_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,":1, "an":1, "example":1} The program should be run by calling /path/to/python_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | from multiprocessing import Pool import json import os def count_words(filename): with open(filename) as f: words = f.read().split() return {word: words.count(word) for word in words} if __name__ == "__main__": with open('/tmp/file.txt', 'r') as f: text = f.read() # Split the text into 4 equal parts parts = [text[i:i+len(text)//4] for i in range(0, len(text), len(text)//4)] # Create a pool of 4 workers pool = Pool(4) # Count the words in each part of the text in parallel counts = pool.map(count_words, parts) # Print the results as JSON for count in counts: print(json.dumps(count)) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | from multiprocessing import Pool import json import os def count_words(filename): with open(filename) as f: words = f.read().split() return {word: words.count(word) for word in words} if __name__ == "__main__": with open('/tmp/file.txt', 'r') as f: text = f.read() # Split the text into 4 equal parts parts = [text[i:i+len(text)//4] for i in range(0, len(text), len(text)//4)] # Create a pool of 4 workers pool = Pool(4) # Count the words in each part of the text in parallel counts = pool.map(count_words, parts) # Print the results as JSON for count in counts: print(json.dumps(count)) |
Below is some student's code. I asked them to parallelize some code. Do not judge if it is correct, I have test cases for that. Just tell me if they tried to parallelize it. from multiprocessing import Pool import json import os def count_words(filename): with open(filename) as f: words = f.read().split() return {word: words.count(word) for word in words} if __name__ == "__main__": with open('/tmp/file.txt', 'r') as f: text = f.read() # Split the text into 4 equal parts parts = [text[i:i+len(text)//4] for i in range(0, len(text), len(text)//4)] # Create a pool of 4 workers pool = Pool(4) # Count the words in each part of the text in parallel counts = pool.map(count_words, parts) # Print the results as JSON for count in counts: print(json.dumps(count)) Answer either 'The student passes' or 'The student fails'.
The student passes
Testing if the previous output contains the string student passes
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | from multiprocessing import Pool import json import os def count_words(filename): with open(filename) as f: words = f.read().split() return {word: words.count(word) for word in words} if __name__ == "__main__": with open('/tmp/file.txt', 'r') as f: text = f.read() # Split the text into 4 equal parts parts = [text[i:i+len(text)//4] for i in range(0, len(text), len(text)//4)] # Create a pool of 4 workers pool = Pool(4) # Count the words in each part of the text in parallel counts = pool.map(count_words, parts) # Print the results as JSON for count in counts: print(json.dumps(count)) |
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/lib/python3.11/multiprocessing/pool.py", line 125, in worker
result = (True, func(*args, **kwds))
^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/multiprocessing/pool.py", line 48, in mapstar
return list(map(*args))
^^^^^^^^^^^^^^^^
File "/usr/src/app/main.py", line 7, in count_words
with open(filename) as f:
^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'it was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolish'
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/app/main.py", line 22, in <module>
counts = pool.map(count_words, parts)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/multiprocessing/pool.py", line 367, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/multiprocessing/pool.py", line 774, in get
raise self._value
FileNotFoundError: [Errno 2] No such file or directory: 'it was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolish'
Resulting in output: {}
Testing if the previous output equals the string {'the': 11, 'it': 10, 'was': 10, 'of': 10, 'we': 4, 'times,': 2, 'age': 2, 'epoch': 2, 'season': 2, 'had': 2, 'before': 2, 'us,': 2, 'were': 2, 'all': 2, 'going': 2, 'direct': 2, 'best': 1, 'worst': 1, 'wisdom,': 1, 'foolishness,': 1, 'belief,': 1, 'incredulity,': 1, 'Light,': 1, 'Darkness,': 1, 'spring': 1, 'hope,': 1, 'winter': 1, 'despair,': 1, 'everything': 1, 'nothing': 1, 'to': 1, 'Heaven,': 1, 'other': 1, 'way': 1}
: False
I have setup the docker container to run the model evaluation.
Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,":1, "an":1, "example":1} The program should be run by calling /path/to/python_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,":1, "an":1, "example":1} The program should be run by calling /path/to/python_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import json import multiprocessing import os def count_words(file_path, start, end): with open(file_path, "r") as f: words = f.read()[start:end].split() word_counts = {} for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): file_path = "/tmp/file.txt" file_size = os.path.getsize(file_path) num_cores = 4 chunk_size = file_size // num_cores # Create a pool of workers pool = multiprocessing.Pool(num_cores) # Divide the file into chunks and assign each chunk to a worker chunks = [(file_path, i * chunk_size, (i + 1) * chunk_size) for i in range(num_cores)] results = pool.starmap(count_words, chunks) # Merge the results from each worker word_counts = {} for result in results: for word, count in result.items(): word_counts[word] = word_counts.get(word, 0) + count # Print the word counts in JSON format for word, count in word_counts.items(): print(json.dumps({word: count})) if __name__ == "__main__": main() |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import json import multiprocessing import os def count_words(file_path, start, end): with open(file_path, "r") as f: words = f.read()[start:end].split() word_counts = {} for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): file_path = "/tmp/file.txt" file_size = os.path.getsize(file_path) num_cores = 4 chunk_size = file_size // num_cores # Create a pool of workers pool = multiprocessing.Pool(num_cores) # Divide the file into chunks and assign each chunk to a worker chunks = [(file_path, i * chunk_size, (i + 1) * chunk_size) for i in range(num_cores)] results = pool.starmap(count_words, chunks) # Merge the results from each worker word_counts = {} for result in results: for word, count in result.items(): word_counts[word] = word_counts.get(word, 0) + count # Print the word counts in JSON format for word, count in word_counts.items(): print(json.dumps({word: count})) if __name__ == "__main__": main() |
Below is some student's code. I asked them to parallelize some code. Do not judge if it is correct, I have test cases for that. Just tell me if they tried to parallelize it. import json import multiprocessing import os def count_words(file_path, start, end): with open(file_path, "r") as f: words = f.read()[start:end].split() word_counts = {} for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): file_path = "/tmp/file.txt" file_size = os.path.getsize(file_path) num_cores = 4 chunk_size = file_size // num_cores # Create a pool of workers pool = multiprocessing.Pool(num_cores) # Divide the file into chunks and assign each chunk to a worker chunks = [(file_path, i * chunk_size, (i + 1) * chunk_size) for i in range(num_cores)] results = pool.starmap(count_words, chunks) # Merge the results from each worker word_counts = {} for result in results: for word, count in result.items(): word_counts[word] = word_counts.get(word, 0) + count # Print the word counts in JSON format for word, count in word_counts.items(): print(json.dumps({word: count})) if __name__ == "__main__": main() Answer either 'The student passes' or 'The student fails'.
The student passes.
Testing if the previous output contains the string student passes
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import json import multiprocessing import os def count_words(file_path, start, end): with open(file_path, "r") as f: words = f.read()[start:end].split() word_counts = {} for word in words: word_counts[word] = word_counts.get(word, 0) + 1 return word_counts def main(): file_path = "/tmp/file.txt" file_size = os.path.getsize(file_path) num_cores = 4 chunk_size = file_size // num_cores # Create a pool of workers pool = multiprocessing.Pool(num_cores) # Divide the file into chunks and assign each chunk to a worker chunks = [(file_path, i * chunk_size, (i + 1) * chunk_size) for i in range(num_cores)] results = pool.starmap(count_words, chunks) # Merge the results from each worker word_counts = {} for result in results: for word, count in result.items(): word_counts[word] = word_counts.get(word, 0) + count # Print the word counts in JSON format for word, count in word_counts.items(): print(json.dumps({word: count})) if __name__ == "__main__": main() |
{"it": 10}
{"was": 10}
{"the": 10}
{"best": 1}
{"of": 10}
{"times,": 2}
{"worst": 1}
{"age": 2}
{"wisdom,": 1}
{"foolish": 1}
{"ness,": 1}
{"epoch": 2}
{"belief,": 1}
{"incredulity,": 1}
{"season": 2}
{"Light,": 1}
{"th": 1}
{"e": 1}
{"Darkness,": 1}
{"spring": 1}
{"hope,": 1}
{"winter": 1}
{"despair,": 1}
{"we": 4}
{"had": 2}
{"everything": 1}
{"before": 2}
{"us,": 2}
{"nothing": 1}
{"were": 2}
{"all": 2}
{"going": 2}
{"direct": 2}
{"to": 1}
{"Heaven,": 1}
{"other": 1}
{"way": 1}
Resulting in output: {'it': 10, 'was': 10, 'the': 10, 'best': 1, 'of': 10, 'times,': 2, 'worst': 1, 'age': 2, 'wisdom,': 1, 'foolish': 1, 'ness,': 1, 'epoch': 2, 'belief,': 1, 'incredulity,': 1, 'season': 2, 'Light,': 1, 'th': 1, 'e': 1, 'Darkness,': 1, 'spring': 1, 'hope,': 1, 'winter': 1, 'despair,': 1, 'we': 4, 'had': 2, 'everything': 1, 'before': 2, 'us,': 2, 'nothing': 1, 'were': 2, 'all': 2, 'going': 2, 'direct': 2, 'to': 1, 'Heaven,': 1, 'other': 1, 'way': 1}
Testing if the previous output equals the string {'the': 11, 'it': 10, 'was': 10, 'of': 10, 'we': 4, 'times,': 2, 'age': 2, 'epoch': 2, 'season': 2, 'had': 2, 'before': 2, 'us,': 2, 'were': 2, 'all': 2, 'going': 2, 'direct': 2, 'best': 1, 'worst': 1, 'wisdom,': 1, 'foolishness,': 1, 'belief,': 1, 'incredulity,': 1, 'Light,': 1, 'Darkness,': 1, 'spring': 1, 'hope,': 1, 'winter': 1, 'despair,': 1, 'everything': 1, 'nothing': 1, 'to': 1, 'Heaven,': 1, 'other': 1, 'way': 1}
: False
I have setup the docker container to run the model evaluation.
Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,":1, "an":1, "example":1} The program should be run by calling /path/to/python_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,":1, "an":1, "example":1} The program should be run by calling /path/to/python_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import json import multiprocessing import os def word_count(file_name, start, end): with open(file_name, "r") as f: words = f.read()[start:end].split() counts = {} for word in words: word = word.strip(".,!?").lower() if word: counts[word] = counts.get(word, 0) + 1 return counts if __name__ == "__main__": file_name = "/tmp/file.txt" file_size = os.path.getsize(file_name) num_cores = 4 chunk_size = file_size // num_cores with multiprocessing.Pool(num_cores) as pool: results = pool.starmap(word_count, [(file_name, i * chunk_size, (i + 1) * chunk_size) for i in range(num_cores)]) for result in results: print(json.dumps(result)) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import json import multiprocessing import os def word_count(file_name, start, end): with open(file_name, "r") as f: words = f.read()[start:end].split() counts = {} for word in words: word = word.strip(".,!?").lower() if word: counts[word] = counts.get(word, 0) + 1 return counts if __name__ == "__main__": file_name = "/tmp/file.txt" file_size = os.path.getsize(file_name) num_cores = 4 chunk_size = file_size // num_cores with multiprocessing.Pool(num_cores) as pool: results = pool.starmap(word_count, [(file_name, i * chunk_size, (i + 1) * chunk_size) for i in range(num_cores)]) for result in results: print(json.dumps(result)) |
Below is some student's code. I asked them to parallelize some code. Do not judge if it is correct, I have test cases for that. Just tell me if they tried to parallelize it. import json import multiprocessing import os def word_count(file_name, start, end): with open(file_name, "r") as f: words = f.read()[start:end].split() counts = {} for word in words: word = word.strip(".,!?").lower() if word: counts[word] = counts.get(word, 0) + 1 return counts if __name__ == "__main__": file_name = "/tmp/file.txt" file_size = os.path.getsize(file_name) num_cores = 4 chunk_size = file_size // num_cores with multiprocessing.Pool(num_cores) as pool: results = pool.starmap(word_count, [(file_name, i * chunk_size, (i + 1) * chunk_size) for i in range(num_cores)]) for result in results: print(json.dumps(result)) Answer either 'The student passes' or 'The student fails'.
The student passes.
Testing if the previous output contains the string student passes
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import json import multiprocessing import os def word_count(file_name, start, end): with open(file_name, "r") as f: words = f.read()[start:end].split() counts = {} for word in words: word = word.strip(".,!?").lower() if word: counts[word] = counts.get(word, 0) + 1 return counts if __name__ == "__main__": file_name = "/tmp/file.txt" file_size = os.path.getsize(file_name) num_cores = 4 chunk_size = file_size // num_cores with multiprocessing.Pool(num_cores) as pool: results = pool.starmap(word_count, [(file_name, i * chunk_size, (i + 1) * chunk_size) for i in range(num_cores)]) for result in results: print(json.dumps(result)) |
{"it": 4, "was": 4, "the": 4, "best": 1, "of": 4, "times": 2, "worst": 1, "age": 2, "wisdom": 1, "foolish": 1}
{"ness": 1, "it": 4, "was": 4, "the": 3, "epoch": 2, "of": 3, "belief": 1, "incredulity": 1, "season": 1, "light": 1, "th": 1}
{"e": 1, "season": 1, "of": 3, "darkness": 1, "it": 2, "was": 2, "the": 2, "spring": 1, "hope": 1, "winter": 1, "despair": 1, "we": 1, "had": 1, "everything": 1, "before": 1}
{"us": 2, "we": 3, "had": 1, "nothing": 1, "before": 1, "were": 2, "all": 2, "going": 2, "direct": 2, "to": 1, "heaven": 1, "the": 1, "other": 1, "way": 1}
Resulting in output: {'it': 10, 'was': 10, 'the': 10, 'best': 1, 'of': 10, 'times': 2, 'worst': 1, 'age': 2, 'wisdom': 1, 'foolish': 1, 'ness': 1, 'epoch': 2, 'belief': 1, 'incredulity': 1, 'season': 2, 'light': 1, 'th': 1, 'e': 1, 'darkness': 1, 'spring': 1, 'hope': 1, 'winter': 1, 'despair': 1, 'we': 4, 'had': 2, 'everything': 1, 'before': 2, 'us': 2, 'nothing': 1, 'were': 2, 'all': 2, 'going': 2, 'direct': 2, 'to': 1, 'heaven': 1, 'other': 1, 'way': 1}
Testing if the previous output equals the string {'the': 11, 'it': 10, 'was': 10, 'of': 10, 'we': 4, 'times,': 2, 'age': 2, 'epoch': 2, 'season': 2, 'had': 2, 'before': 2, 'us,': 2, 'were': 2, 'all': 2, 'going': 2, 'direct': 2, 'best': 1, 'worst': 1, 'wisdom,': 1, 'foolishness,': 1, 'belief,': 1, 'incredulity,': 1, 'Light,': 1, 'Darkness,': 1, 'spring': 1, 'hope,': 1, 'winter': 1, 'despair,': 1, 'everything': 1, 'nothing': 1, 'to': 1, 'Heaven,': 1, 'other': 1, 'way': 1}
: False