Skip to content

Instantly share code, notes, and snippets.

View BensonMuriithi's full-sized avatar

Benson Muriithi BensonMuriithi

  • Kenya
View GitHub Profile
@BensonMuriithi
BensonMuriithi / gameboardtoscreen.py
Created August 8, 2016 10:13
My solution (tictactoeengine.py) for 'Tic Tac Toe Game' exercise on practicepython.org -> http://www.practicepython.org/exercise/2016/08/03/28-tic-tac-toe-game.html
"""Functions for printing a TicTacToe gameboard to the console
The gameboard should be represented as a 2 dimensional list of 3 rows and 3 columns
"""
element_process = lambda i: ("X" if i == 1 else ("O" if i == 2 else " "))
def printboard(board):
print "\n game = [%s]\n" % ",\n\t ".join(["[%s]" % ", ".join(map(element_process, board[i])) for i in xrange(3)])
@BensonMuriithi
BensonMuriithi / decodewebpage.py
Created August 7, 2016 07:46
My solution for 'Decode A Web Page' exercise on practicepython.org ->http://www.practicepython.org/exercise/2014/06/06/17-decode-a-web-page.html
"""Get all <article> tags on New York Times homepage"""
import requests
from bs4 import BeautifulSoup
def get_articles(address):
page = requests.get(address)
soup = BeautifulSoup(page.text, "html.parser")
return list(soup.find_all("article"))
@BensonMuriithi
BensonMuriithi / decodewebpage2.py
Last active August 7, 2016 07:51
My solution for 'Decode A Web Page Two' exercise on practicepython.org -> http://www.practicepython.org/exercise/2014/07/14/19-decode-a-web-page-two.html
"""Fetch an online article and print it to the screen"""
import requests
from bs4 import BeautifulSoup
def getportions(soup):
heading = soup.find("div", {"class":"dek"})
if heading:
yield heading.text
@BensonMuriithi
BensonMuriithi / custommax.py
Last active August 3, 2016 20:03
My solution for 'Max of Three' exercise on practicepython.org ->http://www.practicepython.org/exercise/2016/03/27/28-max-of-three.html
def whosmax(*args):
start = 0
limit = len(args)
i = 1
while i < limit:
if args[start] < args[i]: start = i
i += 1
return args[start]
@BensonMuriithi
BensonMuriithi / gameboardtoscreen.py
Last active August 13, 2016 22:47
My solution for 'Tic Tac Toe Draw' exercise on practicepython.org ->http://www.practicepython.org/exercise/2015/11/26/27-tic-tac-toe-draw.html
"""Functions for printing a TicTacToe gameboard to the console
The gameboard should be represented as a 2 dimensional list of 3 rows and 3 columns
"""
element_process = lambda i: ("X" if i == 1 else ("O" if i == 2 else " "))
def printboard(board):
print "\n game = [%s]\n" % ",\n\t ".join(["[%s]" % ", ".join(map(element_process, board[i])) for i in xrange(3)])
@BensonMuriithi
BensonMuriithi / tictactoecheck.py
Last active August 3, 2016 11:28
My solution for 'Check Tic Tac Toe' exercise on practicepython.org ->http://www.practicepython.org/exercise/2015/11/16/26-check-tic-tac-toe.html
"""Determine the winner of any of a game of tic tac toe
The results of the play woll be provided to the program as a
2 dimensional array of 3 lists which will contain numbers
indicating the mark on that spot on the board.
1 will represent play by player 1
2 represent play by player 2
0 inicates a blank cell
"""
@BensonMuriithi
BensonMuriithi / guessinggame2.py
Created August 1, 2016 10:16
My solution for Guessing Game Two exercise on practicepython.org here ->http://www.practicepython.org/exercise/2015/11/01/25-guessing-game-two.html
"""Guess the number that a user has chosen
The program will try to get the number guessed by the user
between 0 and 100 in as few guesses as possible
For each guess, the user will tell the program if the guess is too high or too low or correct
The program will also return the number of attempts it made at
establishing the number"""
@BensonMuriithi
BensonMuriithi / gameboard.py
Last active July 30, 2016 13:39 — forked from KHerb/gameboard.py
Gameboard Generator
"""Gameboard Generator"""
columns = int(raw_input("How many columns do you want on your gameboard? "))
rows = int(raw_input("How many rows do you want on your gameboard? "))
def gameboard(columns,rows):
column = []
height = []
for i in range(columns):
column.append(" |")
for i in range(columns):
@BensonMuriithi
BensonMuriithi / gameboard.py
Created July 30, 2016 13:12
My solution for drawing a user specified game board exercise on practicepython.org ->http://www.practicepython.org/exercise/2014/12/27/24-draw-a-game-board.html
"""Draw a X x Y matrix as per the specifications entered as input"""
def draw(x, y):
"""Draws a matrix of x rows and y columns"""
##The length of the 'dott' sequence can be adjusted and the rest of the
#drawing will adjust itself after reloading"""
dott = " ----- "
pipe = "|"
@BensonMuriithi
BensonMuriithi / filesoverlap.py
Last active July 29, 2016 13:07
My solution for file overlap exercise on practicepython.org ->http://www.practicepython.org/exercise/2014/12/14/23-file-overlap.html
"""From two text files, print the numbers or words that appear in both files"""
import os
def get_overlap(_file1, _file2):
with open(_file1, 'r') as f1, open(_file2, 'r') as f2:
return set(f1.read().split()).intersection(set(f2.read().split()))
if __name__ == "__main__":