QuestionQuestion

Exercise 8
The (so-called) Birthday Paradox:
1 Write a function called hasduplicates that takes a list and returns True if there is any element that appears more than once. It should not modify the original list.
2 If there are 23 students in your class, what are the chances that two of you have the same birthday? You can estimate this probability by generating random samples of 23 birthdays and checking for matches. Hint: you can generate random birthdays with the randint function in the random module.

Exercise 12
Two words are a “reverse pair” if each is the reverse of the other. Write a program that finds all the reverse pairs in the word list.


Exercise 4
Write a function named uses_only that takes a word and a string of letters, and that returns True if the word contains only letters in the list. Can you make a sentence using only the letters acefhlo? Other than “Hoe alfalfa?”

Exercise 3
Write a function that takes a list of numbers and returns the cumulative sum; that is, a new list where the ith element is the sum of the first i+1 elements from the original list. For example, the cumulative sum of [1, 2, 3] is [1, 3, 6].

Exercise 7
Two words are anagrams if you can rearrange the letters from one to spell the other. Write a function called is anagram that takes two strings and returns True if they are anagrams.

Exercise 10
Two words are “rotate pairs” if you can rotate one of them and get the other (see rotate_word in Exercise 12).
Write a program that reads a wordlist and finds all the rotate pairs.

Exercise 3
Write a function called most_frequent that takes a string and prints the letters in decreasing order of frequency. Find text samples from several different languages and see how letter frequency varies between languages.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

print("--- Exercise 8 ---")

def hasDuplicates(l):
    for x in l:
       if l.count(x) > 1:

            return True
    return False

import random
total = 10000
count = 0
for i in range(total):
    l = [random.randint(1,365) for x in range(23)]
    if hasDuplicates(l):
       count += 1

print("# of total trials", total)
print("# of matches: ", count)
print("Probability: ",count/total*100, "%")


print("\n--- Exercise 12 ---")
f = open('words.txt')
words = []
import string
for word in f:
    word = word.strip()
    word = word.replace(" ","")
    words.append(word)
f.close()

from bisect import bisect_left

def bisect_search(l,w):
    i = bisect_left...

By purchasing this solution you'll be able to access the following files:
YourFirstNameLastName.py and Exercises-YourFirstNameLastName.txt.

$50.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats