Check out this link for more tree recursion practice: https://practice.geeksforgeeks.org/topic-tags
Write a recursive function that checks whether a string is a palindrome (a palindrome is a string that's the same when reads forwards and backwards.)
Write a procedure that computes elements of Pascal's triangle by means of a recursive process. Define the procedure pascal(row, column) which takes a row and a column, and finds the value at that position in the triangle.
Consider the subset_sum problem: you are given a list of integers and a number k. Is there a subset of the list that adds up to k?
We will now write one of the faster sorting algorithms commonly used, named Mergesort. Merge sort works like this:
a. If there’s only one (or zero) item(s) in the sequence, it’s already sorted! b. If there’s more than one item, then we can split the sequence in half, sort each half recursively, then merge the results (using the merge procedure from earlier in the notes). The result will be a sorted sequence.
Using the described algorithm, write a function mergesort(s) that takes an unsorted sequence s and sorts it.
def towers_of_hanoi(n, start, end):
…
def move_disk(start, end):
…
class Link:
def __init__(self, first, rest=empty):
assert rest is Link.empty or isinstance(rest, Link)
self.first = first
self.rest = rest
def __repr__(self):
if self.rest is Link.empty:
return "Link({})".format(self.first)
else:
return "Link({}, {})".format(self.first, self.rest)
class Link:
def __init__(self, first = None, rest = None):
self.first = first
self.rest = rest
def __repr__(self):
# empty link ---> None
if self.first == None:
return "()"
else:
return 'Link' + '(' + str(self.first) + "," + self.rest.__repr__() + ')'
def multiply(self, n):
if self.rest.first == None:
self.first = n * self.first
else:
self.first = n * self.first
self.rest.multiply(n)
def a_multiply(self, n):
# if self == Link.empty
if self.rest.first == None:
return Link(self.first * n)
else:
return Link(self.first * n, self.rest.a_multiply(n))
def skip_node(self, n = 1):
if self.first == None:
return Link()
elif self.rest.first == None:
return Link(self.first, Link())
else:
return Link(self.first, self.rest.rest.skip_node(n))
x = Link(5, Link(7, Link(9, Link(6, Link(8, Link())))))
y = [5,7,9,6]
print(x.a_multiply(3))
print(x)
print(x.skip_node())
Note 1: The term ‘list’ refers to linked list. Note 2: You may use methods you have created in the previous problems to support your code for later problems.
Create a method that…
Source: https://stackoverflow.com/questions/245800/oop-problems-to-use-for-coding-tests-during-interviews
1) Create model classes that will properly represent the following constructs:
Define a Shape object, where the object is any two dimensional figure, and has the following characteristics: a name, a perimeter, and a surface area.
Define a Circle, retaining and accurately outputting the values of the aforementioned characteristics of a Shape.
Define a Triangle. This time, the name of the triangle should take into account if it is equilateral (all 3 sides are the same length), isosceles (only 2 sides are the same length), or scalene (no 2 sides are the same).
Continue with the quadrilaterals!
2) We need to control access to a customer web site.
each customer may have one or more people to access the site
different people from different customers may be able to view different parts of the site
the same person may work for more than one customer
customers want to manage permissions based on the person, department, team, or project
Design a solution for this using object-oriented technique.
3) (Are you smarter than a CS Grad?) Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz."
4) Make a rough class diagram of the game Pacman. What classes would you need? What class / instance variables are necessary? Would some classes depend on the others?
Source: https://www.careercup.com/page?pid=trees-and-graphs-interview-questions
Directions: For each problem, write a new method for the Tree class. You can only use the methods already given or a method from the problems above.
class Tree:
def __init__(self, label, branches):
self.label = label
self.branches = branches
def is_leaf(self):
if not self.branches:
return True
return False
def branches(self):
return self.branches
def label(self):
return self.label
Input:
G
/ \
B R
/ \ / \
B B R R
/ \ / \
B R R R
Output:
R
/ \
R R
\ / \
R R R