Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View shamikalashawn's full-sized avatar
💭
Polishing my latest app!

Shamika La Shawn shamikalashawn

💭
Polishing my latest app!
View GitHub Profile
@shamikalashawn
shamikalashawn / Battleship
Created February 1, 2017 01:55
A simple game of Battleship
from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
@shamikalashawn
shamikalashawn / Code CracklePop
Created February 1, 2017 02:12
From the Recurse Center application, a code the prints out the first 100 natural numbers (excluding zero). If a number is divisible by 3, 'Crackle' is printed instead of the number. If it's divisible by 5, 'Pop' is printed instead. And if it is divisible by 3 and 5, then 'CracklePop' is printed.
for number in range (1, 101):
if number % 3 == 0 and number % 5 == 0:
print ('CracklePop')
elif number % 3 == 0:
print ('Crackle')
elif number % 5 == 0:
print ('Pop')
else:
print (number)
@shamikalashawn
shamikalashawn / SpyCoder
Created February 1, 2017 02:15
A string is encoded with six of the most popular letters of the English language switched when the word "encode" precedes the string. Otherwise, the string is decoded when "decode" precedes the string and the six letters are switched back to their original state.
def spy_coder(input):
encode = ''
decode = ''
result_input = input.split()
del result_input[0]
result_input = " ".join(result_input)
if 'encode' in input:
print 'encoding...'
for letter in result_input:
if letter == 't':
@shamikalashawn
shamikalashawn / Market Analyzer
Created February 1, 2017 02:17
Given a list of stock prices, this function returns the highest profit possible if bought at the lowest price and sold at the highest.
def market_analyze(price_arr):
min = price_arr[0]
max = price_arr[0]
for num in price_arr:
if num < min:
min = num
print min
for num in price_arr:
if num > max:
max = num
@shamikalashawn
shamikalashawn / Prime Finder
Created February 1, 2017 02:50
Two functions for the price of one! First a function that returns True for prime numbers. Second, a function that takes a number and prints all the primes up to and including that number.
def is_prime(number):
if number <=1:
return False
else:
for num in range(2, number):
if (number % num == 0) and (number != num):
return False
return True
def prime_finder(num):
@shamikalashawn
shamikalashawn / Class Calculator
Created February 1, 2017 03:02
Using OOP, a simple calculator is created which keeps track of previous operations.
class Calculator(object):
def __init__(self):
self.cache = {}
def add(self, x, y):
if not self.cache.has_key('add'):
self.cache.setdefault('add', [])
for previous_x, previous_y, res in self.cache['add']:
if previous_x == x and previous_y == y:
return res
@shamikalashawn
shamikalashawn / Bookstore Management
Created February 1, 2017 03:06
Managing a bookstore using procedure oriented programming. Add books, add authors, and search for books and authors!
def create_bookstore(name):
authors = []
books = []
bookstore = {'name': name, 'authors': authors, 'books' : books}
return bookstore
def add_author(bookstore, name, nationality):
ID = name[:3] + nationality
author = {'name': name, 'nationality': nationality, 'id': ID}
@shamikalashawn
shamikalashawn / Temperature Conversion
Created February 1, 2017 03:17
Give me the temperature in Celsius and I'll give it to you straight, in Fahrenheit. And vice versa.
def fahrenheit_to_Celsius():
fahrenheit = float(input('Please enter the temperature in Fahrenheit: '))
celsius = (5/9.0) * (fahrenheit - 32)
print('{:.2} degrees Fahrenheit is {:.2} degrees Celsius.'.format(str(fahrenheit), str(celsius)))
def celsius_to_Fahrenheit():
celsius = float(input('Please enter the temperature in Celsius: '))
fahrenheit = 32 + (celsius * (9/5.0))
print('{:.2} degrees Celsius is {:.2} degrees Fahrenheit.'.format(str(celsius), str(fahrenheit)))
@shamikalashawn
shamikalashawn / Longest Key
Created February 1, 2017 03:19
Give me a dictionary and I'll give you the longest key in that dictionary.
def longest_key(a_dict):
if a_dict == {}:
return None
else:
longestkey = ''
for key in a_dict:
if len(key) > len(longestkey):
longestkey = key
return longestkey
@shamikalashawn
shamikalashawn / A (very) Simple Game of Tic Tac Toe
Created February 1, 2017 03:27
The skeleton of the game is there. X and O take turns and the gameboard updates with each round.
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'low-L': ' ', 'low-M': ' ', 'low-R': ' '}
def printBoard(board):
print(board['top-L'] + '|' + board['top-M'] + '|'+ board['top-R'])
print('-+-+-')
print(board['mid-L'] + '|' + board['mid-M'] + '|'+ board['mid-R'])
print('-+-+-')
print(board['low-L'] + '|' + board['low-M'] + '|'+ board['low-R'])