Given a probability of flipping a head, number of flips, and a random seed, return how many heads were flipped.
import random
import statistics
def simulate_flips(n, heads_prob, seed):
counter = 0
random.seed(seed)
for _ in range(n):
if random.random() <= heads_prob:
counter += 1
return counter, n-counter
trials = 10
flips = 1000
heads_prob = 0.5
results = []
for iteration in range(1, trials+1):
heads = simulate_flips(flips, heads_prob, 100 + iteration)[0]
print("Trial {}: {} heads".format(iteration, heads))
results.append(heads)
print("\nAverage={:.3f}, Std Dev={:.3f}".format(statistics.mean(results), statistics.stdev(results)))
Given a probability of flipping a head, and a ranom seed, return how many flips are required before some number of tails are flipped.
import random
def flip_till_tails(max_tails, heads_prob, seed):
total_counter = 0
tail_counter = 0
random.seed(seed)
while tail_counter < max_tails:
if random.random() > heads_prob:
tail_counter += 1
total_counter +=1
return total_counter
for i in range(1, 21):
print("# tails = {}, # flips = {}".format(i, flip_till_tails(i, 0.5, 123)))
Now how about sequential tails...
import random
import statistics
def flip_till_tail_string(max_tails, heads_prob, seed):
total_counter = 0
tail_counter = 0
random.seed(seed)
while tail_counter < max_tails:
if random.random() <= heads_prob:
tail_counter = 0
else:
tail_counter += 1
total_counter += 1
return total_counter
trials = 10
heads_prob = 0.5
for sequence_len in range(1, 11):
print("== Sequence of Length {} ==".format(sequence_len))
results = []
for t in range(1, trials+1):
flips = flip_till_tail_string(sequence_len, heads_prob, sequence_len*1000 + t)
results.append(flips)
print(results)
avg = int("{:.0f}".format(statistics.mean(results)))
stdev = int("{:.0f}".format(statistics.stdev(results)))
print("Average={:,d}, Std Dev={:,d}\n".format(avg, stdev))
One way to make a palindrome from a word is to append the reverse of itself on the end. Given a list of words, return a list of the palindrome-flip of those words.
def palindrome_flip(word):
return "{}{}".format(word, word[::-1])
def palindrome_flips(list_o_words):
return [palindrome_flip(word) for word in list_o_words]
print(palindrome_flip("howdy"))
print(palindrome_flips(["aye", "bee", "kay"]))
Generate a data set of names (first name + first-name-son), age (uniform in [10, 100]), height (normal around 5'5)
import random
f_names = ['Liam', 'Noah', 'William', 'James', 'Logan', 'Emma', 'Olivia', 'Ava', 'Isabella', 'Sophia']
def make_name():
name = f_names[random.randint(0, len(f_names)-1)]
return name, "{}son".format(name)
def make_person():
height = random.gauss(65, 5)
return make_name() + (random.randint(10, 100), int(height // 12), int(height % 12))
def make_people(seed, n):
random.seed(seed)
return [make_person() for _ in range(n)]
for person in make_people(321, 10):
print(person)
Given a space-separated source, and search+replacement strings, replace all instances of search with replace
def do_replace(word, search, replace):
if word == search:
return replace
else:
return word
def search_n_replace(source, search, replace):
return " ".join([do_replace(word, search, replace) for word in source.split(" ")])
print(search_n_replace("hi there how are you today are you doing well", "are", "etes"))
Find the location of the last occurrence of a string in a space-separated string
def find_last(haystack, needle):
rwords = list(reversed(haystack.split(" ")))
rlens = [len(word) for word in rwords]
for index, word in enumerate(rwords):
if word == needle:
return -(sum(rlens[:index]) + index)
source = "well this is awkward and this is not fun"
print(source[find_last(source, "this"):])