Concept

How can file-processing programs solve dictionary matching, bridge scoring, Sudoku validation, and CSV conversion?

ComputerScienceOne / Exercises

"Exercise 9.6. Write a crossword puzzle cheater. The program will take, as input, a “partial” word in a crossword puzzle. That is, some of the letters are known (from other solved clues) while some of the letters are not known. For the purposes of this exercise, we’ll use a hyphen as a placeholder for missing letters. Your program will match the partial word against words in a standard English dictionary and list all possible matches. For example, if the user provided foo- as input it might match food , fool , and foot .\n\nExercise 9.7. Bridge is a four player (2 team) game played with a standard 52-card deck. Prior to play, a round of bidding is performed to determine which team is playing for or against the contract, the trump suit, and at what level. Understanding the rules of the game or the bidding conventions involved are not necessary for this exercise. Instead, write a program to assist players in how they should bid based on the following point system. A standard 52-card deck is dealt evenly to 4 different hands (Players 1 thru 4, 13 cards each). Each player’s hand is worth a number of points based on the following rules: • Each Ace in the hand is worth 4 points • Each King is worth 3 • Each Queen is worth 2 • Each Jack is worth 1 • For each suit (Diamond, Spade, Club, Heart) such that the hand has only 2 cards (a “doubleton”) an additional point is added • For each suit that the hand has only 1 card in (a “singleton”) two additional points are added • For each suit that the hand has no cards (a “void”) 3 additional points are added. Write a program that reads in a text file containing a deal. The formatting is as follows: the input file will have 4 lines, one for each player. Each line contains the cards dealt to that player delimited by a single space. The cards are indicated by the rank ( A, K, Q, J, 10 , 9 , . . . , 2) and the suit (D, S, C, H). An example: 3C 3D 7S QD KC AS 6S AC JS 4S JD 7H 6D 5D 8C 7D AH 3H QC 8D JH 5H 9D 7C 9C 4D 2H 10D 8H KS QH 4C 10S 9S 6H 8S KD AD QS 2D 10C 6C 2C 10H 4H 2S 3S 5C 9H KH JC 5S\n\nYour program should process the file and output the total number of points each hand represents. You should not make any assumptions about the ordering of the input. Hand 1 Points: 17 Hand 2 Points: 10 Hand 3 Points: 16 Hand 4 Points: 6\n\nExercise 9.8. The game of Sudoku is played on a 9 × 9 grid in which entries consist of the numbers 1 thru 9. Initially, the board is presented with some values filled in and others blank. The player has to fill in the remaining values until all grid boxes are filled and the following constraints are satisfied. • In each of the 9 rows, each number, 1–9 must appear exactly once • In each of the 9 columns, each number 1–9 must appear exactly once • In each of the 3 × 3 sub-grids, each number 1–9 must appear exactly once A full example is presented in Figure 9.4. Figure 9.4.: A solved Sudoku puzzle Write a program that processes a text file containing a possible sudoku solution and determine if it is a valid or invalid solution. The file will have the following format: it will contain 9 lines with 9 numbers on each line delimited by a single space. If the input represents a valid solution, output ”Valid Solution”, otherwise output at least one reason why the input is not a valid solution.\n\nExercise 9.9. Write a program that parses and processes a data file containing Comma Separated Values (CSV) and produce an equivalent JSON (JavaScript Object Notation) output file containing the same data.\n\nThe input file will have the following format. The first line is a CSV list of column names. Each subsequent line is an individual record with values for each column. The number of columns and rows may vary from file to file. The following is an example containing data about students, which has four columns and 3 records. lastName,firstName,NUID,GPA Castro,Starlin,11223344,3.48 Rizzo,Anthony,55667788,3.95 Bryant,Chris,01234567,2.7 The output file will be formatted in JSON where each “object” (record) is denoted with opening and closing curly brackets, each record is separated by a comma, and all records are enclosed in square brackets (putting them in an array). For each record, each value is denoted with a key (the column name) and a value. For this exercise, treat all values as strings even if they are numbers. For example, the input file above would be formatted as follows. 1 [ 2 { 3 \"lastName\": \"Castro\", 4 \"firstName\": \"Starlin\", 5 \"NUID\": \"11223344\", 6 \"GPA\": \"3.48\" 7 }, 8 { 9 \"lastName\": \"Rizzo\", 10 \"firstName\": \"Anthony\", 11 \"NUID\": \"55667788\", 12 \"GPA\": \"3.95\" 13 }, 14 { 15 \"lastName\": \"Bryant\", 16 \"firstName\": \"Chris\", 17 \"NUID\": \"01234567\", 18 \"GPA\": \"2.7\" 19 } 20 ]"

Related Ideas

How can file-processing programs solve dictionary matching, bridge scoring, Sudoku validation, and CSV conversion? | ComputerScienceOne | Bifalgorithm | Bifalgorithm