- Joined
- Jan 2, 2014
- Messages
- 1,787
- Reaction score
- 939
- First Language
- Chinese
- Primarily Uses
- N/A
This post aims to incite you to share some algorithm challenges, so we can learn from those challenges, seek for answers for difficult problems we're facing, and hopefully apply their answers in case we encounter similar issues 
Let's start by sharing mine:
Challenge 1: Eliminating Repeated Executions
It's given that:
- All custom objects are instances of CustomObj.
- @custom_objs and @active_custom_objs will always 100% correctly store all custom objects and all active custom objects respectively.
- Everything's run by 1 single thread only(i.e.: no multithreading, concurrent computing, parallel programming, etc, so there's no need to worry about race conditions).
- When 1 special event starts, each active custom object will call do_something, but any other custom object(i.e., non active custom object) will only call do_something via do_something_once, which can only be called via do_something.
- do_something_cond will always return the same result during any such special event.
Goal - Ensures that no active custom object will call do_something more than once during any such special event, and each non active custom object will also call do_something exactly once during that special event if and only if do_something_cond returns a TrueClass.
Condition - Your codes must always be 100% correct in this situation.
Constraint - You can only add new codes in parts specified by do_something_once(although you can add anything whenever you want).
My Hint -
My Solution -
It achieves the goal, meets the conditions and conform to the constraint.
Proof:
Case 1 - do_something_cond returns a TrueClass
- As everything's run by 1 single thread only, only 1 custom object will be within do_something_once at a time.
- When a custom object becomes the 1st one entering do_something_once, @custom_obj_lock will be an empty array.
- That custom object will then push every other custom objects into @custom_obj_lock, and ask all non active custom objects to call do_something.
- When any other custom object enters do_something_once, it'll delete itself from @custom_obj_lock and exit that method.
- It means no other active custom object will call do_something more than once, and all non active custom object will only call do_something once.
- When the special event ends, @custom_obj_lock will become an empty array again, as all custom objects have called do_something_once exactly once.
Case 2 - do_something_cond returns a FalseClass
- Every active custom object will call do_something_once and then immediately return to do_something.
- It means no active custom object will call do_something more than once, and no non active custom object will ever call do_something.
Combining these 2 cases, this solution's always 100% correct in the specified situation.
I'll continue to post more and more. Let's share yours, so we can all benefit from each other here
Edit: The intended difficulty level of Challenge 1: Eliminating Repeated Executions changed from 4(Decent scripting proficiency) to 3(Some scripting proficiency), as it turned out to be much easier than what I've initially thought lol
Let's start by sharing mine:
Challenge 1: Eliminating Repeated Executions
Intended difficulty level - 3(Some scripting proficiency)
Situation - Consider the below piece of code:
module CustomObjManager attr_reader :custom_objs # An array of all custom objects attr_reader :active_custom_objs # An array of all active custom objects # Eliminates repeated do_something execution for all custom objects def self.do_something_once(obj) return unless do_something_cond # Write your codes here only # end # do_something_onceend # CustomObjManagerclass CustomObj def do_something do_something_part_1 CustomObjManager.do_something_once(self) do_something_part_2 end # do_somethingend # CustomObj
Situation - Consider the below piece of code:
module CustomObjManager attr_reader :custom_objs # An array of all custom objects attr_reader :active_custom_objs # An array of all active custom objects # Eliminates repeated do_something execution for all custom objects def self.do_something_once(obj) return unless do_something_cond # Write your codes here only # end # do_something_onceend # CustomObjManagerclass CustomObj def do_something do_something_part_1 CustomObjManager.do_something_once(self) do_something_part_2 end # do_somethingend # CustomObj
- All custom objects are instances of CustomObj.
- @custom_objs and @active_custom_objs will always 100% correctly store all custom objects and all active custom objects respectively.
- Everything's run by 1 single thread only(i.e.: no multithreading, concurrent computing, parallel programming, etc, so there's no need to worry about race conditions).
- When 1 special event starts, each active custom object will call do_something, but any other custom object(i.e., non active custom object) will only call do_something via do_something_once, which can only be called via do_something.
- do_something_cond will always return the same result during any such special event.
Goal - Ensures that no active custom object will call do_something more than once during any such special event, and each non active custom object will also call do_something exactly once during that special event if and only if do_something_cond returns a TrueClass.
Condition - Your codes must always be 100% correct in this situation.
Constraint - You can only add new codes in parts specified by do_something_once(although you can add anything whenever you want).
My Hint -
Use 1 array instance variable called @custom_obj_lock with its elements always being custom objects.
# Eliminates repeated do_something execution for all custom objects def self.do_something_once(obj) return unless do_something_cond # Write your codes here only @custom_obj_lock ||= [] return @custom_obj_lock.delete(obj) if @custom_obj_lock.include?(obj) @custom_obj_lock = @custom_objs - [obj] (@custom_objs - @active_custom_objs).each { |obj| obj.do_something } # end # do_something_once
Proof:
Case 1 - do_something_cond returns a TrueClass
- As everything's run by 1 single thread only, only 1 custom object will be within do_something_once at a time.
- When a custom object becomes the 1st one entering do_something_once, @custom_obj_lock will be an empty array.
- That custom object will then push every other custom objects into @custom_obj_lock, and ask all non active custom objects to call do_something.
- When any other custom object enters do_something_once, it'll delete itself from @custom_obj_lock and exit that method.
- It means no other active custom object will call do_something more than once, and all non active custom object will only call do_something once.
- When the special event ends, @custom_obj_lock will become an empty array again, as all custom objects have called do_something_once exactly once.
Case 2 - do_something_cond returns a FalseClass
- Every active custom object will call do_something_once and then immediately return to do_something.
- It means no active custom object will call do_something more than once, and no non active custom object will ever call do_something.
Combining these 2 cases, this solution's always 100% correct in the specified situation.
I'll continue to post more and more. Let's share yours, so we can all benefit from each other here
Edit: The intended difficulty level of Challenge 1: Eliminating Repeated Executions changed from 4(Decent scripting proficiency) to 3(Some scripting proficiency), as it turned out to be much easier than what I've initially thought lol
Last edited by a moderator:

