<file_sep>
def long_text_to_list():
    string = open('longtext.txt').read()
    return string.split()


def word_frequency_dict(wordList):
    wordDict = {}
    for word in wordList:
        if word in wordDict:
            wordDict[word] = wordDict[word] + 1
        else:
            wordDict[word] = 1
    return wordDict


def print_250_most_common_words(wordDict):
    sortedDict = sorted(wordDict.items(), key=lambda item: (item[1], item[0]))
    top250 = 0
    print("250 most common words and number of occurrences:")
    while (top250 <= 250):
        print(sortedDict.pop())
        top250 += 1


print_250_most_common_words(word_frequency_dict(long_text_to_list()))
<file_sep># Guessing Game
import random


def guessing_game():
    randomNumber = random.randint(1, 100)
    guessedCorrectly = False
    lastGuess = -1
    numberOfGuesses = 0
    while not guessedCorrectly:
        guess = int(input("Guess a number between 1 and 100: "))
        if lastGuess != guess:
            numberOfGuesses += 1
            lastGuess = guess
        if randomNumber == guess:
            print("You got it!  You guessed the secret number in %d guesses." % numberOfGuesses)
            guessedCorrectly = True
        elif randomNumber < guess:
            print("Too high!")
        else:
            print("Too low!")


guessing_game()
<file_sep>import calendar


def print_month_calendar():
    year = int(input("Enter a year: "))
    month = int(input("Enter a month: "))
    print(calendar.month(year, month))


print_month_calendar()<file_sep># List Functions

fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
stack = [3, 4, 5, 8, 13, 17, 35, 42]


def largestElement(listInput):
    try:
        return max(listInput, key=len)
    except:
        return max(listInput)


def reverseList(listInput):
    listInput.reverse()
    print(listInput)


def contains(element, inputList):
    if element in inputList:
        return True
    return False


def oddPositionElements(listInput):
    oddElements = []
    index = 1
    while index < len(listInput):
        oddElements.append(listInput[index])
        index += 2
    return oddElements


def totalList(listInput):
    runningTotal = []

    totalInt = 0
    totalString = ""

    for element in listInput:
        if isinstance(element, str):
            totalString += element
            runningTotal.append(totalString)
        else:
            totalInt += element
            runningTotal.append(totalInt)

    return runningTotal


print("Fruit list: " + fruits.__str__())
print("Integer list: " + stack.__str__())

print("\nThe largest fruit is %s." % largestElement(fruits))
print("The largest integer is %d." % largestElement(stack))


print("\nReversed fruit list: ")
reverseList(fruits)
print("Reversed integer list:")
reverseList(stack)


print("\nFruit list contains apple: %r" % contains('apple', fruits))
print("Fruit list contains bacon: %r" % contains('bacon', fruits))
print("Integer list contains 17: %r" % contains(17, stack))
print("Integer list contains 7: %r" % contains(7, stack))

print("\nOdd positioned fruits: " + oddPositionElements(fruits).__str__())
print("Odd positioned integers: " + oddPositionElements(stack).__str__())


print("\nRunning total of fruits: " + totalList(fruits).__str__())
print("Running total of integers: " + totalList(stack).__str__())<file_sep># Sum of Even Terms of Fibonacci Sequence < 4,000,000


def is_even(num):
    return num % 2 == 0


def get_even_terms():
    term1 = 0
    term2 = 1
    evens = []
    while term2 <= 4000000:
        sumOfTerms = term1 + term2
        if is_even(term2):
            evens.append(term2)
        term1 = term2
        term2 = sumOfTerms
    return evens


def sum_of_even_terms():
    sumOfEvens = 0
    evens = get_even_terms()
    for even in evens:
        sumOfEvens += even
    return sumOfEvens


# print(is_even(4))  # true
# print(is_even(3))  # false
# print(get_even_terms()) # [2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578]

print("The sum of the even terms of the Fibonacci sequence whose values are less than 4000000 is %d."
     % sum_of_even_terms())
<file_sep># Number of Sundays on First of Month from January 1, 1901 to December 31, 2000

from datetime import date


def number_of_sundays_on_first_of_month():
    sundays = 0
    for year in range(1901, 2001):
        for month in range(1, 13):
            if date(year, month, 1).weekday() == 6:
                sundays += 1
    return sundays


print("There were %d Sundays on the first of the month in the 20th century." % number_of_sundays_on_first_of_month())
<file_sep># Print even numbers that occur before 237 in sequence


def print_evens_before_237(inputList):
    for int in inputList:
        if int == 237:
            break;
        elif int % 2 == 0:
            print(int)


numbers = [386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 958,743, 527]

print("The even numbers in the list that occur before 237 are:")
print_evens_before_237(numbers)