0% found this document useful (0 votes)
16 views10 pages

Eee 111 Ay2324 Wa06

Uploaded by

Aika Na
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Eee 111 Ay2324 Wa06

Uploaded by

Aika Na
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

EEE 111

Introduction to Programing and Computation

Assessments Week 06
Academic Period: 1st Semester AY 2023-2024
Workload: 3 hours
Synopsis: Other Python Topics
Submission Platform: UVLe

Assessment 06A
This assessment will test your understanding of the weekly material through an online quiz. Note
that you can only submit the quiz once. However, you can leave the quiz unsubmitted until it
submits itself on the deadline.

This is worth 5% of your grade for this week.

Steps
1. Access the quiz here.
2. Please read the instructions in each question or group of questions before proceeding to
answer the items.
3. Once you are satisfied with your answers you can either submit it right away or leave the
quiz unsubmitted until the deadline comes.
Assessment 06B
This second assessment will help you become comfortable with writing exceptions.

This is worth 55% of your grade for this week.

Problem Statement
One of the important, but taken for granted, aspects of
computer programming are parentheses. These parentheses
are used to group mathematical expressions, but some
programming languages use them to denote tuples or
generally group programming expressions. For compilers and
interpreters to correctly read the groupings, the parentheses
should be matched or balanced. That is, each opening, or left
parenthesis should have a matching closing or right parenthesis. A string containing parentheses
are matched if 1) the total number of left and right parentheses are the same, and 2) each left
parenthesis has a matching right parenthesis to the right of it, and any parentheses between these
two are also matched. For example, the parentheses (()) match while the parentheses (()( do
not match because the fourth left parenthesis does not have a matching right parenthesis that
should have appeared after it.

Now, it is our task to create a program that will check whether a string contains matching
parentheses or not. The algorithm for checking matches is relatively simple as we only need to look
for the parentheses and ignore other characters. However, we are asked to additionally parse a set
of string generation commands that will generate a string whose matching parentheses we need to
check too.

Input
The input to the program starts with a number 𝑇 by itself on a line denoting the number of test
cases. Then, 𝑇 blocks of lines follow. Each block starts with a line 𝐶 𝑆 with 𝐶 denoting whether the
string is provided via string generation commands (g) or as is (r). If the string is provided as is, then
𝑆 contains the string itself. On the other hand, if the string is provided via string generation
commands, then 𝑆 contains the number of commands that will follow. Each of the next 𝑆 lines
contain a string generation command of the form 𝑃 𝑁, which can be one of the following:

● ( N - generate N contiguous left parentheses (


● ) N - generate N contiguous right parentheses )

EEE 111 Assessments Week 06 | Page 2


● s N - append a string N

Note that a string generation command appends to the end of some accumulator string that is
initially blank. The final resulting string is then checked for balanced parentheses.

Output
The output to the program consists of 𝑇 lines corresponding to the number of test cases. Each line
is of the format 𝐶𝑎𝑠𝑒 #𝑡: 𝑏 with 𝑡 denoting the serial of the test cases starting from 1 and 𝑏 a YES
or NO denoting that the respective string has balanced parentheses or not.

Please see the sample test cases below for more details.

Constraints
Input Constraints
𝑇 ≤ 10
𝐶 ∈ {g, r}
𝑆 ∈ 𝑍; 𝑆 ∈ [1, 50]; if 𝐶 = 𝑔
|𝑆| ∈ [1, 100]; if 𝐶 = 𝑟
𝑃 ∈ {(, ), s}
𝑁 ∈ 𝑍; 𝑁 ≤ 50; if 𝑃 ∈ {(, )}
|𝑁| ∈ [1, 100]; if 𝑃 = 𝑠

Characters in strings 𝑆 and 𝑁 and are within the set [A-Za-z0-9_- ]. Spaces in 𝑆 and 𝑁 do not
appear at the start or the end of them.

You are not required to handle any other errors except those stated in the functional constraints.

Functional Constraints
You are required to have the following function signatures, their arguments in order, and their
return values:
● create_parens - generate a set number of left or right parentheses
○ Arguments
■ count - contains an integer number of parentheses to generate
■ is_right - a boolean denoting whether to generate left or right
parentheses; default is False (i.e. generate left parentheses)
○ Return value - str representing count amount of contiguous left or right
parentheses

EEE 111 Assessments Week 06 | Page 3


○ Raises
■ TypeError if any of the arguments are not of the proper data type
○ Additional constraints - you are not allowed to use the following conditional
constructs: if, elif, else; the ternary operator is allowed
● is_bal_nested_parens - check whether a string contains balanced parentheses
○ Arguments
■ parens_str - string to check for balanced parentheses
○ Return value - bool representing whether the parentheses in the input string is
balanced; True if it is
○ Raises
■ TypeError if any of the arguments are not of the proper data type
○ Additional constraints - you are not allowed to use any sequence constructs (list,
set, tuple, dict).
● main - main entry point of the program
○ Arguments
■ None
○ Return value - None; should be able to process the input and print the output
according to specifications

Output should write to the standard output (i.e. use print()).

No import statements are allowed.

Failure to follow these functional constraints will result in a score of zero in the assessment.

Examples
Sample Test 01
For test case 03, the generated string is ((()))hello!(()).

Input Output
3 Case #1: YES
r ((()())()) Case #2: NO
r ((()())() Case #3: YES
g 5
( 3
) 3
s hello!
( 2
) 2

EEE 111 Assessments Week 06 | Page 4


Steps
1. Download the template source code and edit it according to the problem description. The
template may have some comments to note where to edit.
2. Download the public test file. The test file may have some comments to note where to edit
such that it can test your code accordingly.
3. Run the test file to check for errors. If the test file is named test_public.py, you can
execute the following command from any terminal to run it:
python test_public.py
4. Submit a copy of the source code to the Week 06B submission bin. Make sure that you
attach one (1) file in the bin containing the Python source code (in .py extension). Note that
you can submit as many times as you want before the deadline.

Grading Rubric
100% Hidden test case

EEE 111 Assessments Week 06 | Page 5


Assessment 06C
This assessment will help you become comfortable with parsing files.

This is worth 45% of your grade for this week.

Problem Statement
Computer programs can be configured so that some of their
parameters can be set for customization purposes. For
example, a taskbar or window color can be set through
configuration. These parameters are saved as configuration
files, which programs read at least every startup. One of the
simplest configuration file formats is the TOML (Tom's Obvious
Markup Language) file. In this context, we will be using an
overly simplified version of this file format

A TOML file has the .toml extension. Parameters are saved as a key-value pair in tables. Keys can
be thought of as variable names whose names can only contain ASCII letters, digits, underscores,
and dashes. On the other hand, values can contain integers, floats, booleans, or strings. Integers and
floats are written like how Python integers or floats are written. On the other hand, boolean values
are written in all lowercase letters (i.e. true) while strings are always surrounded by double quotes
(i.e. "). Keys and values are separated by an equal sign =. Each key-value pair is written on a line by
itself and can be separated by an arbitrary number of newlines. Any arbitrary number of whitespaces
before the key, after the value, and around the equal sign are ignored. A example of a valid TOML
file is shown below:

sample.toml
an_int = 42
a_float = 3.14
a_bool =false
a_string = "this is a string!"

Python dict
{
'an_int': 42,
'a_float': 3.14,
'a_bool': False,
'a_string': 'this is a string!'
}

EEE 111 Assessments Week 06 | Page 6


As shown in the previous example, all key-value pairs in a TOML file are saved in tables, which can
be thought of as a Python dictionary. Keys should be unique in a table for them to be valid.
Key-value pairs enumerated at the topmost part of a TOML file do not have a table header, which in
this case, are saved in the root table. In this context, we will be naming this root table as toml. On
the other hand, tables can have tables in them, which can be referenced either via a table header or
through dot notation. A table header appears on a line by itself. The name of the table is a key
surrounded by square brackets. Any whitespace around the square brackets are ignored. Any
key-value pair that appears immediately after the table header is to be saved in that table. An
example of a TOML file with table headers is shown below:

table.toml
an_int = 42

[subtable ]
a_string = "this is a string!"

Python dict
{
'an_int': 42,
'subtable': {
'a_string': 'this is a string!'
}
}

Tables can also be referenced via dot notation. Each key that references a subtable is separated by
a dot (.) and can be used both in a key in a key-value pair or in a table header. Any whitespace
around the dots are ignored. Table headers reference a table relative to the global table, so a
header named [my.table] points to toml['my']['table']. On the other hand, keys
reference a table relative to the current table header, so a key sub.table written under the
header [my.table] points to toml['my']['table']['sub']['table']. An example
of a TOML file with subtables is shown below:

subtable.toml
an_int = 42

[subtable]
a_string = "this is a string!"
b.a_float = 2.718

[subtable.a]
a_bool = true

Python dict

EEE 111 Assessments Week 06 | Page 7


{
'an_int': 42,
'subtable': {
'a': {
'a_bool': True,
}
'a_string': 'this is a string!',
'b': {
'a_float': 2.718
}
}
}

With this information of a TOML file, you are now asked to implement a parser that can open a
TOML file and store the resulting parameters in a Python dictionary.

Input
None. Your solution is checked by parsing your code against various inputs to your functions.

Output
None.

Constraints
Input Constraints
Keys are within the set [A-Za-z0-9_-.].

Keys are unique within a table entry.

String values contain only ASCII characters.

String values will not have double quotes, backslashes, and ASCII unprintable characters (ASCII code
less than 0x20) in them.

You can assume that all of the inputs are well-formed and are always provided within these
constraints. You are not required to handle any other errors except those stated in the functional
constraints.

Functional Constraints

EEE 111 Assessments Week 06 | Page 8


You are required to have the following function signatures, their arguments in order, and their
return values:
● coerce_data - convert a TOML value into its primitive Python equivalent
○ Arguments
■ data - str representation of the data to convert
○ Return value - int, float, str, or bool representing their primitive Python
equivalent
● open_toml - open a TOML file
○ Arguments
■ file_path - str or Path representing the path where the TOML file to
open is
○ Return value - dict representing the contents of the TOML file
○ Raises
■ FileNotFoundError if file does not exist or not found in the specified
path
■ ValueError if file does not have the TOML extension
■ SyntaxError if file has a TOML syntax error somewhere

You are not allowed to write any other functions aside from the permitted ones outlined above.

You may use only the pathlib module as part of your allowed imports.

Failure to follow these functional constraints will result in a score of zero in the assessment.

Examples
Sample Test 01
Input File Output Dict
hello = 3 {'hello': 3, 'world': 7}
world = 7

Sample Test 02
Input File Output Dict
# Wee! {
hello.a = 3 'hello': {'a': 3, 'b': 7},
hello.b = 7 'world': 9, 'cde': 'this is a
world = 9 string!',
cde = "this is a string!" 'table': {'d': True, 'e':
False, 'f': {'g': True, 'h':
[table] False}}
d = true }

EEE 111 Assessments Week 06 | Page 9


e = false

[table.f]
g = true
h = false

Steps
1. Download the template source code and edit it according to the problem description. The
template may have some comments to note where to edit.
2. Download the public test file. The test file may have some comments to note where to edit
such that it can test your code accordingly.
3. Run the test file to check for errors. If the test file is named test_public.py, you can
execute the following command from any terminal to run it:
python test_public.py
4. Submit a copy of the source code to the Week 06C submission bin. Make sure that you
attach one (1) file in the bin containing the Python source code (in .py extension). Note that
you can submit as many times as you want before the deadline.

Grading Rubric
100% Hidden test case

EEE 111 Assessments Week 06 | Page 10

You might also like