Lecture 5 Modify Box Database Scrabble Program Modify
Lecture 5 Modify Box Database Scrabble Program
Modify a Box Database File Box DB file contains: 165 47 160 323 445 533 447 834 667 532 340 325 368 249 97 308 64 64 <x> <y> <xsize> 64 64 -3 -3 <ysize> <const> Increase the size of each box by 20%, but keep the centers the same Use a spreadsheet ?
Reading from files >>> infile=file(“myprimes. txt”, ”r”) >>> primes=infile. readlines() >>> infile. close() >>> primes. . . prints all of the lines in the file as a list of strings >>> primes=[int(i) for i in primes] >>> primes. . . prints all of the lines in the file as a list of integers
Modify a Box Database File >>> increase=0. 2 # scale boxes by 1+increase >>> filein=file(“box. db”, ”r”) # open old file for reading >>> fileout=file(“box 2. db”, ”w”) # open new file for writing >>> while (1) : # loop forever a=filein. readline() if len(a)==0 : break # len(a)==0 at end of file b=[int(i) for i in a. split()] # Turns '1 2 3 4 5' into [1, 2, 3, 4, 5] b[0]-=b[2]*increase/2 # Keep the centers in the right place b[1]-=b[3]*increase/2 b[2]=int(b[2]*(1. 0+increase)) b[3]=int(b[3]*(1. 0+increase)) fileout. write(“%dt%dt%dn”%(b[0], b[1], b[2], b[3], b[4])) >>> filein. close() >>> fileout. close()
Modify a Box Database File Box DB file contains: # A starting comment at the beginning 165 667 64 64 -3 47 532 64 64 -3 160 340 64 64 -3 # Boxes below here are questionable 323 325 64 64 -3 445 368 64 64 -3 533 249 64 64 -3 447 97 64 64 -3 834 308 64 64 -3
Modify a Box Database File >>> increase=0. 2 >>> filein=file(“box. db”, ”r”) >>> fileout=file(“box 2. db”, ”w”) >>> while (1) : a=filein. readline() if len(a)==0 : break # len(a)==0 at end of file try: b=[int(i) for i in a. split()] # Turns '1 2 3 4 5' into [1, 2, 3, 4, 5] except: # If we can't convert a line, just write it out directly fileout. write(a) continue b[0]-=b[2]*increase/2 b[1]-=b[3]*increase/2 b[2]=int(b[2]*(1. 0+increase)) b[3]=int(b[3]*(1. 0+increase)) fileout. write(“%dt%dt%dn”%(b[0], b[1], b[2], b[3], b[4]))
The Scrabble Problem You have 7 random letters. What real words can you make from them ? How many words could we make ? 7*6*5*4*3*2*1 + 7*6*5*4*3*2 +. . . = 7! + 7!/1! + 7!/2! + 7!/3! + 7!/4! + 7!/5! + 7!/6! = 13699
The Scrabble Problem You have 7 random letters. What real words can you make from them ? How many words could we make ? 7*6*5*4*3*2*1 + 7*6*5*4*3*2 +. . . = 7! + 7!/1! + 7!/2! + 7!/3! + 7!/4! + 7!/5! + 7!/6! = 13699 Different approaches: - Make all possible words, check to see if each is in the dictionary - Linear - Recursive - Check each word in the dictionary to see if it can be made from the letters in the list
Homework 1) You have a file on disk called 'file. txt', containing text like: Discovery did a back flip Thursday so the international space station's crew could photograph its belly for any signs of damage. It was only the second time a space shuttle has performed the unusual maneuver before docking with the orbiting complex. (the text could be anything) Write a program to read file. txt can convert all occurances of the word 'it' to 'IT', and write the results to a new file. Yes, you could do it with a text editor easily. You are writing the program that the author of the text editor had to write. Please email the homework to me before dinner on Sunday
- Slides: 9