Challenges

Reddit challenges in python language.

Files Code and Result

challenge1_easy

challenge2_easy

challenge3_easy

challenge4_easy

challenge5_easy

challenge6_easy

challenge7_easy

challenge8_easy

challenge9_easy

challenge10_easy

challenge11.easy

challenge12_easy

challenge13_easy

challenge14_easy

challenge15_easy

challenge16_easy

challenge17_easy

challenge18_easy

challenge19_easy

challenge20_easy

challenge21_easy

challenge22_easy

challenge23_easy

challenge25_easy

challenge26_easy

challenge27_easy

challenge28_easy

challenge29_easy

challenge30_easy

challenge31_easy

challenge32_easy

challenge33_easy

challenge34_easy

challenge35_easy

challenge36_easy

challenge37_easy

challenge38_easy

challenge39_easy

challenge40_easy

challenge41_easy

challenge42_easy

challenge44_easy

challenge45_easy

challenge46_easy

challenge47_easy

challenge48_easy

challenge49_easy

challenge50_easy

challenge51_easy

challenge52_easy

challenge53_easy

challenge54_easy

challenge55_easy

challenge56_easy

challenge57_easy

challenge58_easy

challenge59_easy

challenge60_easy

challenge61_easy

challenge62_easy

challenge63_easy

challenge64_easy

challenge65_easy

challenge66_Dev

challenge66_easy

challenge67_easy

challenge68_easy

challenge69_easy

challenge69_easyBonus

challenge70_easy

challenge71_easy.py

challenge72_easy

challenge73_easy

challenge74_easy

challenge76_easy

challenge77_easy

challenge79_easy

challenge80_easyone

challenge82_easy

challenge83_easy

challenge84_easy

challenge85_easy

challenge85_easybonus

challenge86_easy

challenge87_easy

challenge88_easy

challenge88_easybonus

challenge89_easy

challenge91_easy

challenge92_easy

challenge93_easy

challenge94_easy

challenge95_easy

challenge96_easy

challenge97_easy

challenge98_easy

challenge99_easy

challenge100_easy

challenge100_easybonus

challenge101_easy

challenge101_easybonus

challenge102_easy

challenge103_easy

challenge104_easy

challenge105_easy

challenge106_easy

challenge130_easy

challenge143_easy

challenge146_easy

challenge148_easy

challenge149_easy

challenge153_easy

challenge154_easy

challenge158_easy

challenge158_easybonus

challenge159_easy

challenge160_easy

challenge169_easy

challenge169_easybonus

challenge172_easy

challenge174_easy

challenge175_easy

challenge177_easy

challenge180_easy

challenge181_easy

challenge185_easy

challenge192_easy

challenge193_easy

challenge197_easy

challenge198_easy

challenge199_easy

challenge201_easy

challenge202_easy

challenge203_easy

challenge204_easy

challenge204_easyBonus

challenge205_easy

challenge205_easyBonu

challenge206_easy

challenge208_easy

challenge210_easy

challenge211_easy

challenge212_easy

challenge212_easybonus

challenge213_easy

challenge214_easy

challenge215_easy

challenge216_easy

challenge217_easy

challenge218_easy

challenge218_easyBonus1

challenge218_easyBunus2

challenge219_easy

challenge220_easy.py

challenge221_easy

challenge221_easy

challenge222_easy

challenge222_easy.py

challenge223_easy

challenge226_easy

challenge226_easy

challenge228_easy.py

challenge228_easydev

challenge229_easy

challenge229_easybonus.py

challenge232_easy

challenge232_easy

challenge232_easybonus

challenge232_easybonus

challenge234_easy

challenge234_easybonus

challenge235_easy

challenge236_easy

challenge237_easy

challenge238_easy

challenge238_easybonus

challenge239_easy

challenge240_easy

challenge242_easy

challenge242_easy

challenge242_easyBonus

challenge243_easy

challenge245_easy

challenge245_easyBonus

challenge246_easy

challenge247_easy

challenge249_easy

challenge252_easy

challenge254_easy

challenge255_easy

challenge270_easy

challenge284_easy

challenge286_easy

challenge287_easy

challenge288_easy

challenge290_easy

challenge290_easybonus

challenge291_easy

challenge291_easybonus

challenge181_easy

''' Today, we'll be creating a simple calculator, that we may extend in later challenges.
Assuming you have done basic algebra, you may have seen equations in the form y=ax+b,
where a and b are constants. This forms a graph of a straight line, when you plot y in respect to x.
If you have not explored this concept yet, you can visualise a linear equation such as this using
this online tool, which will plot it for you.

The question is, how can you find out where two such 'lines' intersect when plotted - ie.
when the lines cross? Using algebra, you can solve this problem easily. For example,
given y=2x+2 and y=5x-4, how would you find out where they intersect? This situation would look like this.
Where do the red and blue lines meet? You would substitute y, forming one equation, 2x+2=5x-4,
as they both refer to the same variable y. Then, subtract one of the sides of the equation from the other side

- like 2x+2-(2x+2)=5x-4-(2x+2) which is the same as 3x-6=0 - to solve, move the -6 to the other side of
the = sign by adding 6 to both sides, and divide both sides by 3: x=2. You now have the x value of the co-ordinate
at where they meet, and as y is the same for both equations at this point (hence why they intersect) you can use
either equation to find the y value, like so. So the co-ordinate where they insersect is (2, 6). Fairly simple.

Your task is, given two such linear-style equations, find out the point at which they intersect.
Formal Inputs and Outputs
Input Description

You will be given 2 equations, in the form y=ax+b, on 2 separate lines, where a and b are constants and y and x are variables.
Output Description

You will print a point in the format (x, y), which is the point at which the two lines intersect.
Sample Inputs and Outputs
Sample Input

y=2x+2
y=5x-4

Sample Output

(2, 6)

Sample Input

y=-5x
y=-4x+1

Sample Output

(-1, 5)

Sample Input

y=0.5x+1.3
y=-1.4x-0.2

Sample Output

(-0.7895, 0.9053)

Notes

If you are new to the concept, this might be a good time to learn regular expressions.
If you're feeling more adventurous, write a little parser.

Extension
Draw a graph with 2 lines to represent the inputted equations - preferably with 2 different colours.
Draw a point or dot representing the point of intersection.
'''

# y = 2x + 2
# y = -5x - 4

import re

def make_list(tup):
    lst = []
    for x in range(len(tup)):
            lst.append(float(tup[x]))
    return lst


def extract_spaces(strng):
    compact_string = strng.replace(' ', '')
    return compact_string

def lone_x(strg):
    strng = re.findall('\=x', strg)
    if strng:
        return 1
    else:
        return 0

def missing_constant(lst):
    if len(lst) == 1:
        lst.insert(1, 0)
    return lst

if __name__ == '__main__':

    eq1 = input("Input equation in the form of 'y = 7x + 1' ")
    eq2 = input("Input equation in the form of 'y = 7x + 1' ")
    #eq1 = 'y = x -9'
    #eq2 = 'y = 4x'

    # extract spaces from equations
    print('extract spaces from equations')
    eq1 = extract_spaces(eq1)
    eq2 = extract_spaces(eq2)
    print(eq1)
    print(eq2)

    # extract numbers from eq1 and eq2
    print("extract numbers from eq1 and eq2 using regex")
    ans1 = re.findall('[-+]?\d*\.\d+|[-+]\d+|\d+', eq1)
    ans2 = re.findall('[-+]?\d*\.\d+|[-+]\d+|\d+', eq2)
    print(ans1)
    print(ans2)

    #  check for a lone x
    print(' check for a lone x, using regex, if so insert a zero')
    chk = lone_x(eq1)
    if chk == 1:
        ans1.insert(0, '0')
    print('ans1', ans1)

    chk2 = lone_x(eq2)
    if chk2 == 1:
        ans2.insert(0, '0')
    print('ans2', ans2)

    # check for missing constant, if so replace with a zero
    print('check for missing constant, if so replace with a zero')
    chk = missing_constant(ans1)
    chk2 = missing_constant(ans2)
    print(chk)
    print(chk2)

    # extract results into a list of floats
    print("extract results into a list of floats")
    sum1 = make_list(ans1)
    sum2 = make_list(ans2)
    print (sum1)
    print(sum2)

    # do the math
    print('Do the math...')
    sum3 = sum1[0] - sum2[0]
    sum4 = sum2[1] - sum1[1]
    sum5 = sum4 / sum3
    print('x = ', sum5)

    y = (sum5 * sum1[0]) +(sum1[1])
    print('y = ', y)

Result

Traceback (most recent call last):
  File "/tmp/tmponjwtkz6", line 94, in <module>
    eq1 = input("Input equation in the form of 'y = 7x + 1' ")
EOFError: EOF when reading a line