Description: Description: Description: Description: Description: Description: Description: Description: Breaking News! Click here for details.

 CSC 131 - Introduction to Computer Science

Homework 7 – Due Thursday, Oct. 26, 11:59pm

Submitted on Blackboard (learn.uncw.edu)

Programs based on material from Chapter 5.

 

Program 1: Ordered Parameters

Write a Python method, inOrder, that is given three integers and returns true if the three integers are in order from smallest to largest.  Otherwise, it should return False.

 

print(inOrder(1, 9, 17))

print(inOrder(-8, 4, 100))

print(inOrder(9, 1, 17))

print(inOrder(1, 17, 9))

 

should have the output:

 

True

True

False

False

 

 

Program 2: Get y/n from User

Write a Python method, getContinue, which displays a prompt to the user ‘Do you want to continue (y/n)?”.    The user will input something.   If the value is not ‘Y’, ‘y’, ‘N’ or ‘n’, the method continues to prompt them until they give the proper input.  At that point, the method should return either a lower-case ‘y’ or ‘n’ depending on what the user typed.

 

For instance, the following bit of code:

 

answer = getContinue()

print("The user answered: ", answer)

 

could potentially generate the following behavior:

 

Do you want to continue (y/n)? dog

Do you want to continue (y/n)? cat

Do you want to continue (y/n)? y

The user answered:  y

 

 

Program 3: Above threshold

Write a Python method, aboveThreshold, that takes two parameters, a list of numeric values and a single number.   The method should return a count of how many numbers in the list are above the second parameter.  For instance, the following segment of code

 

list1 = [5, 9, 12, 100, 200, 3]

list2 = [8, 12, 14, 77, 700, 1000, 1001]

list3 = [1]

print(aboveThreshold(list1, 10))

print(aboveThreshold(list2, 10))

print(aboveThreshold(list3, 10))

 

 

should have an output of

 

3

6

0

 

 

Program 4: Tic-Tac-Toe Winner

Write a Python method, winningBoard, that takes a single parameter, a list.  That list will contain the positions of a 3x3 tic-tac-toe board such the first element of the list is a list of the values of the first row, the second item of the list is a list with the values of the second row, and the third item of the list is a list with the values of the third row.  For instance the following board:

 

Image result for tic tac toe

Would be represented as [ ['O', '', 'X'], ['', 'O', ''], ['X', '', 'X'] ]  Notice I’m using the empty string to represent a blank space. 

 

The method winningBoard should return ‘X’ if player X has 3 in a row, ‘O’ if player Y has 3 in a row, and False otherwise.   Feel free to make other methods if you find them useful.

 

Here are some examples of code:

 

board1 = [ ['O', '', 'X'], ['', 'O', ''], ['X', '', 'X'] ]

print("Board1:", winningBoard(board1))

 

board2 = [ ['X', 'X', 'X'], ['', 'O', ''], ['X', '', 'X'] ]

print("Board2:", winningBoard(board2))

 

board3 = [ ['X', 'O', 'X'], ['', 'O', ''], ['X', 'O', 'X'] ]

print("Board3:", winningBoard(board3))

 

should have an output of

 

Board1: False

Board2: X

Board3: O