Skip to content

Instantly share code, notes, and snippets.

"""Creates a histogram of the word lengths from the text file"""
import sys
def read_text():
inputF = open(sys.argv[1], 'r')
return inputF
openF = read_text()
text = openF.read()
""" Exception handling while dividing integers"""
while True:
integer = input("Provide an integer: ")
try:
print(10/int(integer))
except ValueError:
print("Your input must be an integer")
except ZeroDivisionError:
print("Your input must not be zero")
""" A program to change case using functions """
import sys
def caps_case(text):
"""Returns the 1st character capitalized"""
print(text.capitalize())
def title_case(text):
"""Returns the 1st letter of all words capitalized"""
s = input("Enter a sentence for testing: ")
if s.isupper() and s.endswith("."):
print("Perfect! Input meets both requirements")
elif s.isupper() and not s.endswith("."):
print("Input doesn't end with a period")
elif not s.isupper() and s.endswith("."):
print("Input is not all upper-case")
else:
print("Please re-enter a sentence that satisfies both the requirements")
@accessnash
accessnash / stringchanger.py
Created May 18, 2018 19:09
Every even letter in a string is upper case and odd letter is lower case
def myfunc(string):
str = ''
index =0
for l in string:
if index %2 ==0:
str = str + l.lower()
index +=1
else:
str = str + l.upper()
index +=1
#SAMPLE CODE FOR SPATIAL PATTERNS OF VARIABLES
library(ggplot2)
library(fiftystater)
bachdeg <- read.csv("C:/Users/DASA0/Desktop/Stat 524/Project/bachdeg.csv", sep=",")
bachdeg$state <- tolower(bachdeg$State)
# map_id creates the aesthetic mapping to the state name column in your data
p <- ggplot(bachdeg, aes(map_id = state)) +
# map points to the fifty_states shape data
geom_map(aes(fill = bachdeg), map = fifty_states) +
def check_prime(nums):
if num <2:
return 0
myprimelist = [2]
j = 3
while j <=num:
for i in range(j,3,2):
if j % i ==0:
j += 2
break
@accessnash
accessnash / mlist.py
Created May 19, 2018 21:38
sum of multiples of 3 and 5 below 1000
mlist = []
for x in range(1, 1000):
if x %3 ==0 and x % 5 ==0:
mlist.append(x)
elif x % 3 ==0:
mlist.append(x)
elif x % 5==0:
mlist.append(x)
print(sum(mlist))
@accessnash
accessnash / evenfibonnaci.py
Created May 19, 2018 21:59
sum of even fibonacci numbers less than 4 millions - problem from Project Euler
fiblist = []
a,b=1,2
while b < 8000001:
fiblist.append(a)
a,b = b,a+b
print(fiblist)
newlist = []
for num in fiblist:
if num % 2 ==0:
newlist.append(num)
@accessnash
accessnash / maxprime.py
Created May 19, 2018 22:57
Project Euler problem 3 - to find the largest prime factor of a given number
def max_prime_factor(n):
i = 2
pfactors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
pfactors.append(i)
if n > 1: