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

challenge86_easy

challenge94_easy

challenge104_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

challenge206_easy

'''
A recurrence relation is a mathematical construct for defining a series of numbers.
It works by first giving an initial term, and then recursively defining the rest
of the series as functions of the first one. For example, let's say we have a series
of numbers called u, which is defined by this recurrence relation:

u[0] = 1
u[n+1] = 2 * u[n]

The first relation tells us that u(0), the first term in the series, is 1.
The second relation says that, given the n-th term u(n), the next term (u(n+1))
is the previous term multiplied by two. So, to get the second term in the series,
you multiply the first term by two, to get 2. To get the third term in the series,
you multiply the second term by two, to get 4.

Recurrence relations get their name in part due to their recursive nature, as
successive terms are essentially defined as recursive application of a function,
like this Python example:

def recurrence(n):
    return n * 2

first_term = 1
second_term = recurrence(first_term)
third_term = recurrence(recurrence(first_term))
fourth_term = recurrence(third_term) # or recurrence(recurrence(recurrence(first_term)))

Or, with the help of another function to apply the recurrence function for us:

def get_nth_term(recurrence_relation, first_term, n):
    if n == 0:
        return first_term
    else:
        return get_nth_term(recurrence_relation, recurrence_relation(first_term), n - 1)

sixteenth_term = get_nth_term(recurrence, first_term, 16) #65536

You get the idea. Today you will be writing a program to compute the n-th
term of a given series defined by a recurrence relation.
Formal Inputs and Outputs
Input Description

You will first accept a line of input like this one:

*3 +2 *2

This string means that, to get the next term in the series, you multiply by three,
add two, then multiply by two. The operation that takes place is the first character
of every space-separated part of the line, and the possible characters are + for add,
- for subtract, * for multiply, and / for divide; the number given can be any real number.
Next, you will accept the starting value, like so:

0

Finally, you will accept the term number to print to (where the first term in
the series is term 0):

7

(this input can be viewed on Wolfram|Alpha.)
Output Description

You are to print all terms in the series, up to the given term number, like so:

Term 0: 0
Term 1: 4
Term 2: 28
Term 3: 172
Term 4: 1036
Term 5: 6220
Term 6: 37324
Term 7: 223948

Sample Inputs and Outputs
Series 1

This series starts with 1, and for each successive member of the series,
will multiply the last one by two and add one.
Input

*2 +1
1
10

Output

Term 0: 1
Term 1: 3
Term 2: 7
Term 3: 15
Term 4: 31
Term 5: 63
Term 6: 127
Term 7: 255
Term 8: 511
Term 9: 1023
Term 10: 2047

Series 2

This one is a bit different. This just multiplies each successive term by -2.
Notice how the series oscillates between positive and negative numbers?
Input

*-2
1
8

Output

Term 0: 1
Term 1: -2
Term 2: 4
Term 3: -8
Term 4: 16
Term 5: -32
Term 6: 64
Term 7: -128
Term 8: 256

Series 3
Input

+2 *3 -5
0
10

Output

Term 0: 0
Term 1: 1
Term 2: 4
Term 3: 13
Term 4: 40
Term 5: 121
Term 6: 364
Term 7: 1093
Term 8: 3280
Term 9: 9841
Term 10: 29524

'''

import re

def regex_operators(lst):
    opers = re.findall('(\W)-\d|(\W)', lst)
    opers = extract(opers)
    return opers


def regex_operands(lst):
    num = re.findall('[-]\d|\d+', lst)
    return num


def extract(lst):
    strng = ''
    for x in range(0, len(lst)):
        for y in range(0, 2):
            temp = lst[x][y]
            if not temp.strip() == '':
                strng += temp
    return strng


def evall(ops):
    global total
    opers = regex_operators(ops)
    num = regex_operands(ops)
    for op in range(0, len(opers)):
        if opers[op] == "+":
            total += int(num[op])
        elif opers[op] == "-":
            total -= int(num[op])
        elif opers[op] == "*":
            total *= int(num[op])
        elif opers[op] == "/":
            total = op / int(num[op])
        elif opers[op] == "^":
            total **= int(num[op])

    return total


def difference(ops, iter):
    global total
    acc = 0
    print()
    print('{0}{1}{2}{3}'.format('Term ', acc, ': ', total))

    while True:
        if iter == 0:
            return
        else:
            acc += 1
            print('{0}{1}{2}{3}'.format('Term ', acc, ': ',evall(ops)))
            iter -= 1



if __name__ == '__main__':
    '''
        ops = input("Pass a list of operators: ")
        total = int(input("And a starting number: "))
        iter = int(input("And a number of iterations: "))
        '''
    opslst = ['*-2', '*3 +2 *2', '*2 +1', '+2 *3 -5']
    iterlst = [8, 7, 10, 10]
    totallst = [1, 0, 1, 0]

    for x in range(0, 4):
        total = totallst[x]
        iter = iterlst[x]
        ops = opslst[x]

        difference(ops, iter)

Result

Term 0: 1
Term 1: -2
Term 2: 4
Term 3: -8
Term 4: 16
Term 5: -32
Term 6: 64
Term 7: -128
Term 8: 256

Term 0: 0
Term 1: 4
Term 2: 28
Term 3: 172
Term 4: 1036
Term 5: 6220
Term 6: 37324
Term 7: 223948

Term 0: 1
Term 1: 3
Term 2: 7
Term 3: 15
Term 4: 31
Term 5: 63
Term 6: 127
Term 7: 255
Term 8: 511
Term 9: 1023
Term 10: 2047

Term 0: 0
Term 1: 6
Term 2: 24
Term 3: 78
Term 4: 240
Term 5: 726
Term 6: 2184
Term 7: 6558
Term 8: 19680
Term 9: 59046
Term 10: 177144