تم الحل ✓
categoryبرمجة وتطوير البرمجيات
schoolبكالوريوس
event_available2026-07-15
السؤال
Transcribed Image Text:
In this task, you will design an input file processor using object-oriented programming techniques.
⚫. First, you will design a class called text_parser, which will accept a multiline string and separate each of this text into tokens based on the delimiter
character. This processing is aking to doing
line.split( delimiter )
Moreover, you will need to clean up each token that will result from this processing from spaces using str.strip() call. For example:
In [5]:
1
=
line "This
2
delimiter =
is, a, test, string
3 tokens line.split( delimiter )
4 print("Tokens before cleaning: {}".format( tokens ) )
5 new_tokens = list map str.strip, tokens ) )
6 print("Tokens after cleaning: {}".format(new_tokens ) )
Tokens before cleaning: ['This ',
is, a', 'test', 'string
Tokens after cleaning: ['This', 'is', 'a', 'test', 'string']
The API that your text_parser class will support are the following:
ני
text_parser( self, multiline_string, delimiter) should initialize a new text_parser instance based on the multiline string and the
delimiter for parsing tokens.
■ text_parser.parse_rows( self) should return a list of lists, where each list represents the cleaned up tokens on each row/line. For example:
S =
www.
a b
C, d
J
e, f, g,h
j,k,1,m"""
tp = text_parser( S,
rows =
tp.parse_rows()
print(rows)
should print out the following:
[['a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h'],
['j', 'k', '1', 'm']]
)
•
cols = tp.parse_columns()
print( cols )
should print out the following:
[['a', 'e', ''],
['b', 'f', 'k',
['c', 'g', '1'],
['d', 'h', 'm']]
text_parser.convert_to_type( parsed_data, type ) should be defined as a class method, and it should convert all elements of the parsed data
into the specified type. For example, if we call text_parser.convert_to_type([['1', '2', '3'], ['11', '22', '33']], int ), the return
value should be the same values converted to int.
• Next, you need to support the four arithmetic operations, add, sub, mul, and div between any of the rows and columns of data your class parses,
depending on an axis parameter, which will specify whether addition should happen horizontally (axis=0, default) or vertically ( axis=1).
Rows/columns will be indexed by integers starting at 0. For example,
data =
[[1, 2, 3,],
[3, 4, 5],
[6, 7, 8]]
text_parser.add( data, 0, 1, axis=0)
should return [4, 6, 8]. However,
text_parser.add( data, 0, 1, axis=1)
should return [3, 7, 13] since we need to add values vertically.
Similarly, the other three arithmetic functions should work exactly the same way.
>>> ENTER YOUR PROGRAM IN THE CODE BOX BELOW ACCORDING TO THE SPECIFICATIONS FOR THE GIVEN TASK ABOVE
In [6]:
1 # Enter your code here
2
3
4
5
check_circle الجواب — حل مفصل خطوة بخطوة
hourglass_top
🔒
الحل الكامل متاح للمشتركين
اشترك في أرشيف الأسئلة لعرض هذا الحل وآلاف الحلول المفصلة خطوة بخطوة من معلمين معتمدين.