Question of Coding
Question of Coding
Question of Coding
Workshop Details
Constraints and Rules
Page | 2
[CheckRoom] This process should be called after each "Move" action (only when exploring). If there
is
a room at the current location, it is explored and the time taken to explore the room is
returned (in minutes). Note: if there was no room, zero is returned
Expected Logic
- Determine which sub-process to call based on the output from of the black box sub-
process [RandomSearch] which will return A, B, or X (do not define the black box
process, just know you will get a random character output):
[ChangeDirection] Rotates your direction 90 degrees (left or right) in-place without affecting
distance or time. The output will always be opposite the input value (example: if
[L]eft is the input, the output should be [R]ight.
[TurnAround] Rotates your direction 180 degrees in-place (without affecting distance or time)
APS145 Applied Problem Solving
Page | 3
Expected Logic
- Call [ChangeDirection] twice in sequence providing the same input value (either "L"
or "R")
- There is no need to store the output value generated by the [ChangeDirection] sub-
process call
Each room you discover will take an unknown/variable number of minutes (based on what lies
within).
Track room exploration time separately from travel/distance time. You will need to develop a
tracking
method to store each room duration and use this information to calculate predictive estimates in the
determination of whether you can risk further exploration time or should turn around and return to
safety before your flashlight power runs out.
To help you accomplish these estimates, you should apply the following logic:
1. Maintain an ongoing average room exploration time (recalculate after each room exploration)
2. Keep track of the longest time spent in a room (this will represent the worst-case scenario based on
actual data)
3. Apply a pessimistic risk factor in predicting the next room exploration time by adding both the
average room exploration time with the room that took the longest time. This estimated time
should be used in the assessment of whether further exploration is possible.
4. You must also factor the time required to return to safety (distance traveled).
Note: When returning, no room explorations can be performed so the time is based purely on the
traveled distance (result of a "[Move]" command).
ExploreRoom-A
Congratulations you found a room that will provide you with some much-needed fun time playing
"Tic-
Tac-Toe"! Use the small dedicated handheld "Tic-Tac-Toe" game machine and enjoy!
Page | 4
o CheckWinner - this will check the board for a winner and return 0 if no winner yet, 1 if
player 1 wins, 2 if player 2 wins or 3 if there is a tie.
o ClearBoard - removes all X's and O's from the board to prepare for a new game
Constraints
• Determine who starts the first game by calling a black box [RandomBinary] process that returns
a number (1 or 2). Do not define the black box process, just know you will get a random
number output:
o 1: means Player-1 starts
o 2: means Player-2 starts
• Each subsequent game's starting player will be whoever won the previous game or, if a tie, then
the player who did not start last time
• Each player move adds 1 minute to the explored time
• Each tied game adds another 2 minutes to the explored time
• Each game you lose (Player-1), 5 minutes is added to the explored time
• The OUTPUT returned by this process is the total time accumulated to play all three games.
ExploreRoom-B
Congratulations you found another room that will provide you with some much-needed fun time
playing "Hang-Person" (formerly known as "Hangman")! Use the small dedicated handheld "Hang-
Person" game machine and enjoy!
This game requires 1 player. The game begins when a random generated hidden secret word
(between 5 and 15 characters) is chosen by the game logic. You are shown a series of underscore
characters (underlines) that represent the hidden characters of the secret word. You must guess
letters one at a time in an attempt to reveal/show as many matching letters in the secret word as you
can. If you can correctly guess the word you win otherwise, you lose!
• You can call RandomWord to create a word to guess and then call Guess and/or Solve until you
guess the word or run out of guesses.APS145 Applied Problem Solving
Page | 5
Your Task [Logic 1] the logic for Room A which will return the time it took to explore Room A.
[Logic 2] the logic for Room B which will return the explore time for the room.
[Logic 3] the main logic for exploring the dungeon (including returning to the starting point) which
will
invoke logic 1 or logic 2 when it needs to explore room A or room B.
Answer:
Hello I have here the answer for your query. Please be guided below. Thanks!
class Rule:
"""Invoke these functions to define rules.
Rules are not run as they are defined,
they are run when you issue `session.commit()'.
Use code completion to discover rules.
"""
@staticmethod
def sum(derive: InstrumentedAttribute, as_sum_of: any, where: any = None):
"""
Derive parent column as sum of designated child column, optional where
Example
Rule.sum(derive=Customer.Balance, as_sum_of=Order.AmountTotal,
where=Lambda row: row.ShippedDate is None)
Optimized to eliminate / minimize SQLs: Pruning, Adjustment Logic
"""
return Sum(derive, as_sum_of, where)
@staticmethod
def count(derive: InstrumentedAttribute, as_count_of: object, where: any = None):
"""
Derive parent column as count of designated child rows
Example
Rule.count(derive=Customer.UnPaidOrders, as_count_of=Order,
where=Lambda row: row.ShippedDate is None)
Optimized to eliminate / minimize SQLs: Pruning, Adjustment Logic
"""
return Count(derive, as_count_of, where)
@staticmethod
def constraint(validate: object, as_condition: any = None,
error_msg: str = "(error_msg not provided)", calling: Callable = None):
"""
Constraints declare condition that must be true for all commits
Example
Rule.constraint(validate=Customer, as_condition=lambda row: row.Balance <= row.CreditLimit,
error_msg="balance ({row.Balance}) exceeds credit ({row.CreditLimit})")
"""
return Constraint(validate=validate, calling=calling, as_condition=as_condition,
error_msg=error_msg) # --> load_logic
@staticmethod
def formula(derive: InstrumentedAttribute, calling: Callable = None,
as_expression: Callable = None, as_exp: str = None):
"""
Formulas declare column value, based on current and parent rows
Example
Rule.formula(derive=OrderDetail.Amount,
as_expression=lambda row: row.UnitPrice * row.Quantity)
Unlike Copy rules, Parent changes are propagated to child row(s)
Supply 1 (one) of the following:
* as_exp - string (for very short expressions - price * quantity)
* ex_expression - lambda (for type checking)
* calling - function (for more complex formula, with old_row)
"""
return Formula(derive=derive, calling=calling, as_exp=as_exp, as_expression=as_expression)
@staticmethod
def copy(derive: InstrumentedAttribute, from_parent: any):
"""
Copy declares child column copied from parent column
Example
Rule.copy(derive=OrderDetail.UnitPrice, from_parent=Product.UnitPrice)
Unlike formulas references, parent changes are not propagated to children
"""
return Copy(derive=derive, from_parent=from_parent)
@staticmethod
def early_row_event(on_class: object, calling: Callable = None):
"""
Row Events are Python functions called before logic
Possible multiple calls per transaction
Use: computing foreign keys...
"""
EarlyRowEvent(on_class, calling) # --> load_logic
@staticmethod
def row_event(on_class: object, calling: Callable = None):
"""
Row Events are Python functions called during logic, after formulas/constraints
Possible multiple calls per transaction
Use: recursive explosions (e.g, Bill of Materials)
"""
RowEvent(on_class, calling) # --> load_logic
@staticmethod
def commit_row_event(on_class: object, calling: Callable = None):
"""
Row Events are Python functions called during logic, after formulas/constraints
Example
Rule.commit_row_event(on_class=Order, calling=congratulate_sales_rep)
1 call per row, per transaction
Use: send mail/message
"""
return CommitRowEvent(on_class, calling) # --> load_logic