Write a recursive function
orange(n)
that prints the word 'orange' n times.
Write a recursive function isPowOfTwo(n)
that returns
True if n is a power
of 2.
Write a recursive function count(x,
l
)
that returns the number of times that x
occurs in the given list.
Write a recursive function mul(a, b)
that returns the
product (a * b) for positive integers a and b. You may use the
built-in addition operator (+), but not the multiplication operator
(*).
Write a recursive function largest(
l
)
that returns the largest element in a
list.
Write a recursive procedure reverse()
that reads a
series of lines until the end of the input, and write the lines out
in reverse order. Do not use any loops or lists.
Write a recursive function pos(s, c)
that returns the
first index at which the character c appears in the string s, or None
if none.
Write a recursive function reverse(s): string
that
reverses the string s. You may use string concatenation, but may not
call any library functions.
Write a recursive function that determines whether a string is a palindrome.
Use recursion to write a function sameAsFirst(a)
that
returns the number of integers in an array that are equal to its
first element.
Suppose that we run a sorting algorithm on an array that is already sorted. How many array writes will it perform if we use each of the following algorithms? An answer in big-O notation is acceptable.
bubble sort
insertion sort
mergesort
quicksort (using Hoare partitioning)
Suppose that we sort an array of distinct integers of length N, and that initially the largest integer is at the beginning of the array. How many times will this integer move to a new array position if we use each of the following algorithms?
bubble sort
insertion sort
mergesort
quicksort (using Hoare partitioning)
In the lecture we implemented a fixed-size queue using an array. Now implement an array-based queue that can grow to any size.
In the following
exercises, you are given a class
LinkedList
with
a single attribute head
that
points to the head of a linked list. Each list node is an instance of
the Node
class:
class Node: def __init__(self, val, next): self.val = val self.next = next class LinkedList: def __init__(self, head): self.head = head
Write method that
appends a value to a LinkedList
.
Write a method that
deletes the first node (if any) in a LinkedList
.
Write a method
that deletes the last node (if any) in a LinkedList
.
Write a method
that moves the last node in a LinkedList
to the head of
the list.
Write a method
that returns the second-to-last element of a LinkedList
of non-negative integers. If there is no such element, return None
.
Write a method
that deletes the Nth element (if any) of a LinkedList
.
Write a method
that deletes every other element of a LinkedList
,
starting with the first.
Write a method that reverses a LinkedList
. Do not
allocate any additional memory.
Write a method that sorts a LinkedList
using
mergesort. Do not use any
ordinary lists (i.e. arrays).
Write a method
that splits a LinkedList
into two LinkedList
objects, one containing even numbers, one odd. The numbers in each
list should appear in the same order as in the original list.