Ene Uran 638 Posting Virtuoso

The word game Scrabble has a total of 100 letter tiles. I listed a dictionary of the letters in the game showing the letter and a tuple containng the frequency of the letter and its value.

# scrabble letter (frequency, value) total 100 tiles
# from http://thepixiepit.co.uk/scrabble/rules.html
scrabbleD = {
'A': (9, 1),
'B': (2, 3),
'C': (2, 3),
'D': (4, 2),
'E': (12, 1),
'F': (2, 4),
'G': (3, 2),
'H': (2, 4),
'I': (9, 1),
'J': (1, 8),
'K': (1, 5),
'L': (4, 1),
'M': (2, 3),
'N': (6, 1),
'O': (8, 1),
'P': (2, 3),
'Q': (1, 10),
'R': (6, 1),
'S': (4, 1),
'T': (6, 1),
'U': (4, 1),
'V': (2, 4),
'W': (2, 4),
'X': (1, 8),
'Y': (2, 4),
'Z': (1, 10),
'blank': (2, 0)
}

# test it ...
print "%6s %6s %6s" % ("letter", "freq", "val")
print "-"*24
for letter, (freq, val) in scrabbleD.iteritems():
    print "%6s %6s %6s" % (letter, freq, val)

You can use this information to write a Python program to play Fastscrabble, a variation of the game that can be played without a board. The rules are simple:

All 100 tiles are laid on the table face down.

Each player takes a turn to expose one tile.

If any player can make a word from any of the exposed tiles, he/she says "scrabble", sets the tiles of the word aside, and counts the values to his/her score.

Players who yell "scrabble" and …

Ene Uran 638 Posting Virtuoso

If you want to create a unique filename in a given folder use the Python module tempfile:

import tempfile

# creates a random file (text=True is textfile, text=False is binary file)
ext = '.txt'
pfx = 'tmp'
dir = 'C:\\Temp'
filename = tempfile.mkstemp(suffix=ext, prefix=pfx, dir=dir, text=True)[1]
print filename  # eg. C:\Temp\tmpsnrfgk.txt

# test it ...
fout = open(filename, 'w')
fout.write("Just a text file")
fout.close()
Ene Uran 638 Posting Virtuoso

This short code finds all files in a given drive or folder/directory that end with a selected file extension. It also checks all the subdirectories:

import os
 
# pick a folder or drive
folder = 'C:\\'
# pick a file extension
extension = ".css"
 
print "All files in %s ending with %s :" % (folder, extension)
file_list = []
for (paths, dirs, files) in os.walk(folder):
    for file in files:
        if file.endswith(extension):
            # show progress
            print '.',
            file_list.append(os.path.join(paths, file))
 
print
 
for full_filename in file_list:
    print full_filename
Ene Uran 638 Posting Virtuoso

Write a program to show all the files in a given drive that have been modified in the last 24 hours.

Ene Uran 638 Posting Virtuoso

Display the letters in a text sorted by their frequency of occurance:

import string
 
# create a list of lower case letters
# ['a', 'b', 'c', 'd', 'e', ... ]
alpha_list = list(string.lowercase)
 
text = """The Spell Checker Poem ...
 
Eye halve a spelling chequer
It came with my pea sea
It plainly marques four my revue
Miss steaks eye kin knot sea.
 
Eye strike a key and type a word
And weight four it two say
Weather eye am wrong oar write
It shows me strait a weigh.
As soon as a mist ache is maid
It nose bee fore two long
And eye can put the error rite
Its rare lea ever wrong.
 
Eye have run this poem threw it
I am shore your pleased two no
its letter perfect awl the weigh
My chequer tolled me sew.
"""
 
# convert text to all lower case letters
text = text.lower()
 
# create a list of (frequency, letter) tuples
tuple_list = []
for letter in alpha_list:
    tuple_list.append((text.count(letter), letter))
 
# sort by frequency (high value first)
tuple_list.sort(reverse=True)
#print tuple_list
 
# show all letters with frequency above 0
for freq, letter in tuple_list:
    if freq > 0:
        print letter, freq
 
"""
result =
e 67
t 33
a 32
r 29
s 26
...
 
"""

Moderator's note: php tags didn't work properly, replaced them.

Ene Uran 638 Posting Virtuoso

In the USA yo have the following currency options:
100 Dollars
50 Dollars
20 Dollars
10 Dollars
5 Dollars
1 Dollar ( the 2 Dollar bill is very rare, ignore it)
25 cents ( the 50 cent coin is rare, also ignore it)
10 cents
5 cents
1 cent

Let's say a customer has a charge of $161.13 and pays with two 100 Dollar bills. Write a Python program to figure out how to pay the customer his change with the least amount of currency items.

Ene Uran 638 Posting Virtuoso

Write a program that gives a list of 100 unique random integers in the range of 0 to 999.

Ene Uran 638 Posting Virtuoso

You can use a global variable within a function by prefixing it with the word "global". Here is an example:

# keep track of how many times a function has been accessed
# using a global variable, somewhat frowned upon
def counter1():
    global count
    # your regular code here
    count += 1
    return count

# test it
count = 0         # a global variable
print counter1()  # 1
print counter1()  # 2
print count       # 2  variable count has changed
print

The trouble with a global variable is with using it somewhere else in a large program. Everytime you call this function its value will change and may lead to buggy code.

One solution is to use the namespace of the function, making the variable more unique. It gives you a hint where it is used, for example:

# avoid the global variable and use the function namespace
def counter2():
    # your regular code here
    counter2.count += 1
    return counter2.count    

# test it
counter2.count = 0  # now count has counter2 namespace, notice the period
print counter2()    # 1
print counter2()    # 2
print
Ene Uran 638 Posting Virtuoso

How about a program to calculate the phase of the moon. I have seen a C program to do this, Python should be a lot easier, once you know the formula. Google for the formula, do a console version or embellish with some graphics if you want to. Both Tkinter and wxPython allow you to draw on a canvas.

Lardmeister commented: sweet idea +10
Ene Uran 638 Posting Virtuoso

Make a Molecular Weight calculator. The user enters for instance the formula for salt NaCl and the program looks up the atomic weight of sodium and chlorine and adds them up.

A dictionary with element:weight pairs would make it easy. You also have to design some kind of parser that figures out molecules like water H2O. Here you have to add up hydrogen twice and oxygen once.

Things get a little more challenging for the parser with toluene, since the user could enter C7H8 or C6H5CH3. Elements like silicon Si have to be entered that way and not as SI, since your parser would look at it as a combination of sulfur S and iodine I.

Since the right data is already there, you can expand the program to do elemental analysis, giving the percent of each element in the formula. Happy coding!

Ene Uran 638 Posting Virtuoso

An electronic version of an Index Card File would be interesting, like a collection of recipes for cooking. You could search quickly using a limited set of keywords/titles, or slower using the general text of each card.

If you want to be fancy, you can add a feature to cook for two people, five people or even 12.