Monday 21 December 2015

Here...Have Some Old Python Programs

This one's gonna be a bit of a rushed entry because I just came home and my dad had taken me to White Spot and OH FUCKING LORD THESE EXAMS AND GODDAMN JETLAG IS DRIVING ME INSANE so yeah here's some Python programs I had made:

The first one is called drunken_math. Essentially the idea is that this program would function like a drunk mathematician. It's stupid, I know.

def drunken_math(num1, num2, sign, drinks, alc_cont):
    """   (int, int, str, int, float) -> str
   
    Precondition: alc_cont < 1.0
    Returns the equation according to drunken math
    >>> drunken_math(1, 1, '+', 10, 0.5)
    '*hic* 1 + 1 = 7'
    >>> drunken_math(2, 4, '-', 4, 0.6)
    '*hic* 2 - 4 = -2'
    >>> drunken_math(3, 7, '*', 7, 0.267)
    '*hic* 3 * 7 = 0'
    >>> drunken_math(30, 5, '/', 6, 0.309)
    '*hic* 30 / 5 = 1'
    """
    hiccup = round(drinks * alc_cont)
    if sign in '+-*/' and len(sign) == 1:
        if sign == '+':
            drunk_num = num1 + num2 + hiccup
        if sign == '-':
            drunk_num = num2 - num1 - (hiccup * 2)
        if sign == '*':
            drunk_num = num2 * num1 * (round(hiccup / 4))
        if sign == '/':
            drunk_num = round((num1 / num2) / (hiccup + num2))
        equation = str(num1) + ' ' + sign + ' ' + str(num2)
        return('*hic* ' + equation + ' = ' + str(drunk_num))
    else:
        return('No sir, I can\'t do it, now drive me home *hic*')

The second one is polynomials_with_six. A lot more interesting mathematically speaking and it saved my ass when I was in first year so this is cool.

def six_x(num): #6x
    return num % 6 == 0

def six_x_plus_1(num): #6x + 1
    return ((num - 1) // 6) == ((num - 1) / 6)

def six_x_squared(num): #6(x ** 2)
    return ((num // 6) ** 0.5) == ((num / 6) ** 0.5)

def six_x_squared_plus_1(num): #6(x ** 2) + 1
    return (((num - 1) // 6) ** 0.5) == (((num - 1) / 6) ** 0.5)

def six_x_squared_plus_five_x(num): #6(x ** 2) + 5x == x(6x + 5)
    i = 0
    test = i * (6 * i + 5)  
    while test < num:
        i = i + 1
        test = i * (6 * i + 5)  
    return test == num

def six_x_squared_plus_five_x_plus_1(num): #6(x ** 2) + 5x + 1 == x(6x + 5) + 1
    num = num - 1
    i = 0
    test = i * (6 * i + 5)  
    while test < num:
        i = i + 1
        test = i * (6 * i + 5)  
    return test == num

def six_x_plus_five_y_plus_1(num): #6x + 5y + 1
    num = num - 1
    i = 0
    temp = num - (5 * i)
    while temp > 0 and temp % 6 != 0:
        i = i + 1
        temp = num - (5 * i)
    if temp % 6:
        return False
    else:
        return ['y is equal to ' + str(i), 'x is equal to ' + str(temp // 6)]

Third one is this bizarre form of sigma notation I conjured up called Siretsuonian Sigma. God only knows.

'''
A polynomial is a statement with a variable and equations
such as 'x ** 2' or '3 * x + 2'
'''

def siretsuonian_sigma(l, h, eff_x, gee_a, n_input, n_in_x=None):
    ''' (int, int, polynomial, polynomial, int/str, polynomial) -> num
   
    Precondition: h > l
   
    Return a number based on Siretsuonian Sigma Notation
    '''
   
    if n_input == 'from x' and n_in_x == None:
        l_to_h = list(range(l, h+1))
        siret_sum = 0
       
        for i in range(len(l_to_h)):
            x_value = l_to_h[i]
            a_value = l_to_h[-1 - (i)]
            f_x = eff_x.replace('x', str(x_value))
            g_a = gee_a.replace('a', str(a_value))
           
            if 'x' in g_a:
                g_a = g_a.replace('x', str(x_value))
            try:
                x_and_a = eval(f_x) * eval(g_a)
            except ZeroDivisionError:
                x_and_a = 0
            siret_sum = siret_sum + x_and_a
           
        siret_sigma = siret_sum / len(l_to_h)
        return siret_sigma
   
    elif n_input == 'from x' and n_in_x != None:
        return 'That cannot be calculated'
   
    elif str(n_input).isdigit() and n_in_x == None:
        a_value = n_input
        g_a = gee_a.replace('a', str(a_value))
       
        if 'x' in g_a:
            g_a = g_a.replace('x', str(x_value))
           
        siret_sum = 0
       
        for i in range(l, h+1):
            x_value = i
           
            if x_value != n_input:
                f_x = eff_x.replace('x', str(x_value))
                try:
                    x_and_a = eval(f_x) * eval(g_a)
                except ZeroDivisionError:
                    x_and_a = 0              
                siret_sum = siret_sum + x_and_a
            else:
                try:
                    siret_sum = siret_sum + eval(g_a)
                except ZeroDivisionError:
                    siret_sum = siret_sum
               
        siret_sigma = siret_sum / ((h - l) + 1)
        return siret_sigma
   
    elif str(n_input).isdigit() and n_in_x != None:
        siret_sum = 0
        for i in range(l, h+1):
            x_value = i
            n_x = n_in_x.replace('x', str(x_value))
            try:
                a_value = round(eval(n_x))
            except ZeroDivisionError:
                a_value = n_input
               
            g_a = gee_a.replace('a', str(a_value))
           
            if 'x' in g_a:
                g_a = g_a.replace('x', str(x_value))          
           
            if x_value != n_input:
                f_x = eff_x.replace('x', str(x_value))
                try:
                    x_and_a = eval(f_x) * eval(g_a)
                except ZeroDivisionError:
                    x_and_a = 0
                siret_sum = siret_sum + x_and_a
               
            else:
                try:
                    siret_sum = siret_sum + eval(g_a)
                except ZeroDivisionError:
                    siret_sum = siret_sum                
                   
        siret_sigma = siret_sum / ((h - l) + 1)
        return siret_sigma

The fourth one involves balls. That's all I can say.

import random

def shuffle_cups(cups):
   
    shuffle_cups = []
    for i in range(len(cups)):
        new_cup = random.choice(cups)
        shuffle_cups.append(new_cup)
        cups.remove(new_cup)
   
    return shuffle_cups

def get_three_red_balls():
   
    red_ball_counter = 0
    attempts = 0
    start = input('What level of difficulty do you want? ')
    levels = ['1', '2', '3', '4']
   
    while start not in levels:
        start = input('What level of difficulty do you want? ')
       
    if start in levels:
        if start == '1':
            cups = ['red ball', 'red ball', 'white ball']
        elif start == '2':
            cups = ['red ball', 'yellow ball', 'blue ball']
        elif start == '3':
            cups = ['red ball', 'yellow ball', 'blue ball', 'green ball']
        elif start == '4':
            cups = ['red ball', 'yellow ball', 'blue ball', 'green ball', 'white ball']
   
 
    while red_ball_counter < 3:
        copy_cups = tuple(cups)
        new_cups = shuffle_cups(cups)
        print('There are ' + str(len(new_cups)) + ' cups')
        choose = int(input('Where is the red ball? '))
       
        while choose not in range(1, len(new_cups) + 1):
            print('There is no cup that exists')
            choose = int(input('Where is the red ball? '))
       
        choice = choose - 1
       
        if new_cups[choice] == 'red ball' and red_ball_counter == 2:
            red_ball_counter = red_ball_counter + 1
            attempts = attempts + 1
            cups = list(copy_cups)      
       
        elif new_cups[choice] == 'red ball' and red_ball_counter < 2:
            red_ball_counter = red_ball_counter + 1
            print('You got one! Only ' + str(3 - red_ball_counter) + ' left')
            attempts = attempts + 1
            cups = list(copy_cups)
           
        if new_cups[choice] != 'red ball':
            print('Sorry, you did not get a red ball.')
            print('You got instead: ' + str(new_cups[choice]))
            attempts = attempts + 1
            cups = list(copy_cups)
   
    if red_ball_counter == 3:
        print('You got 3 of them! Attempts made: ' + str(attempts))
        try_again = input('Again? ')
        if try_again in ['Yes', 'yes', 'y', 'Y']:
            get_three_red_balls()
        elif try_again in ['Fuck you', 'FUCK YOU']:
            print("Okay asshole, now you've done it")
            cups = ['white ball'] * 100 + ['red ball']
            new_cups = shuffle_cups(cups)
            print("There's 101 cups")
            print("Only one has a red ball")
            print("If you think you're so poppin' fresh, which one has it?")
            guess = int(input('Well? '))
           
            while guess not in range(1, 102):
                guess = int(input("Is this too hard for you, sunshine? Give me an answer! "))
            if new_cups[guess - 1] == 'red ball':
                print('Fuck, you win...')
            else:
                print("Don't you be dissing me again, boy.")
        else:
            print('Okay...ttyl')

Fifth is a program called obnoxious_salesman. It would probably work better if it wasn't solely text-based.

def obnoxious_salesman():
    first_line = input("Hey, can I interest you in this product? ")
    while first_line != 'No':
        first_line = input("Hey, did you hear me? ")
    if first_line == 'No':
        second_line = input("Come on, it's absolutely wonderful! ")
        counter = 0
    while second_line != 'Go away' and counter % 2 == 0:
        second_line = input("You know you want it! ")
        counter = counter + 1
    while second_line != 'Go away' and counter % 2 == 1:
        second_line = input("I know you want it! ")
        counter = counter + 1
    if second_line == 'Go away':
        third_line = input("Please sir, I got a family to feed. ")
    while third_line != "I don't believe you":
        if third_line == "Really?":
            third_line = input("Yes, I do. " )
        if third_line == "How many kids?":
            third_line = input("Uh...3, I think... " )
        else:
            third_line = input("Come on, I'm desperate! ")
    if third_line == "I don't believe you":
        fourth_line = input("No, really! I do! ")
        flb1 = "What's one of your sons' name?" #flb stands for fourth line breaker
        flb2 = "What's your daughter's name?"
        flb3 = "How many boys and girls?"
        flb4 = ['Liar', 'Liar!', 'You liar!']
    while fourth_line not in flb4:
        if fourth_line == flb1:
            fourth_line = input("Sandra...I MEAN SAM! ")
        if fourth_line == flb2:
            fourth_line = input("Sandra. ")
        if fourth_line == flb3:
            fourth_line = input("Two boys and one girl. ")
    if fourth_line in flb4:
        fifth_line = input('PLEASE, JUST BUY ONE OF THEM! ')
        flbl = ['No way', 'No dice', 'Nuh-uh', 'Not a chance'] #fifth line breaker list
    while fifth_line not in flbl:
        fifth_line = input('I WILL NOT STOP UNTIL YOU TAKE ONE OF THESE! ')
    if fifth_line in flbl:
        print('You win...jerk.')

Sixth one is really useful if you ever want to put text inside of a box. Yeah.

def in_a_box(text):
    """ (str) -> print
   
    Return text in a box
   
    >>> in_a_box('thirty days\nwool')
    ---------------
    | thirty days |
    |    wool       |
    ---------------
    """
    #obtain lines
    lines = []
    line = ''
    for ch in text:
        if ch != '\n':
            line = line + ch
        else:
            lines.append(line)
            line = ''
   
    #get last line the for loop overlooks (if it is not empty)
    if line.strip() != '':
        lines.append(line)
       
    #obtain length of longest line, which we dub hard_line
    lengths = []
    for item in lines:
        length = len(item)
        lengths.append(length)
   
    hard_line = max(lengths)
    tops = '-' * 4 + '-' * hard_line
   
    #building the box
    print(tops)
   
    for i in range(len(lines)):
        if len(lines[i]) == hard_line:
            print("| " + lines[i] + " |")
        elif len(lines[i]) != hard_line and len(lines[i]) % 2 == 0:
            if hard_line % 2 == 0:
                num_space = (hard_line // 2) - (len(lines[i]) // 2)
                print("| " + ' ' * num_space + lines[i] + ' ' * num_space + " |")
            elif hard_line % 2 == 1:
                num_space = (hard_line // 2) - (len(lines[i]) // 2)
                print("| " + ' ' * num_space + lines[i] + ' ' * (num_space + 1) + " |")
        elif len(lines[i]) != hard_line and len(lines[i]) % 2 == 1:
            if hard_line % 2 == 0:
                num_space = hard_line // 2
                print("|" + ' ' * num_space + lines[i] + ' ' * (num_space + 1)  + "|")
            elif hard_line % 2 == 1:
                num_space = (hard_line // 2) - (len(lines[i]) // 2)
                print("|" + ' ' * (num_space + 1) + lines[i] + ' ' * (num_space + 1) + "|")
               
    print(tops)

And finally, the greatest one of them all, the best program that I, nay, anyone has ever coded ever before in their lives:

def guess():
    x = input('Give me a number between 1 and 10: ')
    if int(x) > 10:
        print('Too high')
        y = input()
        if y == 'U fukkin know it':
            print('B^)')

I know this is a cop-out but I did put a lot of effort into these back in the day. Especially the last one. Please don't judge me. I've had a long day.

No comments:

Post a Comment