CSC207 - Lab 10
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.

firstAgain

Given a string, write a function frontAgain(s) that will return True if the first 2 chars in the string also appear at the end of the string, such as with "edited".

frontAgain("edited") → True
frontAgain("edit") → False
frontAgain("ed") → True 

Tea Party

We are having a party with amounts of tea and candy. Write a function teaParty(tea, candy) that will return the int outcome of the party as either, bad=0, good=1, or great=2. A party is good (1) if both tea and candy are at least 5. However, if either tea or candy is at least double the amount of the other one, the party is great (2). However, in all cases, if either tea or candy is less than 5, the party is always bad (0).

teaParty(6, 8) → 1
teaParty(3, 8) → 0
teaParty(20, 6) → 2 

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") → "" 

BlackJack

Given 2 int values greater than 0, write a function blackjack(player, dealer) that returns whichever value is nearest to 21 without going over. Return 0 if they both go over.

blackjack(19, 21) → 21
blackjack(21, 19) → 21
blackjack(19, 22) → 19 

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.