0% found this document useful (0 votes)
37 views5 pages

CS2030DE Lab 7 Final v2

Lab 7 of CS2030DE focuses on Lazy Evaluation and Functor, with an assignment released on March 26, 2025, and due by April 2, 2025. It includes three questions covering a 1D Game of Life simulation, a navigation problem using an NxN grid, and a function for detecting repeated substrings in a string generator. The lab is graded out of 100 marks, with specific guidelines and requirements for submission and testing.

Uploaded by

sahilbaori22
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)
37 views5 pages

CS2030DE Lab 7 Final v2

Lab 7 of CS2030DE focuses on Lazy Evaluation and Functor, with an assignment released on March 26, 2025, and due by April 2, 2025. It includes three questions covering a 1D Game of Life simulation, a navigation problem using an NxN grid, and a function for detecting repeated substrings in a string generator. The lab is graded out of 100 marks, with specific guidelines and requirements for submission and testing.

Uploaded by

sahilbaori22
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/ 5

CS2030DE Programming Methodology II

Lab 7 – Lazy Evaluation and Functor

Assignment Release date: 26th March, 2025


Submission deadline: 23:59 PM, Wednesday, 2 April, 2025
Grading: There are 3 questions in this exercise. Your programs will be graded out
of 100 marks. The guidelines explained in the template will also carry marks. So
please adhere to the guidelines. Please note that passing all public test cases does not
guarantee full marks.
Concepts used: Lazy evaluation and Functor

Instructions:
(1) You must test all possible input cases/different parameter values, etc., to make
sure your code runs successfully. There may be other cases of inputs and/or input
parameters that may not show a “green” signal!
(2) Do make sure to submit your work at least 30 minutes before the deadline to
avoid any last-minute technical issues or delays. Submissions made too close to the
deadline will not be excused for reasons such as folder closures or system timeouts.
Plan accordingly to ensure a smooth and timely submission.
Grading:
Lab work - 100 marks for each lab (20 marks in class and 80 marks post lab);
Final marks for each lab will have a weightage of four (4) marks. Total of all the 8
labs account for 32 marks.
Your attendance to every lab carries: 1 Mark
Lab component overall: 8 + 32 = 40 Marks.

Question 1: (20 Marks)


Question 2: 1D Game of Life (20 Marks)
In this task, you will simulate a 1D Game of Life—a simplified version of Conway's
Game of Life where each cell in a one-dimensional grid can either be alive (1) or
dead (0). The state of each cell in the next generation depends on the state of its
neighbours, following a specific rule. The game will be simulated over multiple
iterations, applying the rules to update the grid.
Task 1:
Create a generate(population) function that takes a list population, representing
the current state of the game, and returns a new list representing the next state based
on the following rules. The list is a 1D array of cells where 1 indicates a live cell,
and 0 indicates a dead cell. The rules for the next generation are as following:
• A live cell always becomes dead
• A dead cell becomes alive if exactly one of its immediate neighbours is alive.
Note: The computation should be done in one line (function should consist of a
single return statement only). Use functional programming techniques such as map
and lambda functions.
Task 2:
Implement a GameFunctor class that includes a map()method, which maps the
current generation to the next generation based on the rules specified in Task 1. The
functor should be capable of generating an infinite number of generations.
Task 3:
Implement the function gameOfLife(population, generatorFunction,
nIters) to simulate the Game of Life for the specified number of iterations. Return
a list of lists, where each inner list represents the population for each generation,
including the initial population.
Question 3: Navigation Problem (40 Marks)
0 5 9 8
7 2 1 2
3 4 8 3
1 6 2 9
Figure 1: Example of a Navigation Grid with Rewards
You are given an NxN grid (N >= 1), where each cell contains a reward. A player
starts at the top-left corner and must navigate to the bottom-right corner, moving
only right or down. As the player moves through the grid, they accumulate rewards
from the cells they pass through.
Your task is to write a program that finds the path which maximizes the total reward
collected, using functors and recursion. Implement the following classes:
class Movement:
Method Description
__init__(grid) Initialize with an NxN grid
moveRight() Returns a function that maps the current position and
reward to the new position and updated reward when
the player moves right by one step.
moveDown() Returns a function that maps the current position and
reward to the new position and updated reward when
the player moves down one step.

class MoveFunctor:
Method Description
__init__(positio Initializes the functor class with the given position and
n, reward) reward.
map(moveMethod) Returns a new Functor after applying the movement.

class Navigator:
Method Description
__init__(grid) Initializes with an NxN grid
move(path, Apply the movements as specified in path (e.g., ['R',
p=(0,0), r=0) 'D', 'D'], where 'R' represents a right movement and
'D' represents a down movement), starting from the
initial position p and initial reward r. Chain the
movement functors to execute the movements. This
method should be implemented using lazy execution,
yielding the resulting functor after each step as
needed.
navigate() Returns the maximum accumulated reward and the
optimal path to achieve it using recursion. This
function must be pure, and movements should be
simulated using the map() method of the
MoveFunctor class.

Question 4: Repeated Substrings (20 Marks)


Implement a function repeated_substring that takes in a generator yielding
characters from a string, which may be of finite or infinite length, and finds repeated
substrings of length k within the most recent w characters read so far. The function
should yield each repeated substring only once, as soon as its repetition is detected.
Inputs:
• genS: a generator that yields the input string, which may be finite or infinite.
• k: the length of the substring to detect.
• w: the number of most recent read characters to consider when detecting
repetitions.
Output:
• A generator that yields repeated substrings one at a time, as soon as they are
detected.

Example 1: infinite string


gen = repeated_substring((x for x in itertools.cycle("abcdefdebc")), 2, 100)
Input:
• A generator that yields characters of the infinite string formed by repeating
"abcdefdebc": " abcdefdebcabcdefdebcabcdefdebc... "
• k=2: looking for repeated substrings of length 2.
• w=100: search for repetitions within the most recent 100 characters read so
far.
Output:
• A generator that yields repeated substrings: ' de ' followed by ' bc '.

Example 2: finite string


repeated_substring((x for x in iter('abcdefde')), 2, 100)
Input:
• A generator that yields characters of the finite string 'abcdefde'.
• k=2: looking for repeated substrings of length 2.
• w=100: search for repetitions within the most recent 100 characters read so
far.
Output:
• A generator that yields the repeated substring: ' de '

Note on Test Utilities (used in Coursemology test cases):


• (x for x in iter(s)): returns a generator that yields characters from the finite
string s.
• (x for x in itertools.cycle(s)): generates an infinite string by repeating s
indefinitely.
• itertools.islice(g, k): retrieves the first k elements yielded by iterator g.

-END-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy