Question
These 3 questions about python. Question1 : Define a function called get_lines_from_file(filename) that takes a filename as a parameter. The function reads the content of
These 3 questions about python.
Question1 :
Define a function called get_lines_from_file(filename) that takes a filename as a parameter. The function reads the content of the file specified in the parameter and returns a list of String elements. The strings in the file are separated by whitespaces. Remember to close the input text file.
For example:
Test | Result |
---|---|
print( get_lines_from_file("steve.txt")) | ['bbbbbbbb', 'bbbbbbbb', 'bccccccb', 'cccccccc', 'cwpccpwc', 'cccttccc', 'ccbccbcc', 'ccbbbbcc'] |
print( get_lines_from_file("steve_palette.txt")) | ['b:165,42,42', 'c:255,228,196', 'w:255,255,255', 'p:128,0,128', 't:210,180,140'] |
Question2:
Define a function called create_pattern_list(lines) that takes a list of strings as a parameter and returns a pattern_list which is a list of codes lists. Each element of the list is a list of single letters.
For example:
Test | Result |
---|---|
lines = ['gggggggg', 'gggggggg', 'gggggggg', 'gggggggg', 'gbbggbbg', 'gggddggg', 'gbbbbbbg', 'gggggggg'] print(create_pattern_list(lines)) | [['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g'], ['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g'], ['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g'], ['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g'], ['g', 'b', 'b', 'g', 'g', 'b', 'b', 'g'], ['g', 'g', 'g', 'd', 'd', 'g', 'g', 'g'], ['g', 'b', 'b', 'b', 'b', 'b', 'b', 'g'], ['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g']] |
lines = ['bbbbbbbb', 'bbbbbbbb', 'bccccccb', 'cccccccc', 'cwpccpwc', 'cccttccc', 'ccbccbcc', 'ccbbbbcc'] print(create_pattern_list(lines)) | [['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'], ['b', 'c', 'c', 'c', 'c', 'c', 'c', 'b'], ['c', 'c', 'c', 'c', 'c', 'c', 'c', 'c'], ['c', 'w', 'p', 'c', 'c', 'p', 'w', 'c'], ['c', 'c', 'c', 't', 't', 'c', 'c', 'c'], ['c', 'c', 'b', 'c', 'c', 'b', 'c', 'c'], ['c', 'c', 'b', 'b', 'b', 'b', 'c', 'c']] |
Question3:
Define a function called create_colours_dictionary(colours_list) that takes a list of strings as a parameter and returns a colours dictionary from the parameter. The key of each item in the dictionary is the colour code that we will use to draw a pixel art pattern and the value is a list containing the RGB colour components. An RGB colour value is specified with: red, green, blue. Each component (red, green, and blue) defines the intensity of the colour as an integer between 0 and 255. For example, (0, 0, 255) is rendered as blue, because the blue component is set to its highest value (255) and the others are set to 0. The format of the RGB is a list of three integers.
For example:
Test | Result |
---|---|
colours_list = ['b:165,42,42', 'c:255,228,196', 'w:255,255,255', 'p:128,0,128', 't:210,180,140'] colour_dictionary = create_colours_dictionary(colours_list) for key in sorted(colour_dictionary.keys()): print(key, colour_dictionary[key]) | b [165, 42, 42] c [255, 228, 196] p [128, 0, 128] t [210, 180, 140] w [255, 255, 255] |
colours_list = ['p:255,192,203', 'b:0,0,0', 'w:255,255,255', 'd:139,0,0'] colour_dictionary = create_colours_dictionary(colours_list) for key in sorted(colour_dictionary.keys()): print(key, colour_dictionary[key]) | b [0, 0, 0] d [139, 0, 0] p [255, 192, 203] w [255, 255, 255] |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started