Skip to content

Instantly share code, notes, and snippets.

@umair-tp
umair-tp / Hangman.py
Created February 27, 2018 13:08
Exercise 32 : This exercise is Part 3 of 3 of the Hangman exercise series. The other exercises are: Part 1 and Part 2. You can start your Python journey anywhere, but to finish this exercise you will have to have finished Parts 1 and 2 or use the solutions (Part 1 and Part 2). In this exercise, we will finish building Hangman. In the game of Han…
import random, sys
def displayHint(answers, choosed_word):
won = True
for i in range(len(choosed_word)-1):
if choosed_word[i] in answers:
print choosed_word[i],
else:
won = False
print '-',
@umair-tp
umair-tp / Pick Word.py
Created February 27, 2018 12:47
Exercise 30 : This exercise is Part 1 of 3 of the Hangman exercise series. The other exercises are: Part 2 and Part 3. In this exercise, the task is to write a function that picks a random word from a list of words from the SOWPODS dictionary. Download this file and save it in the same directory as your Python code. This file is Peter Norvig’s c…
import random
with open('sowpods.txt') as dict_file:
words_list = dict_file.read().split('\n')
print words_list[random.randint(0, len(words_list))]
@umair-tp
umair-tp / Guessing Game Two.py
Created February 27, 2018 12:37
Exercise 25 : In a previous exercise, we’ve written a program that “knows” a number and asks a user to guess it. This time, we’re going to do exactly the opposite. You, the user, will have in your head a number between 0 and 100. The program will guess a number, and you, the user, will say whether it is too high, too low, or your number. At the …
numbers = range(100)
low = 0
tries = 0
high = len(numbers) - 1
while True:
print str(low) + " : " + str(high)
tries += 1
if low == high:
print "You must have choosed : " + numbers[low]
break
@umair-tp
umair-tp / Draw A Game Board.py
Created February 27, 2018 12:14
Exercise 24 : This exercise is Part 1 of 4 of the Tic Tac Toe exercise series. The other exercises are: Part 2, Part 3, and Part 4. Time for some fake graphics! Let’s say we want to draw game boards that look like this: --- --- --- | | | | --- --- --- | | | | --- --- --- | | | | --- --- --- This one is 3x3 (like in tic tac toe). Obviously, they …
def printBoard(dimension):
for i in range(dimension):
for j in range(dimension):
print " ",
for k in range(dimension):
print "-",
print "\n",
print "|",
for j in range(dimension):
for k in range(dimension):
@umair-tp
umair-tp / File Overlap.py
Created February 27, 2018 11:09
Exercise 23 : Given two .txt files that have lists of numbers in them, find the numbers that are overlapping. One .txt file has a list of all prime numbers under 1000, and the other .txt file has a list of happy numbers up to 1000. (If you forgot, prime numbers are numbers that can’t be divided by any other number. And yes, happy numbers are a r…
with open('primenumbers.txt') as prime_file:
prime_nums = prime_file.read().split('\n')
with open('happynumbers.txt') as happy_file:
happy_nums = happy_file.read().split('\n')
for prime_num in prime_nums:
if prime_num in happy_nums:
print prime_num
@umair-tp
umair-tp / Cows And Bulls.py
Created February 27, 2018 10:30
Exercise 18 (and Solution) Create a program that will play the “cows and bulls” game with the user. The game works like this: Randomly generate a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong …
import random
def main():
randomNum = str(random.randint(1000, 9999))
tries = 0
choosedNum = '0000'
while choosedNum != randomNum:
choosedNum = raw_input("Enter your number : ")
print getCowAndBulls(choosedNum, randomNum)
tries += 1
@umair-tp
umair-tp / Decode A Web Page.py
Created February 27, 2018 09:55
Exercise 17 : Decode A Web Page NYTimes Homepage
import requests
from bs4 import BeautifulSoup
base_url = 'http://www.nytimes.com'
r = requests.get(base_url)
soup = BeautifulSoup(r.text, "html.parser")
for story_heading in soup.find_all(class_="story-heading"):
if story_heading.a:
print(story_heading.a.text.replace("\n", " ").strip())
@umair-tp
umair-tp / Reverse Word Order.py
Created February 27, 2018 09:24
Exercise 15 : Write a program (using functions!) that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order. For example, say I type the string: My name is Michele Then I would see the string: Michele is name My shown back to me.
inputString = raw_input("Enter you string : ")
inputString = inputString.split(" ")
inputString.reverse()
inputString = " ".join(inputString)
print inputString
@umair-tp
umair-tp / Fibonacci.py
Created February 27, 2018 09:09
Exercise 13 : Write a program that asks the user how many Fibonnaci numbers to generate and then generates them. Take this opportunity to think about how you can use functions. Make sure to ask the user to enter the number of numbers in the sequence to generate.(Hint: The Fibonnaci seqence is a sequence of numbers where the next number in the se…
def getNextFibonnaciElement(lastElement, currentElement):
return lastElement + currentElement
if __name__ == '__main__':
num = int(raw_input("Enter number of fibonacci series element : "))
lastElement = 0
currentElement = 1
for i in range(num):
print str(currentElement) + ","
@umair-tp
umair-tp / List Ends.py
Created February 27, 2018 09:02
Exercise 12 : Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25]) and makes a new list of only the first and last elements of the given list. For practice, write this code inside a function.
def getBoundaryElements(a):
return [a[0], a[-1]]
if __name__ == '__main__':
a = [5, 10, 15, 20, 2510, 15, 20, 2510, 15, 20, 225]
print getBoundaryElements(a)