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
12 changes: 9 additions & 3 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
Your function must utilize recursion. It cannot contain any loops.
'''
def count_th(word):
if len(word) < 2:
return 0

# TBC

pass
if word[:2] == "th":
return 1 + count_th(word[2:])
else:
return count_th(word[1:])


print(count_th('worhit'))
28 changes: 26 additions & 2 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,32 @@ def sort(self):
"""
Sort the robot's list.
"""
# Fill this out
pass
# The robot starts at index 0, so if the robot cannot move right then there is
# at most one item in the list (i.e. the list is already sorted)
if not self.can_move_right():
return

# Pick up the first item in the list
self.swap_item()

while True:
# Traverse to the last item in the list, swapping as needed in order to hold the smallest item
while self.can_move_right():
self.move_right()
if self.compare_item() == 1:
self.swap_item()
# The robot is now at the end of the list and holding the smallest item

# Traverse left to the index where this latest iteration started
while self.compare_item() != None:
self.move_left()

# Drop the smallest item in place and move right one item and pick it up
self.swap_item()
if not self.can_move_right():
return
self.move_right()
self.swap_item()


if __name__ == "__main__":
Expand Down
38 changes: 19 additions & 19 deletions robot_sort/test_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@ def test_sorting_random_list(self):
robot.sort()
self.assertEqual(robot._list, sorted(self.random_list))

# def test_stretch_times(self):
# robot = SortingRobot(self.small_list)
# robot.sort()
# self.assertLess(robot._time, 110)

# robot = SortingRobot(self.medium_list)
# robot.sort()
# print(robot._time)
# self.assertLess(robot._time, 1948)

# robot = SortingRobot(self.large_list)
# robot.sort()
# print(robot._time)
# self.assertLess(robot._time, 27513)

# robot = SortingRobot(self.large_varied_list)
# robot.sort()
# print(robot._time)
# self.assertLess(robot._time, 28308)
def test_stretch_times(self):
robot = SortingRobot(self.small_list)
robot.sort()
self.assertLess(robot._time, 110)

robot = SortingRobot(self.medium_list)
robot.sort()
print(robot._time)
self.assertLess(robot._time, 1948)

robot = SortingRobot(self.large_list)
robot.sort()
print(robot._time)
self.assertLess(robot._time, 27513)

robot = SortingRobot(self.large_varied_list)
robot.sort()
print(robot._time)
self.assertLess(robot._time, 28308)


if __name__ == '__main__':
Expand Down
20 changes: 20 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# name = input('Enter your name: ')
# print('Your name is: ',name)

# if name == 'Hamid':
# print('Welcome Admin')
# else:
# print('Sorry your not the admin')

def recursive_fib(n):
if n < 1:
print("Incorrect input.")
elif n == 1:
return 0
elif n == 2:
return 1
else:
return recursive_fib(n - 1) + recursive_fib(n - 2)

n = input(int())
recursive_fib(n)