Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Short-Answer/Algorithms_Answers.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

## Exercise I

a)
a) O(n) -- run time increases linearly as n increases. Single loop.


b)
b) O(n^2) -- 2 loops. So run time increases twice as n increases.


c)
c) It is O(n) because the runtime will increase at a constant pace with the size of the input.

## Exercise II

Binary search is best way to go about this.

1. Drop the egg on floor 1, if it breaks then f =0 and end program

2. if egg does not break.
halve the total num of floors, if total floors is odd then move to 1 floor lower.
drop egg
if it breaks, consider this floor as top floor. Move to middle of 1st floor and current floor.
if its does not break, consider this as bottom floor. move to middle floor of topmost and current floor.

3. follow above process and find the floor where egg does not break, move 1 floor ahead and find where it does not.

4. this is give the 'f' answer.

9 changes: 6 additions & 3 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
Your function must utilize recursion. It cannot contain any loops.
'''
def count_th(word):
if len(word)<2: # if word length is less than 2 return 0 as we are looking for 'th'
return 0
if word[0:2] == 'th': # use recursion to repeated checks for 'th'
return count_th(word[1:]) + 1

# TBC

pass
return count_th(word[1:])

40 changes: 38 additions & 2 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,50 @@ def sort(self):
"""
Sort the robot's list.
"""
# Fill this out
pass
# in this algo I am using the light for exit loop, or know when to quit sorting.
# motivation in this algo is to reduce the number of steps robot is taking to get the sorting done.
# idea is not to reduce the code execution time. Also we want to keep space complexity as 1, since making more
# copies is not an option. So recursive sorting is ruled out. I am doing an iterative approach.

# Plan:
# 1. Robot picks up first ele, moves right if possible and compares.
# 2. if comparioson result is 1, swap. move left by 1 step and swap again. set light ON
# 3. if comparision result is not 1, move left 1 step, but back the carrying element, move 1 step to right.
# No action with light.
# 4. repeat steps 1-3, until robot cannot move right.
# 5. Move back to first element after 1 pass and repeat steps 1-4
# 6. exit when after 1 pass through the array light remains off.

self.set_light_on() # set light on in beginning.

while self.light_is_on():
self.set_light_off()

while self.can_move_right(): # Step 1
self.swap_item()
self.move_right()

if self.compare_item() == 1: # Step 2
self.swap_item()
self.move_left()
self.swap_item()
self.move_right()
self.set_light_on()

else: # Step 3
self.move_left()
self.swap_item()
self.move_right()

while self.can_move_left(): # step 5
self.move_left()


if __name__ == "__main__":
# Test our your implementation from the command line
# with `python robot_sort.py`


l = [15, 41, 58, 49, 26, 4, 28, 8, 61, 60, 65, 21, 78, 14, 35, 90, 54, 5, 0, 87, 82, 96, 43, 92, 62, 97, 69, 94, 99, 93, 76, 47, 2, 88, 51, 40, 95, 6, 23, 81, 30, 19, 25, 91, 18, 68, 71, 9, 66, 1, 45, 33, 3, 72, 16, 85, 27, 59, 64, 39, 32, 24, 38, 84, 44, 80, 11, 73, 42, 20, 10, 29, 22, 98, 17, 48, 52, 67, 53, 74, 77, 37, 63, 31, 7, 75, 36, 89, 70, 34, 79, 83, 13, 57, 86, 12, 56, 50, 55, 46]

robot = SortingRobot(l)
Expand Down