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

challenge107_easy

challenge108_easy

challenge109_easy

challenge110_easy

challenge111_easy2

challenge112_easy

challenge113_easy

challenge114_easy

challenge115_easy

challenge116_easy

challenge117_easy

challenge118_easy

challenge119_easy

challenge121_easy

challenge122_easy

challenge125_easy

challenge126_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

challenge153_easy

'''
You may have seen Pascal's Triangle before. It has been known about
for a long time now and is a very simple concept - it makes several
appearances in mathematics, one of which is when you calculate
the binomial expansion.
If you've not seen it before, you can calculate it by first putting
 1 on the outermost numbers:

    1
   1 1
  1 _ 1
 1 _ _ 1
1 _ _ _ 1

And then each number within the triangle can be calculated by adding
the two numbers above it, to form this:

     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1

It has several interesting properties, however what we're interested
in is the 3-dimensional version of this triangle - Pascal's Pyramid.
It works in much the same way - the corner numbers are all 1s.
However the edges are all layers of Pascal's triangle, and each inner
number is the sum of the 3 numbers above it. Besides that there is
nothing else to it.

Here are the first 5 cross-sectional 'layers', top to bottom:

1

 1
1 1

  1
 2 2
1 2 1

   1
  3 3
 3 6 3
1 3 3 1

      1
    4  4
   6  12 6
 4  12 12 4
1  4  6  4  1

See how the outermost 'rows' or 'edges' of numbers on all of the above are
layers of Pascal's Triangle, as we saw above. Therefore, The faces of
Pascal's Pyramid, were it a 3D object, would have Pascal's Triangle on them!

Your challenge is, given a number N, to calculate and display the
Nth layer of Pascal's Pyramid.
Formal Inputs and Outputs
Input Description

On the console, you will be given a number N where N > 0.
Output Description

You must print out layer N of Pascal's Pyramid. The triangle cross-section
must be presented so the point is at the top. Each row shall be separated
by newlines, and each number shall be separated by spaces.
Spacing is not important but your submission would be even cooler if it
were displayed properly. For example, for the 3rd layer, a valid output
would be as so:

1
2 2
1 2 1

Or, better:

  1
 2 2
1 2 1

Or even:

   1
     2   2
1   2 1

But why you'd do the latter is beyond me.
Sample Inputs & Outputs
Sample Input

6

Sample Output

1
5 5
10 20 10
10 30 30 10
5 20 30 20 5
1 5 10 10 5 1

Challenge
Challenge Input

14

Notes

There are ways to quickly do this that use the Factorial function.
Also, look at the pattern the 'rows' make in relation to the leftmost
and rightmost number and Pascal's triangle.
Reading material on Pascal's Pyramid can be found here.

Jagged multidimensional arrays will come in handy here.

I'm still trying to gauge relative challenge difficulty,
so please excuse me and let me know if this is too challenging
for an Easy rating.
'''

# ones on the corners, the outside have Pascal's Triangle on them and the inners are a sum of the three numbers above it.


def pasctetrow(n, r):
    """
    return a row of Pascal's Tetrahedron at level n
    """
    row = []
    for c in range(0, r+1):
        row.append(pasctet(n, r, c))
    return row


def pasctet(n, r, c):  # thanks to David Chako

    """
    lookup entry (r = row, c = column)
    in level n of Pascal's Tetrahedron
    """
    i,j,k = getijk(n, r, c)
    return fact(n)/(fact(i)*fact(j)*fact(k))


def getijk(n, r, c):
    i = n-r; j = r-c; k = n-i-j
    return [i, j, k]


def fact(n):
    """
    factorial of n (recursive)
    """
    if n <= 1:
        return (1)
    else:
        return n * fact(n-1)


def pasctetlev(n):
    """print level n of Pascal's Tetrahedron"""
    for r in range(n+1):
        row = pasctetrow(n, r)
        for x in range(0, len(row)):
            if x == len(row) - 1:
                print(int(row[x]))
            else:
                print(int(row[x]), end='' + ' ')


if __name__ == '__main__':
    pasctetlev(14)

# adapted from 4D solutions by Kirby Urner

Result

1
14 14
91 182 91
364 1092 1092 364
1001 4004 6006 4004 1001
2002 10010 20020 20020 10010 2002
3003 18018 45045 60060 45045 18018 3003
3432 24024 72072 120120 120120 72072 24024 3432
3003 24024 84084 168168 210210 168168 84084 24024 3003
2002 18018 72072 168168 252252 252252 168168 72072 18018 2002
1001 10010 45045 120120 210210 252252 210210 120120 45045 10010 1001
364 4004 20020 60060 120120 168168 168168 120120 60060 20020 4004 364
91 1092 6006 20020 45045 72072 84084 72072 45045 20020 6006 1092 91
14 182 1092 4004 10010 18018 24024 24024 18018 10010 4004 1092 182 14
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1