Skip to content

Instantly share code, notes, and snippets.

@alenbasic
alenbasic / mhp.py
Created April 19, 2014 08:26
The Monty Hall Problem
import random
right = 0.0
wrong = 0.0
x = 100000
for i in range(x):
items = ['goat','prize', 'goat']
random.shuffle(items)
choice = random.randrange(0,3)
choice_name = items[choice]
@alenbasic
alenbasic / grades.py
Created May 28, 2013 12:27
This is a simple script to figure out what your grade is, or where you're currently sitting at if you haven't got all your assessments back.
fin_tot = 0
fin_worth = 0
def style_text(one,two):
print()
print('#'*50)
print(one)
print(two)
print('#'*50)
print()
@alenbasic
alenbasic / petals.py
Created May 22, 2013 13:26
A CLI implementation of the game Petals Around the Rose.
from random import randrange
a = "+-------+ "
b = ["| | ","|* | ","|* *| "]
c = ["| | ","| * | ","|* *| "]
d = ["| | ","| *| ","|* *| "]
while True:
count = 0
@alenbasic
alenbasic / tabs2spaces.py
Created May 22, 2013 02:31
When helping a friend with his code I kept having issues modifying it because of tabs/spaces (my text editors convert tabs to spaces while his doesn't) so I created this script so that I could quickly convert his tabs to spaces.
import re; from sys import argv
# start the program like this:
# python tabs2spaces.py file_name_to_change.extension
file_name = argv[1]
read = open(file_name,'r+').read()
update = re.sub("[\t]", ' ', read)
save = open(file_name, 'w')
save.write(update)
@alenbasic
alenbasic / janken.py
Created May 18, 2013 11:35
Ultimate Janken: Inspired by http://9gag.com/gag/aEvLGNn
import random
while True:
print()
print('#'*50)
print("ULTIMATE JANKEN!")
print("1: Rock 8: Paper ")
@alenbasic
alenbasic / tictactoe.py
Last active December 16, 2015 10:59
A game of tic tac toe with in built AI. With a little tweak you could randomize the AI's strategy so the game won't always end in a draw.
import random
# the board we print out
board = ['[0]','[1]','[2]','[3]','[4]','[5]','[6]','[7]','[8]']
# the list of winning combinations
win_list = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
X = '\033[92m' + ' X ' + '\033[0m' # makes the x green in *nix systems
O = '\033[91m' + ' O ' + '\033[0m' # makes the o red in *nix systems
@alenbasic
alenbasic / hangman.py
Created February 9, 2013 01:06
A hangman game based on the words file in *nix systems.
import random
file = open("/usr/share/dict/words").read().splitlines()
dict = []
for word in file:
dict.append(word)
guesses = []
incorrect = 10
word = (dict[random.randrange(0,len(dict)-1)]).lower()
blank = list('*' * len(word))
@alenbasic
alenbasic / arab_roman.py
Created February 9, 2013 01:01
Arabic/Roman numerals converter
i = 0
count = 0
str = ''
roman_list = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
roman_dict = {1000:'M',900:'CM',500:'D',400:'CD',100:'C',90:'XC',50:'L',40:'XL',10:'X',9:'IX',5:'V',4:'IV',1:'I'}
arabic_dict = {'M':1000, 'CM':900, 'D':500, 'CD':400, 'C':100, 'XC':90, 'L':50, 'XL':40, 'X':10, 'IX':9, 'V':5, 'IV':4, 'I':1}
while True:
print '1. Arabic to Roman'
print '2. Roman to Arabic'
@alenbasic
alenbasic / palin_list.py
Created February 9, 2013 00:57
List of palindromes based on the words file in *nix systems.
a = []
b = []
i = 0
z = 0
for line in open('/usr/share/dict/words', 'r').readlines():
while i < len(line)-2:
a.append(line[i])
i = i + 1
while i > 0:
b.append(line[i])