Skip to content

Instantly share code, notes, and snippets.

@thisiswei
thisiswei / scrabble_play.py
Last active December 19, 2015 02:49
these codingbat from peter remind me CS212.
"""
In a series of exercises we will develop a program to find the highest-scoring
play on a Scrabble board. But we'll get there in small steps. In this exercise,
we will decide if it is legal to play a given word at a given start position on
a Scrabble board. To make things easier, in these first few exercises, the
board will consist of a single row, which will be represented as a list: a 1
indicates a blank square (later we will add 2, 3, etc. for double-and triple
letter scores), a '*' marks the start square at the center of the board, and a
capital letter indicates a letter already on the bjoard. Write
scrabble_legal_play(row, k, word) to return True if it is legal to play the
@thisiswei
thisiswei / Towers_of_Hanoi.py
Last active December 18, 2015 22:28
I feel I just got smarter
"""
The Towers of Hanoi is a puzzle where there is a pyramid of disks, each one
smaller than the one below it, all placed on to one of three rods. The puzzle
is to move all the disks from the left rod to the right rod, possibly using the
middle rod as necessary. You can only move one disk at a time, onto another
rod, but never moving a disk onto a disk that is smaller. Return a list of the
moves in the form[1, 'L', 'M'], which means to move disk number 1, the smallest
@thisiswei
thisiswei / improving.py
Last active December 18, 2015 19:49
practice
"""
Given k and n, return a list of all the positive integers less than n
such that the sum of the kth powers of their individual digits equals the integer itself.
For example, with k=3 and n=1000, one of the numbers we would return in the list would be 371,
because 371 = 3^3 +7^3 + 1^3 = 27 + 343 + 1. (This problem inspired by Project Euler.)
powersum(1, 10) → [1, 2, 3, 4, 5, 6, 7, 8, 9]
powersum(2, 100) → [1]
powersum(3, 1000) → [1, 153, 370, 371, 407]"""
@thisiswei
thisiswei / word.py
Last active December 18, 2015 17:39
#each letter's points
POINTS = dict(A=1, B=3, C=3, D=2, E=1, F=4, G=2, H=4, I=1, J=8, K=5, L=1, M=3, N=1, O=1, P=3, Q=10, R=1, S=1, T=1, U=1, V=4, W=4, X=8, Y=4, Z=10, _=0)
X, Y = (1, 0), (0, 1) # horizontal, vertical
class anchor(set):
" squares where word can be place, is either right next to a word, or * in the middle "
LETTERS = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
mnx, moab = anchor(list('MNX')), anchor(list('MOAB'))
ANY = anchor(LETTERS)
@thisiswei
thisiswei / Puzzle.py
Last active December 18, 2015 17:39
Logic puzzle
"""You will write code to solve the following logic puzzle:
1. The person who arrived on Wednesday bought the laptop.
2. The programmer is not Wilkes.
3. Of the programmer and the person who bought the droid,
one is Wilkes and the other is Hamming.
4. The writer is not Minsky.
5. Neither Knuth nor the person who bought the tablet is the manager.
6. Knuth arrived the day after Simon.
7. The person who arrived on Thursday is not the designer.
"""
this is final exam problem 4 from udacity CS212
simple description from me
| | | | | | | | | | | | | | | |
| G G . . . Y | | 9 10 11 12 13 14 |
| P . . B . Y | | 17 18 19 20 21 22 |
| P * * B . Y @ | 25 26 27 28 29 30 31
| P . . B . . | | 33 34 35 36 37 38 |
| O . . . A A | | 41 42 43 44 45 46 |
@thisiswei
thisiswei / natalie.py
Created June 20, 2013 04:27
5 months ago.
"""
A portmanteau word is a blend of two or more words, like 'mathelete',
which comes from 'math' and 'athelete'
rules are: a portmanteau must be composed of three non-empty pieces,
start+mid+end,
'adolescented' comes from
'adolescent','scented', with
start+mid+end='adole'+'scent'+'ed'.
@thisiswei
thisiswei / dvr_remote.py
Last active December 18, 2015 13:19
You know how the remote on your DVR makes you select the title of a movie or TV show by moving a cursor up/down/left/right through a grid of letters and then pressing the 'select' key? It takes forever and is very annoying. Imagine you had an app running on your phone that would allow you to type or speak the name, and the app would send the rig…
def dvr_remote(s, width):
x = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'
rows = [x[i:i+width] for i in range(0, 27, width)]
s = 'A' + s.replace(' ', '_')
DIC = dict((char, (i, pos))
for i in range(len(rows))
for (pos, char) in enumerate(rows[i]))
def helper(current, next):
row1, col1 = DIC[current]
fun getFinal init res =
let
fun helper total bet n =
case n+1 > size res of
true => total
| _ => if String.sub (res, n) = #"L"
then helper (total-bet) (bet*2) (n+1)
else helper (total+bet) 1 (n+1)
in
helper init 1 0
getFinal initial res = helper initial 1 0
where helper total bet n = if n > (length res) - 1 || bet > total.
then total
else if res !! n == 'L'.
then helper (total-bet) (bet*2) (n+1)
else helper (total+bet) 1 (n+1)