Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CMT120

Fundamentals of Programming

Programming Challenges

Assignment

To complete this coursework, you must complete a set of programming chal- lenges in Python and JavaScript.

Each challenge can be awarded a maximum of 10 marks.   Therefore, perfectly solving ve exercises will give you 50 marks (pass), and perfectly solving all the exercises will give you 100 marks. An exercise is solved per- fectly only if high-quality functional code is submitted in both Python and JavaScript. Providing high-quality functional code in only one programming language results in a lower mark (i.e., five marks out of ten). Therefore, you can still pass the coursework by only completing problems in one language, or by completing half the problems in both languages.

You might not be able to solve all the exercises.  This  is hne.  You are not all expected to be able to solve all of the challenges in both languages. However, you should be able to solve enough of the exercises in one or both languages to be able to pass the assessment and demonstrate you have met the learning outcomes being assessed.

The challenges are described in detail below, and you are also provided with a set of test cases that will check whether your code produces the required output or not.  In particular, you will be given two test cases per exercise. You should make sure that your submitted code passes the supplied tests to ensure it functions correctly.  However, please note that your code will be tested against a further two different test cases, which you have not been supplied with.  In total then each exercise will be tested against four test cases, including the two provided. You should therefore ensure that you try to cover all possible inputs and that your code still functions correctly. Your code will need to pass all 4 tests (2 seen, 2 unseen) in order to score full marks for the functionality.


Instructions for completing the challenges

。You will nd template code for the assignment on Learning Central.

This provides two folders,  python  and  js.   Inside each folder you will find a template .{js/py} file, in which you should complete your solutions. You will also nd a test_template .{js/py} file containing the test cases that will check your code’s functionality, along with a folder of test data required for some of the tests. You are also supplied with a Readme .md le containing detailed instructions on how to run the test cases to check your code.

。In the templates, the functions’ interfaces are given but the functions’

bodies are empty.  Solve the exercises by correctly lling in the func- tions’ bodies.

。It  is  forbidden  to  change  the  functions’  interfaces.   However,  new

functions  can  be  defined  to  support  the  solution  of the  exercises. These functions must have names that are different from those already present in the templates.

。You are NOT allowed to import any additional modules. Use of mod-

ule functions will result in zero marks for the corresponding exercises.

。In all the exercises, you can assume that the inputs are provided in the

appropriate format and type. Therefore, error-checking is not needed.

。The nal submission should NOT contain any input, print, or console .log

statements.

You will be given marks for solving each problem in both programming languages.  Further marks will be awarded for solution style and quality. The mark scheme is described in further detail later.


Exercise 1: Iris Species Classier

The diagram in Figure 1shows how to classify an iris ower into one of three species (i.e., setosa, versicolor and virginica) according to its characteristics: sepal length (Sepal .Le), sepal width (Sepal .Wi), petal length (Petal .Le), and petal width (Petal .Wi).

Petal.Le < 2.5

Petal.Wi < 1.8

Petal.Le < 4.9

Figure 1: Iris decision tree.

Complete function exercise1(SepalLen,SepalWid,PetalLen,PetalWid), taking as input four oats representing an iris ower characteristics and     returning a string containing the name of the corresponding species, i.e.,     setosa, versicolor or virginica.

Examples:

exercise1(1 .5,0 .7,2,2 .3) returns setosa’ .

。exercise1(1 .9,1 .5,2 .7,2 .5) returns versicolor’ .

Exercise 2: Dog Breeds Standards

Dog breeds have different standards. Write a function that, given the breed, the height (in inches), the weight (in pounds) and the sex of a dog, returns True/true if the dog complies with the breed standard, and False/false otherwise. In particular, for the sake of this exercise, a dog complies with the standard if it is within 10% (inclusive) of the average height and weight for its breed and sex. The list of breeds and standard characteristics considered in this exercises is given in Table 1 (please, note that all the numbers are made up).

Breed

Male height

Male weight

Female height

Female weight

Bulldog

15

50

14

40

Dalmatian

24

70

19

45

Maltese

9

7

7

6

Table 1: Exercise 2. List of breeds and standard characteristics.

Complete function exercise2(breed,height,weight,male), taking as input a string, breed, two oats, height and weight, and a bool, male, taking value True/true if the dog is male and False/false otherwise. The function should return True/true or False/false depending on whether the dog complies with the breed standard, according to the rule given above.

Examples:

。exercise2(‘Maltese’,9 .5,6 .7,True) should return True, as the dog

complies with the standard for the Maltese breed.

。exercise2(‘Bulldog’,16,44,False) should return False, as the dog

does not comply with the standard for the Bulldog breed (it is too tall).

Exercise 3: Basic Statistics

The  function  in  this  exercise  is  given  a  list/array  of oats.   The  func- tion should return a list of two tuples (python) or an array of two arrays (javascript).  The rst returned list/array contains the minimum, the av- erage, the median and the maximum values for the input list/array  (the average and the median are rounded to the second decimal digit). The sec- ond returned list/array contains the same statistics, this time calculated from the square of the input list/array values.

The average is defined as the sum of the values, divided by the number of values (e.g., the average of [1,5,4,6] is 4).

The median is defined as the middle value” of a list of ordered numbers (note that the values in the input list might not be sorted).  If the list has an odd number of elements, then the median equals the value in the middle (e.g., the median of [1,2,3] is 2). If the list has an even number of elements, then the median is equal to the average between the two central values (e.g., the median of [1,2,3,4] is (2+3)/2  =  2 .5.

Complete function exercise3(l) that takes a list/array of numbers l and returns a list of two tuples (python) or an array of two arrays (javascript) containing summary statistics, as illustrated above.

Examples:

。exercise3([1,2,3,4,5]) should return [(1,3,3,5),(1,11,9,25)].            。exercise3([7,2,4,5]) should return [(2,4 .5,4 .5,7),(4,23 .5,20 .5,49)]

Exercise 4: Finite-State Machine Simulator

A nite-state machine (FSM) or nite-state automaton (FSA, plural:  au-

tomata), finite automaton, or simply a state machine, is a mathematical

model of computation. It is an abstract machine that can be in exactly one

of a nite number of states at any given time.  The FSM can change from

one state to another in response to some inputs; the change from one state

to another is called a transition.  An FSM is defined by a list of its states,

its initial state, and the inputs that trigger each transition (Wikipedia con-

tributors, 2022a).

Complete the function exercise4(trans,init_state,input_list). trans is the dictionary describing the FSM, where the keys are  "state/input"

strings (i.e., current state and input, respectively) and the values are "state/output" strings (i.e., next state and output, respectively).  init_state is the initial

state.  input_list is a list of input values. The function returns the list of

outputs. You can assume that all the inputs, the outputs and the states are

strings.

Examples:

。Let trans be {"a/0":"a/1",  "a/1":"a/0"}, init be a’, and input

be [’0’,’0’,’1’,’1’,’0’,’0’]. The corresponding output is [’1’,’1’,’0’,’0’,’1’,’1’].

。Let trans be {"a/0":"a/1",  "a/1":"b/0","b/0":"b/0","b/1":"a/1"},

init be a’, and input be  [’0’,’0’,’1’,’1’,’0’,’0’].  The cor-

responding output is  [’1’,’1’,’0’,’1’,’1’,’1’].

Exercise 5: Document Stats

Write function exercise5(filename) that reads a text le and provides as

output a tuple  (python) or an array  (javascript) containing the following

values in the following order:

the number of letters in the le

。the number of numeric characters in the le

。the number of symbol characters in the le (i.e., characters that are not alphanumeric and are not whitespaces)

。the number of words in the le (where we assume that words are only

made of alphanumeric characters, and are separated by whitespace or punctuation)

。number of sentences in the le (you can assume that a sentence ends

with a dot, question mark, or exclamation mark)

。number of paragraphs in the le (two paragraphs are separated by an

empty line).

Example: Suppose the text le contains the following text:

She   s e l l s   10   sea

s h e l l s   by

the  7- seas shores .

The  10   s h e l l s   she   s e l l s

are   surely

s e a s h e l l s . So   i f   she

s e l l s   7   s h e l l s

on  the  7- seashores , I m

sure   she   s e l l s

7- seashores s h e l l s .

Then, the function should return (128,  8,  10,  36,  3,  3).

Exercise 6: List Depth

Complete the function exercise6(l) that given a list or array l, calculates the maximum depth of the list/array.  The maximum depth of a list/array without sub-lists/sub-arrays is 1. Otherwise, the maximum is one more than the maximum depth of its sub-lists/sub-arrays.

Examples:

exercise6([1,2,3]) returns 1.

。exercise6([1,[2,[]],[4,5]]) returns 3.

Exercise 7: Change, please

Write a function that determines if it is possible to use a specific number of coins (e2, e1, 50p, 20p, 10p, 5p, 2p, and 1p) to obtain a specific total. For example, it is possible to have a total of e1 using ve coins if they are all 20p. However, there is no way to have a total of e1 using 3 coins.

Complete function exercise7(amount,coins) that returns True/true if the total amount can be obtained using exactly the specified number of coins, or False/false otherwise.

Examples:

。exercise7(3,2) returns True, as one e2 coin and one e1 coin equal

e3.

。exercise7(5,2) returns False, as e5 cannot be totalled using only

two coins.

Exercise 8: Five Letter Unscramble

For this exercise you need to use the provided wordle .txt le that contains a list of 5-letter words used in the game Wordle (Disclaimer:  This list is taken directly from the Wordle game and therefore may contain some words which some people may nd offensive. Reader discretion is advised).

Complete function exercise8(s) that, given a string containing letters, returns the number of unique words in wordle .txt that can be obtained by rearranging the characters in the string. Each character in s can be used at most once.

Examples:

。exercise8(‘sehuoh’) returns 1, as the string can be rearranged into

‘house’ .

。exercise8(‘caarto’) returns 5, as the string can be rearranged into

‘carat’ , ‘carta’ , ‘actor’ , ‘aorta’ , ‘taroc’ .

Exercise 9: Wordle Set

For this exercise you need to use the provided wordle .txt le that contains a list of 5-letter words used in the game Wordle (Disclaimer:  This list is taken directly from the Wordle game and therefore may contain some words which some people may nd offensive. Reader discretion is advised).

Wordle2 is  a web-based word game created  and developed by Welsh software engineer Josh Wardle.  Players have six attempts to guess a ve- letter word, with feedback given for each guess in the form of colored tiles indicating when letters match or occupy the correct position.  After every guess, each letter is marked as either green, yellow or gray: green indicates that letter is correct and in the correct position, yellow means it is in the answer but not in the right position, while gray indicates it is not in the answer at all.   Multiple instances of the same letter in a guess,  such as the o”s in robot”, will be colored green or yellow only if the letter also appears multiple times in the answer; otherwise, excess repeating letters will be colored gray (Wikipedia contributors, 2022b).

Let us define a Wordle set” as the set of ve-letter words in wordle .txt that match a given configuration of green, yellow, and gray letters. Complete function exercise9(green,yellow,gray) that returns the cardinality (i.e. the size) of the corresponding Wordle set. In particular, green is a dictionary that specifies the letters whose positions are known. The keys are positions (i.e., 0,1,2,3,4) and their associated values are their corresponding letters. Only positions with known letters are included in the dictionary. yellow is a dictionary specifying the letters that are in the answer but their position is not known. The keys are letters and their values are sets of known wrong positions (i.e., 0,1,2,3,4). Finally, gray is a set of letters that are known to not be in the answer. Note that a letter cannot be in gray and, at the same time, in green or in yellow. However, a letter could be both in green and in yellow and the clues could refer to the same letter.

Example: Given green  =  {1:’i’,3:’c’}, yellow  =  {’e’:{3}}, and gray  =  {’r’,’a’,’s’,’d’,’f’}, exercise9(green,yellow,gray) returns 5, as the Wordle set is comprised of wince’, ‘mince’, ‘niece’, ‘piece’, and ‘yince’ .

Exercise 10: One Step of Wordle

This exercise builds upon the previous one and, therefore, makes use of the provided wordle .txt le that contains a list of 5-letter words used in the game Wordle (Disclaimer: This list is taken directly from the Wordle game and therefore may contain some words which some people may find offensive. Reader discretion is advised).

Given the green, yellow and gray letters, exercise10(green,yellow,gray) returns the set of best words,’ according to the criterion of choosing the word(s) that provide the most information under every possible scenario, explained in the following.

Given green, yellow, and gray, compute their associated Wordle set S . For every word v in S, calculate its score as follows.

1.  Consider every other word w in S and assume that it is the correct one.  Update the configuration of the green, yellow, and gray letters with the information provided by w using the following rules:

。If vi = wi, add the item i :  vi  to green.

。If vi wi  and vi  is in w, then add to yellow an item i to the set

corresponding to key vi .

。If vi wi  and vi  is not in w, then add an item vi  to gray.

vi   and wi  refer to the letters in position  i in the words v  and w , respectively.

2. Use the new configuration to calculate the hypothetical Wordle set T (v, w) and its cardinality |T (v, w)|.

3.  The score of v is the sum over w of all the cardinalities |T (v, w)|.

The set of best words v* is the set of words having the lowest score. Note that this set might have only one element.

Examples: Given the same green, yellow and gray as in the Exer- cise 9’s example, the set of best words is: {’wince’,  ’yince’,  ’mince’}. In fact, the scores obtained are:   ’niece’  10;  ’yince’ ,  6;  ’piece’ ,  10; ’wince’ , 6; ’mince’, 6.

Learning Outcomes Assessed

LO1: Use high-level programming languages to complete programming tasks.

LO2: Demonstrate familiarity with programming concepts,  simple data- structures and algorithms

Criteria for assessment

Credit will be awarded against the following criteria.

Each exercise can be awarded a maximum of 10 marks. Therefore, per- fectly solving ve exercises will give you  50 marks  (pass),  and perfectly solving ten exercises will give you 100 marks.

Each exercise is marked for both function (8 marks max) and style/qual- ity  (2 marks max).   Exercises that are not a real attempt at solving the problem presented will receive zero marks.

Functionality [8 marks/exercise max] The functional part of the sub- mission is automatically marked by scripts that run the completed function against a set of test cases. You will be provided with two test cases per ex- ercise. During marking, each exercise will be tested against four test cases, including the two provided.  A test should take less than three seconds to complete, otherwise, it is considered failed. For each language (Python and JavaScript) and exercise, passing one test will award you 1 mark. Therefore, the maximum functionality mark of 8 will be given only if all the tests are passed in both languages.

Code quality and style [2 marks/exercise max] Each version of the exercise (i.e., Python and JavaScript) is assessed independently, according to the following criteria:

High quality and style (50-100%,

0.5-1 marks per exercise)

Low quality and style (0-50%, 0-0.5

marks per exercise)

Code is elegant

Code has no redundancies

Code is well commented

Code is perfectly modular (i.e.,

appropriate functions and/or classes

defined)

Code makes smart use of built-in

language features and classes.

Code is messy or overly verbose

Code has multiple redundancies and

repetitions

Code is lacking in meaningful

comments

Code is disorganised

Code does not make use of language

features

Therefore, an exercise solved in only one language could achieve at most one of the style/quality marks. To obtain both marks, the exercise must be solved in both languages.  Also, only exercises that pass at least two tests are evaluated for style and quality.

Feedback and suggestion for future learning

Feedback on your coursework will address the above criteria. Feedback and marks will be returned on the return date via Learning Central and/or email.

The feedback from this assignment will be useful for your second pro- gramming assignment, and will also be relevant for any future programming tasks.