Skip to content

Instantly share code, notes, and snippets.

@raeq
raeq / anagram.py
Created May 14, 2018 11:49
Use list comprehension and prime products to find anagrams.
import sys
from collections import defaultdict
import urllib2
#quit if not python@2
if sys.version_info >= (3, 0):
sys.stdout.write("Sorry, requires Python 2.x, not Python 3.x\n")
sys.exit(1)
@raeq
raeq / quicksort.py
Last active May 14, 2018 14:50
A small python function to quicksort any list from smallest to largest. Now using the correct division operator "//".
def quicksort(p_list):
"""
Quicksort using comprehension and recursion on a list.
"""
if len(p_list) < 1:
return p_list
center = p_list[len(p_list) // 2]
def pront(prompt, message):
print(f"{message} {input(prompt)}!")
pront ("Please enter your name: ", "Hello,")
def pront(prompt, message):
userinput = input(prompt)
outputmessage = f"{message} {userinput}!"
print(outputmessage)
pront ("Please enter your name: ", "Hello,")
age = input("Please tell me your age: ")
age = int(age)
if age < 21:
print ("No alcohol can be served to you.")
else:
print ("Which drink may I prepare for you?")
age = input("Please tell me your age: ")
age = int(age)
if age < 21:
print ("You may not hire a vehichle.")
elif age <25:
print ("We will add a 'young driver fee' to your rental bill.")
elif age < 45:
print ("Do you require a child seat for a small daily fee?")
elif age > 55:
print (f"1 and 1: {bool(1 and 1)}")
print (f"0 and 0: {bool(0 and 0)}")
print (f"1 and 0: {bool(1 and 0)}")
print (f"not 0: {bool(not 0)}")
print (f"not 1: {bool(not 1)}")
print (f"1 or 1: {bool(1 or 1)}")
print (f"0 or 0: {bool(0 or 0)}")
print (f"1 or 0: {bool(1 or 0)}")
students = {"Emily@gmail.com", "Harish@gmail.com", "Naomi@gmail.com", "Haru@gmail.com", "Haru@gmail.com"}
for student in students:
print (student)
#Let's define a string to begin with.
sentence = "The quick brown fox jumped over the lazy dog."
#let's show the string with all characters in lower case
print(f"Lower case representation:\n{ sentence.lower() }")
#let's show the string with all characters in title case
print(f"Title case representation:\n{ sentence.title() }")
capitals = {"London", "New York", "Kuala-Lumpur", "Den Haag", "Bern", "Peking"}
for city in capitals:
if city.find(' ') > 0:
print(f"This city's name is comprised of more than one word: {city}")
elif city.find('-') > 0:
print(f"This city's name is comprised of more than one word: {city}")
else:
print(f"This city's name is just one word: {city}")