Skip to content

Instantly share code, notes, and snippets.

@rivermont
Last active June 6, 2017 18:50
Show Gist options
  • Save rivermont/cfea7233c12dc3186adeab145bfebd37 to your computer and use it in GitHub Desktop.
Save rivermont/cfea7233c12dc3186adeab145bfebd37 to your computer and use it in GitHub Desktop.
A collection of completely random things I wrote years ago.
def count_vowels(word):
''' (str) -> int
Return the number of vowels in word, always
including the letter y.
>>> count_vowels('Hello, how are you?')
7
>>> count_vowels('Hmm?')
0
'''
numVowels = 0
for char in word:
if char in('aeiouyAEIOUY'):
numVowels = numVowels + 1
return numVowels
def is_vowel(letter):
"""(str) -> bool
Return True if and only if letter is a vowel,
including y.
>>> is_vowel('a')
True
>>> is_vowel('E')
True
>>> is_vowel('c')
False
>>> is_vowel('M')
False
"""
if letter in('aeiouyAEIOUY'):
return True
else:
return False
def count_consonants(word):
"""(str) -> int
Return the number of consonants in word, always
excluding the letter y.
>>> count_consonants('Hello, how are you?')
6
>>> count_consonants('Hmm?')
3
"""
num_cons = 0
for char in word:
if char in('bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ'):
num_cons = num_cons + 1
return num_cons
def count_letters(word):
"""(str) -> int
Return the number of letters in word,
excluding all punctuation.
>>> count_letters('Hello, how are you?')
14
>>> count_letters('Hmm?')
3
"""
return (count_vowels(word)) + (count_consonants(word))
def count_numbers(word):
"""(str) -> str
Return the numbers in word.
>>> count_numbers('123abc')
'123'
>>> count_numbers('abc')
''
"""
nums = ''
for char in word:
if char in('1234567890'):
nums = nums + char
return nums
#------------------------Function Definitions-------------------------------------------------------------------
def funct1():
''' (NoneType) -> NoneType'''
print('Nothing here! Try again.')
why = input('>>> ')
if 'fun' in why:
print('Whoa, hold on there. This function isn\'t done yet!')
time.sleep(2)
print('Okay you can go now.')
else:
print('Come on, type something already! I\'m getting bored.')
def funct2():
''' (NoneType) -> NoneType'''
i = 10
while i > 0:
print(i)
i = i - 1
time.sleep(1)
print('BOOM')
def funct3():
''' (NoneType) -> NoneType'''
i = random.randint(5, 15)
while i > 0:
namer = names[(random.randint(0, 7))]
print('Hi, you\'re ' + namer + '!')
i = i - 1
def funct4():
''' (NoneType) -> NoneType'''
word = input('Please type a word or sentence. Anything! ')
for char in word:
print(char)
def funct5():
''' (NoneType) -> NoneType'''
print('blaaaaaaaaaaaaaaaaaaaaaargh')
def funct6():
''' (NoneType) -> NoneType'''
print('Hi ' + name + '! Don\'t type anything.')
time.sleep(5)
def funct7():
""" (NoneType) -> NoneType"""
print('Who created this program?')
who1 = input()
who1 = who1.lower()
if who1 in 'will bennett' or who1 in 'will benet':
print('That\'s correct!')
else:
print("Wrong.")
def funct8():
""" (NoneType) -> NoneType"""
i = random.randint(2, 10)
while i > 0:
print(_random_[random.randint(0, 17)])
i = (i - 1)
def funct9():
''' (NoneType) -> NoneType'''
_vowels = 0
_cons = 0
for char in name:
if char in vowels_:
_vowels = _vowels + 1
if char in consanants:
_cons = _cons + 1
_vowels = str(_vowels)
_cons = str(_cons)
print('Your name has ' + _vowels + ' vowel(s) and ' + _cons + ' consanant(s) in it!')
def funct10():
''' (NoneType) -> NoneType'''
_vowels_ = 0
cons_ = 0
vowels = ''
cons = ''
for char in name:
if char in _vowels:
vowels = vowels + char
if char in consanants:
cons = cons + char
cons_ = len(cons)
vowels_ = len(vowels)
if cons_ > 1 and vowels_ > 1:
print('Your name has the vowels ' + vowels + ' and the consanants ' + cons + ' in it!')
if cons_ == 1 and vowels_ > 1:
print('Your name has the vowels ' + vowels + ' and the consanant ' + cons + ' in it!')
if cons_ == 0 and vowels_ > 1:
print('Your name has the vowels ' + vowels + ' and no consanants in it!')
if cons_ == 0 and vowels_ == 0:
print('Your name has no vowels and no consanants in it!')
if cons_ == 1 and vowels_ == 0:
print('Your name has no vowels and the consanant ' + cons + ' in it!')
if cons_ == 1 and vowels_ == 1:
print('Your name has the vowel ' + vowels + ' and the consanant ' + cons + ' in it!')
if cons_ > 1 and vowels_ == 1:
print('Your name has the vowel ' + vowels + ' and the consanants ' + cons + ' in it!')
if cons_ > 1 and vowels_ == 0:
print('Your name has no vowels and the consanants ' + cons + ' in it!')
elif cons_ == 0 and vowels_ == 1:
print('Your name has the vowel ' + vowels + ' and no consanants in it!')
def funct11():
""" (NoneType) -> NoneType"""
funct8()
funct8()
funct8()
funct8()
funct8()
def funct12():
''' (NoneType) -> NoneType'''
why = input('>>> ')
time.sleep(5)
print(why)
def funct13():
""" (NoneType) -> NoneType"""
funct11()
funct11()
funct11()
funct11()
def funct14():
''' (NoneType) -> number'''
bla = input('Enter a number please. ')
nums = lettering.count_numbers(bla)
nums = int(nums)
nums = nums * 2 + 3
nums = nums + (nums - 1) + (1 * (3 + 2 / 4)) + 4
return nums
def funct15():
''' (NoneType) -> NoneType'''
print(('\n' + name + '.') * 5)
def funct16():
''' (NoneType) -> NoneType'''
print('Would you like to code something? (Yes or No)')
yn = input()
yn = yn.lower()
while not yn == '':
if yn == 'n' or yn == 'no':
break
if yn == 'y' or yn == 'ye' or yn == 'yes':
print("Well Haha, because I can't code stuff!!\nSo there.")
break
def funct17():
''' (NoneType) -> number'''
data = int(input('Please enter a number: '))
return data * 0.5
def funct18():
''' (NoneType) -> number'''
x = int(input('Please enter some numbers. '))
return ((2 * x) * x)
def funct19(length, height):
'''(number, number) -> number
Return the area of a rectangle, using the length and height of the square.
NOTICE: *Previously called rec_area, this function has been renamed*
>>> funct19(4, 4)
16
>>> funct19(10, 7)
70
>>> funct19(1.5, 2)
3
'''
return length * height
def funct20():
""" (NoneType) -> str"""
return ('BlabLblA' * 3)
def funct21():
""" (NoneType) -> NoneType"""
i = 500
while i > 0:
print('BOB IS EVIL ' * 50)
i = i - 1
def funct22(s1, s2):
'''(str, str) -> str
Return a new string containing all characters from s1 that appear at least
once in s2. The characters in the result will appear in the same order as
they appear in s1.
NOTICE: *Previously called common_chars, this function has been renamed*
>>> funct22('abc', 'ad')
'a'
>>> funct22('a', 'a')
'a'
>>> funct22('abb', 'ab')
'abb'
>>> funct22('abracadabra', 'ra')
'araaara'
'''
res = ''
for ch in s1:
if ch in s2:
res = res + ch
return res
def funct23():
''' (NoneType) -> NoneType'''
import storymaker
def funct24(d):
'''(dict of {str: int}) -> bool
Each key in d is a fruit and each value is the quantity of that fruit.
Try to eat one of each fruit: reduce by 1 all quantities greater than 0
associated with each fruit in d and return True if and only if any fruit was
eaten.
NOTICE: *Previously called eat, this function has been renamed*
>>> funct24({'apple': 2, 'banana': 3, 'pear': 3, 'peach': 1})
True
>>> funct24({'apple': 0, 'banana': 0})
False
'''
ate = False
for fruit in d:
if d[fruit] > 0:
d[fruit] = d[fruit] - 1
ate = True
return ate
def funct25(v, d):
''' (object, dict of {object: list}) -> bool
Return whether v is an element of one of the list values in d.
NOTICE: *Previously called contains, this function has been renamed*
>>> funct25('moogah', {1: [70, 'blue'], 2: [1.24, 'moogah', 90], 3.14: [80, 100]})
True
>>> funct25('moogah', {'moogah': [1.24, 'frooble', 90], 3.14: [80, 100]})
False
'''
found = False # Whether we have found v in a list in d.
for k in d:
for i in range(len(d[k])):
if d[k][i] == v:
found = True
return found
def funct26():
'''(NoneType) -> NoneType'''
print('Hi! You still don\'t know my name, do you?')
s = input()
tries = str(random.randint(5, 20))
print('Well, try to guess it! You have ' + tries + ' tries to guess it. (Hint: I\'m a boy)')
name_ = names2[random.randint(0, 5)]
guess = input()
while guess != name_:
guess = input()
if guess == name_:
print('You\'re correct!')
break
def funct27():
'''(NoneType) -> str'''
return (('\n' * 5 + '.' * 5) * 5)
def funct28(prompt):
''' (str) -> str
Use prompt to ask the user for a "yes" or "no"
answer and continue asking until the user gives
a valid response. Return the answer.
NOTICE: *Previously called get_answer, this function has been renamed*
'''
answer = input(prompt)
while not (answer == 'yes' or answer == 'no'):
answer = input(prompt)
return answer
def funct29(s):
''' (str) -> str
Return a substring of s from index 0 up to but
not including the first vowel in s.
NOTICE: *Previously called up_to_vowel, this function has been renamed*
>>> funct29('hello')
'h'
>>> funct29('there')
'th'
>>> funct29('cs')
'cs'
'''
def funct30(s1, s2):
'''(str, str) -> int
Return the index at whick s2
appears the second time in s1.
Precondition: s2 occurs at least twice in s1, and those
occurrences can overlap.
'''
return s1.find(s2, s1.find(s2) + 1)
def funct31(width, height, length):
'''(number, number, number) -> number
Return the are of a rectangular prism, using the width, height, and length.
NOTICE: *Previously called rec_prism_area, this function has been renamed*
>>> funct31(2,2,2)
8
>>> funct31(5,4,3)
60
'''
return width * height * length
def funct32(L):
''' (list) -> NoneType
Shift each item in L one position to the left
and shift the first item to the last position.
NOTICE: *Previously called shift_left, this function has been renamed*
Precondition: len(L) >= 1
'''
first_item = L[0]
for i in range(1, len(L)):
L[i - 1] = L[i]
L[-1] = first_item
return L
def funct33(s):
''' (str) -> int
Return the number of occurrences of a character and
an adjacent character being the same.
NOTICE: *Previously called count_adjacent_repeats, this function has been renamed*
>>> funct33('abccdeffggh')
3
>>> funct33('abcdefg')
0
'''
repeats = 0
for i in range(len(s) - 1):
if s[i] == s[i + 1]:
repeats = repeats + 1
return repeats
def funct34(L):
""" (list of str) -> list of str
Return a new list with adjacent pairs of string elements from L
concatenated together, starting with indices 0 and 1, 2 and 3,
and so on.
NOTICE: *Previously called compress_list, this function has been renamed*
Precondition: len(L) >= 2 and len(L) % 2 == 0
>>> funct34(['a', 'b', 'c', 'd'])
['ab', 'cd']
"""
compressed_list = []
i = 0
while i < len(L):
compressed_list.append(L[i] + L[i + 1])
i = i + 2
return compressed_list
def funct35():
'''(NoneType) -> int
NOTICE: *Previously called t, this function has been renamed*
'''
i = 523
total = 0
while i != 10509:
if i % 2 == 0:
total = total + i
i = i + 1
return total
def funct36(lst):
''' (list of number) -> number
NOTICE: *Previously called mystery, this function has been renamed*
'''
for sublist in lst:
total = 0
for num in sublist:
total = total + num
return total
def funct37(s):
""" (str) -> bool
Returns True if and only only if s is
equal to the reverse of s.
NOTICE: *Previously called mystery2, this function has been renamed*
>>> funct37('racecar')
True
>>> funct37('gone')
False
"""
matches = 0
for i in range(len(s) // 2):
if s[i] == s[len(s) - 1 - i]:
matches = matches + 1
return matches == (len(s) // 2)
def funct38(L):
''' (list) -> NoneType
Shift each item in L one position to the right
and shift the last item to the first position.
NOTICE: *Previously called shift_right, this function has been renamed*
Precondition: len(L) >= 1
'''
last_item = L[-1]
# MISSING CODE GOES HERE
for i in range(1, len(L)):
L[len(L) - i] = L[len(L) - i - 1]
L[0] = last_item
return L
def funct39(L):
""" (list) -> list
NOTICE: *Previously called merge, this function has been renamed*
"""
merged = []
for i in range(0, len(L), 3):
merged.append(L[i] + L[i + 1] + L[i + 2])
return merged
def funct40(list1, list2):
''' (list of str, list of int) -> list of [str, int] list
Return a new list in which each item is a 2-item list
with the string from the
corresponding position of list1 and the int from the
corresponding position of list2.
NOTICE: *Previously called make_pairs, this function has been renamed*
Precondition: len(list1) == len(list2)
>>> funct40(['A', 'B', 'C'], [1, 2, 3])
[['A', 1], ['B', 2], ['C', 3]]
'''
pairs = []
# CODE MISSING HERE
for i in range(len(list1)):
pairs.append([list1[i], list2[i]])
return pairs
def funct41(list1, list2):
''' (list of str, list of int) -> list of [str, int] list
Return a new list in which each item is a 2-item list
with the string from the
corresponding position of list1 and the int from the
corresponding position of list2.
NOTICE: *Previously called make_pairs2, this function has been renamed*
Precondition: len(list1) == len(list2)
>>> funct41(['A', 'B', 'C'], [1, 2, 3])
[['A', 1], ['B', 2], ['C', 3]]
'''
pairs = []
# CODE MISSING HERE
for i in range(len(list1)):
inner_list = []
inner_list.append(list1[i])
inner_list.append(list2[i])
pairs.append(inner_list)
return pairs
def funct42(s):
''' (str) -> str
NOTICE: *Previously called secret, this function has been renamed*
'''
i = 0
result = ''
while s[i].isdigit():
result = result + s[i]
i = i + 1
return result
def funct43(L):
""" (list) -> list
NOTICE: *Previously called example, this function has been renamed*
"""
i = 0
result = []
while i < len(L):
result.append(L[i])
i = i + 3
return result
def funct44(L):
""" (list of number) -> number
NOTICE: *Previously called for_version, this function has been renamed*
"""
found_even = False
total = 0
for num in L:
if num % 2 != 0 and not found_even:
total = total + num
else:
found_even = True
return total
def funct45(L):
""" (list of number) -> number
NOTICE: *Previously called while_version, this function has been renamed*
"""
i = 0
total = 0
while i < len(L) and L[i] % 2 != 0:
total = total + L[i]
i = i + 1
return total
def funct46(playlist, song):
'''(list of str, str) -> NoneType
Make sure there are no more than 3 occurrences of song in playlist.
NOTICE: *Previously called cap_song_repitition, this function has been renamed*
'''
while playlist.count(song) > 3:
playlist.pop(playlist.index(song))
return playlist
def funct47(playlist, song):
'''(list of str, str) -> NoneType
Make sure there are no more than 3 occurrences of song in playlist.
NOTICE: *Previously called cap_song_repitition2, this function has been renamed*
'''
while playlist.count(song) > 3:
playlist.remove(song)
return playlist
def funct48(L, increment):
''' (list, int) -> list
NOTICE: *Previously called increment_items, this function has been renamed*
'''
i = 0
while i < len(L):
L[i] = L[i] + increment
i = i + 1
def funct49(s):
'''(str) -> str
Return the reverse of s.
NOTICE: *Previously called reverse, this function has been renamed*
>>> fun49('abc')
'cba'
>>> fun49('a')
'a'
>>> fun49('madam')
'madam'
>>> fun49('1234!')
'!4321'
'''
result = ''
i = len(s) - 1
while i >= 0:
result = result + s[i]
i = i - 1# CODE MISSING HERE
return result
def funct50(L1, L2):
'''(list, list) -> bool
Return True if and only if L1 and L2 contain the same number of elements.
NOTICE: *Previously called same_length, this function has been renamed*
'''
if len(L1) == len(L2):
return True
else:
return False
def funct51(x):
''' (number) -> number
NOTICE: *Previously called f, this function has been renamed*'''
y = x * 3
return y - x
def funct52(L1, L2):
'''(list of int, list of int) -> int
Return the larger of the smallest value in L1 and the smallest value in
L2.
NOTICE: *Previously called laarger_of_smallest, this function has been renamed*
Precondition: L1 and L2 are not empty.
>>> 52([1, 4, 0], [3, 2])
2
>>> 52([4], [9, 6, 3])
4
'''
return max(min(L1), min(L2))
def funct53(L1, L2):
'''(list, list) -> bool
Return True if and only if L1 and L2 contain the same number of elements.
NOTICE: *Previously called same_length2, this function has been renamed*
'''
return len(L1) == len(L2)# CODE MISSING HERE
def funct54(L, d):
'''(list, dict) -> list
Return a new list containing all the items in L that are keys in d.
NOTICE: *Previously called get_keys, this function has been renamed*
>>> 54([1, 2, 'a'], {'a': 3, 1: 2, 4: 'w'})
['a', 1]
'''
result = []
for k in d:# CODE MISSING HERE
if k in L:
result.append(k)
return result
def funct55(L1, L2):
'''(list of int, list of str) -> bool
Return True if and only if all the ints in L1 are the lengths of the strings
in L2 at the corresponding positions.
NOTICE: *Previously called are_lengths_of_strs, this function has been renamed*
Precondition: len(L1) == len(L2)
>>> 55([4, 0, 2], ['abcd', '', 'ef'])
True
>>> 55([1, 2, 1], ['hello', 'bla', 123'])
False
'''
result = True
for i in range(len(L1)):
if L1[i] != len(L2[i]):# CODE MISSING HERE
result = False
return result
def funct56(collection):
''' (list) -> list
NOTICE: *Previously called double_values, this function has been renamed*'''
for v in range(len(collection)):
collection[v] = collection[v] * 2
return collection
def funct57(n):
""" (number) -> number
NOTICE: *Previously called absolute_value, this function has been renamed*"""
if n < 0:
n = -n
return n
def funct58():
''' (NoneType) -> NoneType
NOTICE: *Previously called hello, this function has been renamed*'''
print("Hello")
def funct59(width, height):
""" (number, number) -> number
NOTICE: *Previously called area, this function has been renamed*"""
return width * height
def funct60(name):
''' (str) -> NoneType
NOTICE: *Previously called print_welcome, this function has been renamed*'''
print("Welcome ", + name)
def funct61():
''' (NoneType) -> NoneType
NOTICE: *Previously called print_func, this function has been renamed*'''
a = 17
print("in print_func a = ", a)
def funct62(a_var, e_var):
''' (number, number) -> number
NOTICE: *Previously called a_func, this function has been renamed*'''
print("in a_func a_var = ", + a_var)
b_var = 100 + a_var
d_var = 2 * a_var
print("in funct62 b_var = ", + b_var)
print("in funct62 d_var = ", + d_var)
print("in funct62 e_var = ", + e_var)
return b_var + 10
def funct63():
''' (NoneType) -> NoneType
NOTICE: *Previously called print_options, this function has been renamed*'''
print("Options:")
print(" 'p' print options")
print(" 'c' convert from Celsius")
print(" 'f' convert from Fahrenheit")
print(" 'q' quit the program")
def funct64(c_temp):
''' (number) -> number
NOTICE: *Previously called celsius_to_fahrenheit, this function has been renamed*'''
return 9.0 / 5.0 * c_temp + 32
def funct65(f_temp):
''' (number) -> number
NOTICE: *Previously called fahrenheit_to_celsius, this function has been renamed*'''
return (f_temp - 32.0) * 5.0 / 9.0
def funct66(prompt):
''' (str) -> float
NOTICE: *Previously called positive_input, this function has been renamed*'''
number = float(input(prompt))
while number <= 0:
print('Must be a positive number')
number = float(input(prompt))
return number
def funct67(a, b):
''' (number, number) ->
NOTICE: *Previously called mult, this function has been renamed*'''
if b == 0:
return 0
rest = mult(a, b - 1)
value = a + rest
return value
def funct68(n):
''' (number) -> number
NOTICE: *Previously called factorial, this function has been renamed*'''
if n <= 1:
return 1
return n * funct68(n - 1)
def funct69(seconds):
''' (number) -> numbers
NOTICE: *Previously called count_down, this function has been renamed*'''
print(seconds)
time.sleep(1)
if seconds > 0:
return count_down(seconds-1)
def funct70():
''' (NoneType) -> NoneType
NOTICE: *Previously called f2, this function has been renamed*'''
print('In function funct70')
print('When does this print?')
#------------------------Import Statements----------------------------------------------------------
import time
import random
import lettering
#------------------------Introduction---------------------------------------------------------------
def intro():
''' (NoneType) -> str'''
print('Hello user, what would you like me (the program) to call you from now on? You cannot change it later.')
name = input()
print('Okay ' + name + ", in this program, you will find a collection of many functions.")
print('''All the functions in this program are called by typing
in something like 'funct17()'. There are 70 functions, so you can go
from 'funct1()' to 'funct70()'. Some of the functions have
some parameters, so look for those. Have fun!''')
return name
name = intro()
#------------------------Lists----------------------------------------------------------------------
names = ['stupid', 'an idiot', 'funny', 'crazy', 'wierd', 'awesome', 'cool', 'wacky', 'dumb']
_random_ = ['Hello ' + name + '.', '!?!?!?!?', 'Blablablablablablabla...', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '\n\n\n', 'I like tomatoes!', '*Yawn*','Whooaah... That was cool!', 'Do you even know what I\'m talking about?', 'Hey! You\'re ' + names[random.randint(0, 7)] + '!']
vowels_ = ('AEIOUaeiou')
consanants = ('bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ')
names2 = ['Bob', 'Henry', 'Will', 'Jack', 'Frank', 'Jordan']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment