<repo_name>kfleming123/edx_intro_to_comp_sci_mit<file_sep>/README.md
# edx_intro_to_comp_sci_mit
## Completed programming assignments in MIT's Introduction to Computer Science on edX.
**Assignment 1:** Various programs dealing with strings.<br>
**Assignment 2:** Calculating repayment of loans on a monthly basis given an interest rate.<br>
**Assignment 3:** Hangman.<br>
**Assignment 4:** Simplified Scrabble.<br>
**Assignment 5:** Cypher.
<file_sep>/problem_set1_question1.py
# Problem Set 1, Problem 1
"""
Prints a count of the number of vowels contained in the string s. 
Valid vowels are: 'a', 'e', 'i', 'o', and 'u
"""
def vowel_count(s):
    vowels = 0
    for i in s:
        if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
            vowels += 1
    print("Number of vowels: " + str(vowels))

vowel_count('testing')<file_sep>/problem_set2_question1.py
# Problem Set 2, Question 1
"""
Calculates the credit card balance after one year if a person only pays the 
minimum monthly payment required by the credit card company each month.

The following inputs contain values as described below:
balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal
monthlyPaymentRate - minimum monthly payment rate as a decimal

Returns remaining balance based on inputs. 
"""

def balance_one_year(balance, interest_rate, monthly_min):
    interest = interest_rate / 12
    for i in range(12):
        balance -= monthly_min*balance 
        balance += interest*balance
        answer = "Remaining balance: $" + str(round(balance, 2))
    return answer

balance_one_year(42, 0.2, 0.04)
<file_sep>/problem_set2_question3.py
# Problem Set 2, Question 2
"""
Calculates the minimum fixed monthly payment needed in order pay off a credit 
card balance within 12 months. 

The following inputs contain values as described below:

balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal

Returns the lowest monthly payment that will pay off all debt in under 1 year.
"""
def min_monthly_bisection(balance, annualInterestRate):
    interest = annualInterestRate / 12
    remainder = 1
    lower_bound = balance / 12
    upper_bound = (balance * (1 + interest)**12 / 12)
    while remainder > 0:
        guess = (lower_bound + upper_bound) / 2
        test_balance = balance
        for i in range(12):
            test_balance -= guess
            test_balance += interest*test_balance
        if round(test_balance, 2) == 0:
            break
        elif round(test_balance, 2) > 0:
            lower_bound = guess
        else:
            upper_bound = guess
    return("Lowest Payment: {:.2f}".format(guess))

min_monthly_bisection(500, 0.15)<file_sep>/problem_set2_question2.py
# Problem Set 2, Question 2
"""
Calculates the minimum fixed monthly payment needed in order pay off a credit 
card balance within 12 months. 

The following inputs contain values as described below:

balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal

Returns the lowest monthly payment (in increments of $10) that will pay off 
all debt in under 1 year.
"""

def min_monthly(balance, annualInterestRate):
    interest = annualInterestRate / 12
    guess = 10
    remainder = 1
    while remainder > 0:
        test_balance = balance
        for i in range(12):
            test_balance -= guess
            test_balance += interest*test_balance
        if test_balance <= 0:
            break
        guess +=10
    return("Lowest Payment: {:.2f}".format(guess))

min_monthly(500, 0.15)<file_sep>/problem_set1_question3.py
# Problem Set 1, Problem 3
"""
Prints the longest substring of s in which the letters occur in alphabetical order. 
For example, if s = 'azcbobobegghakl', then your program should print:
Longest substring in alphabetical order is: beggh

In the case of ties, print the first substring. 
For example, if s = 'abcbcd', then your program should print:
Longest substring in alphabetical order is: abc
"""

def alph_substring(s):
    ans_string = s[0]
    # Loop over all possible lengths of a substring
    for i in range(2, len(s) + 1):
        index_1 = 0
        # Loop over all possible substrings of size i
        for j in range(len(s) - i + 1):
            # Start with index = 0 and define substring as ss
            count = 0
            ss = s[index_1:index_1 + i]
            index_2 = 0
            for k in range(len(ss) - 1):
                if ss[index_2] > ss[index_2 + 1]:
                    count += 1
                index_2 += 1
                    # If in alphabetical order, update the answer string
            if count == 0:
                ans_string = ss
                break
            index_1 += 1
    print(ans_string)

alph_substring('azcbobobegghakl')<file_sep>/problem_set1_question2.py
# Problem Set 1, Problem 2
"""
Prints a count of the number of times the string 'bob' occurs in string s. 
"""
def bob_count(s):
    ans = 0
    for i in range(len(s) - 2):
        if s[i:i+3] == 'bob': 
            ans += 1
    print("Number of times bob occurs is: " + str(ans))

bob_count('testinbobobgbob')