CSC207 - Lab 11
Functional Practice

Overview

This lab is repetitive practice writing functions for many different situations involving strings, lists and boolean conditions.

Description

Success on large programming project can depend on your ability to program small useful functions. For this lab,
  1. Write a function for each of the problems below.
  2. Make sure your function matches the outputs listed.
  3. Develop three test cases of your own which fully test your function.
  4. Record the output of your function on your three test cases in a Google Doc.

Sandwiches

A sandwich is two pieces of bread with something in between. Write a function getSandwich(s) that will return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread.

getSandwich("breadjambread") → "jam"
getSandwich("xxbreadjambreadyy") → "jam"
getSandwich("xxbreadyy") → "" 

xyBalance

We'll say that a String is xy-balanced if for all the 'x' chars in the string, there is a 'y' char somewhere later in the string. So "xxy" is balanced, but "xyx" is not. Write a function xyBalance(s) that will return True if the given string is xy-balanced.

xyBalance("aaxbby") → True
xyBalance("aaxbb") → False
xyBalance("yaaxbb") → False 

TripleUp

Write a function tripleUp(t) that will return True if the list contains, somewhere, three increasing adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25.

tripleUp([1, 4, 5, 6, 2]) → True
tripleUp([1, 2, 3]) → True
tripleUp([1, 2, 4]) → False 

evenOdd

Write a function evenOdd(t) that will return a list that contains the exact same numbers as the given list, but rearranged so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order. You may modify and return the given list, or make a new list.

evenOdd([1, 0, 1, 0, 0, 1, 1]) → [0, 0, 0, 1, 1, 1, 1]
evenOdd([3, 3, 2]) → [2, 3, 3]
evenOdd([2, 2, 2]) → [2, 2, 2] 

canBalance

Given a non-empty list, write a function canBalance(t) that will return True if there is a place to split the list so that the sum of the numbers on one side is equal to the sum of the numbers on the other side.

canBalance([1, 1, 1, 2, 1]) → True
canBalance([2, 1, 1, 2, 1]) → False
canBalance([10, 10]) → True 

Thanks to Nick Parlante at JavaBat.com for these exercise ideas.