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

challenge124_easy

challenge125_easy

challenge126_easy

challenge127_easy

challenge128_easy

challenge130_easy

challenge131_east

challenge132_easy

challenge133_easy

challenge134_easy

challenge135_easy

challenge136_easy

challenge137_easy

challenge138_easy

challenge139_easy

challenge140_easy

challenge141_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

challenge188_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

challenge85_easybonus

'''
Write a program that reads a matrix of numbers separated by newlines
 and whitespace, like this:

10 5 4 20
9 33 27 16
11 6 55 3

then calculates the sums for each row and column, optionally outputting them...

Rows: 39 85 75
Columns: 30 44 86 39

then prints two new matrices:

    first, print the matrix with its rows sorted by their sums
    then, print the matrix with its columns sorted by their sums.

Like this:

10 5 4 20
11 6 55 3
9 33 27 16

10 20 5 4
9 16 33 27
11 3 6 55

Here's a large input matrix to test your program on.

5 58 88 60 11 23 97 48 59 82 95 24 6 67 47
45 14 36 99 16 70 77 18 43 39 97 54 11 53 98
85 14 96 66 34 86 95 49 4 49 72 76 45 49 37
72 88 20 56 37 16 20 97 71 11 91 33 90 5 96
15 53 54 95 61 93 75 95 51 83 71 70 2 57 83
54 29 56 80 79 93 40 55 40 14 63 94 77 12 90
96 97 3 47 2 43 12 2 82 92 1 99 90 13 35
24 19 54 96 82 96 10 40 62 30 35 16 70 83 64
59 81 8 84 14 46 32 45 41 35 98 66 87 51 49
13 49 12 51 34 82 36 77 88 14 84 41 66 18 56
6 68 82 63 77 72 67 36 85 53 66 70 21 86 80
40 51 87 5 78 56 99 44 39 48 78 56 19 55 40
5 94 62 46 85 73 24 67 95 63 42 95 43 53 4
14 99 7 36 25 65 22 71 20 80 16 10 71 97 23
99 77 85 53 13 32 37 19 61 32 45 62 25 18 32
98 79 35 17 26 96 22 3 76 20 81 9 40 95 72
18 39 55 99 96 63 90 78 77 81 2 99 68 6 84
53 27 68 43 48 29 27 14 50 29 53 65 5 56 46
94 36 17 64 2 93 5 95 47 78 90 3 85 26 32
46 62 70 63 81 6 86 51 44 96 47 83 33 28 28

For bonus points, format your output matrices nicely
(align the columns, draw boxes with - and |...)

'''
import re

matrix = '''5 58 88 60 11 23 97 48 59 82 95 24 6 67 47
45 14 36 99 16 70 77 18 43 39 97 54 11 53 98
85 14 96 66 34 86 95 49 4 49 72 76 45 49 37
72 88 20 56 37 16 20 97 71 11 91 33 90 5 96
15 53 54 95 61 93 75 95 51 83 71 70 2 57 83
54 29 56 80 79 93 40 55 40 14 63 94 77 12 90
96 97 3 47 2 43 12 2 82 92 1 99 90 13 35
24 19 54 96 82 96 10 40 62 30 35 16 70 83 64
59 81 8 84 14 46 32 45 41 35 98 66 87 51 49
13 49 12 51 34 82 36 77 88 14 84 41 66 18 56
6 68 82 63 77 72 67 36 85 53 66 70 21 86 80
40 51 87 5 78 56 99 44 39 48 78 56 19 55 40
5 94 62 46 85 73 24 67 95 63 42 95 43 53 4
14 99 7 36 25 65 22 71 20 80 16 10 71 97 23
99 77 85 53 13 32 37 19 61 32 45 62 25 18 32
98 79 35 17 26 96 22 3 76 20 81 9 40 95 72
18 39 55 99 96 63 90 78 77 81 2 99 68 6 84
53 27 68 43 48 29 27 14 50 29 53 65 5 56 46
94 36 17 64 2 93 5 95 47 78 90 3 85 26 32
46 62 70 63 81 6 86 51 44 96 47 83 33 28 28'''

rows = []
column = []
columns = []
rows_sums=[]
column_sums = []

#-- convert matrix to list of lists ------------
matrix = matrix.split('\n')
for line in matrix:
    row = re.findall('\d+', line)
    int_row = []
    for item in row:
        int_row.append(int(item))
    rows.append(int_row)


#-- ROWS ---------------------------------

#-- sum rows and add to a list ----------
for row in rows:
    sum_row = sum(row)
    rows_sums.append(sum_row)
print('Rows: ', rows_sums)

#-- copy and sort row sums ---------
sorted_row_sums = rows_sums[::-1]
sorted_row_sums.sort()

#-- sort rows ------------------
output_rows = []
for num in sorted_row_sums:
    for row in rows:
        sum_of_row = sum(row)
        if sum(row) == num:
            output_rows.append(row)
            break

#- number to string ----------------------

rows_output = []
for row in output_rows:
    for num in row:
        rows_output.append(str(num))


chunks = [rows_output[x:x + len(row)] for x in range(0, len(rows_output), len(row))]

#print(chunks)

#-- print rows -----------------------------------
print('rows ')
for lst in chunks:
    print(*lst)

#-- columns ------------------------------------------------

print()
# iterate over rows, extract columns
acc = 0
for row in rows:
    len_row = len(row)
len_rows = len(rows)
while acc < len_row:

    for x in range(0, len(rows)):
        column.append(rows[x][acc])
    columns.append(column)
    column = []
    acc += 1

#-- sum columns and add to list ----------
for column in columns:
    sum_column = sum(column)
    column_sums.append(sum_column)
print('Columns: ', column_sums)

#-- copy and sort column sums ---------
sorted_column_sums = column_sums[::-1]
sorted_column_sums.sort()

#-- sort columns ------------------
output_columns = []
for num in sorted_column_sums:
    for column in columns:
        if sum(column) == num:
            output_columns.append(column)
            break

column_output = sum(output_columns, [])

#- number to string ----------------------

col_output = []
for num in column_output:
    col_output.append(str(num))

#-- print columns ------------------------------
print()
cols = len(row)
lenl = len(col_output)
l = col_output
split = [l[i:i + int(lenl/cols)] for i in range(0, int(lenl), int(lenl / cols))]
print('columns')
for row in zip(*split):
    print("".join(str.ljust(i, 3) for i in row))

Result

Rows:  [770, 770, 857, 803, 958, 876, 714, 781, 796, 721, 932, 795, 851, 656, 690, 769, 955, 613, 767, 824]
rows 
53 27 68 43 48 29 27 14 50 29 53 65 5 56 46
14 99 7 36 25 65 22 71 20 80 16 10 71 97 23
99 77 85 53 13 32 37 19 61 32 45 62 25 18 32
96 97 3 47 2 43 12 2 82 92 1 99 90 13 35
13 49 12 51 34 82 36 77 88 14 84 41 66 18 56
94 36 17 64 2 93 5 95 47 78 90 3 85 26 32
98 79 35 17 26 96 22 3 76 20 81 9 40 95 72
5 58 88 60 11 23 97 48 59 82 95 24 6 67 47
5 58 88 60 11 23 97 48 59 82 95 24 6 67 47
24 19 54 96 82 96 10 40 62 30 35 16 70 83 64
40 51 87 5 78 56 99 44 39 48 78 56 19 55 40
59 81 8 84 14 46 32 45 41 35 98 66 87 51 49
72 88 20 56 37 16 20 97 71 11 91 33 90 5 96
46 62 70 63 81 6 86 51 44 96 47 83 33 28 28
5 94 62 46 85 73 24 67 95 63 42 95 43 53 4
85 14 96 66 34 86 95 49 4 49 72 76 45 49 37
54 29 56 80 79 93 40 55 40 14 63 94 77 12 90
6 68 82 63 77 72 67 36 85 53 66 70 21 86 80
18 39 55 99 96 63 90 78 77 81 2 99 68 6 84
15 53 54 95 61 93 75 95 51 83 71 70 2 57 83

Columns:  [941, 1134, 995, 1223, 901, 1233, 973, 1004, 1135, 1029, 1227, 1125, 954, 928, 1096]

columns
11 67 5  6  97 88 48 82 47 24 58 59 60 95 23 
16 53 45 11 77 36 18 39 98 54 14 43 99 97 70 
34 49 85 45 95 96 49 49 37 76 14 4  66 72 86 
37 5  72 90 20 20 97 11 96 33 88 71 56 91 16 
61 57 15 2  75 54 95 83 83 70 53 51 95 71 93 
79 12 54 77 40 56 55 14 90 94 29 40 80 63 93 
2  13 96 90 12 3  2  92 35 99 97 82 47 1  43 
82 83 24 70 10 54 40 30 64 16 19 62 96 35 96 
14 51 59 87 32 8  45 35 49 66 81 41 84 98 46 
34 18 13 66 36 12 77 14 56 41 49 88 51 84 82 
77 86 6  21 67 82 36 53 80 70 68 85 63 66 72 
78 55 40 19 99 87 44 48 40 56 51 39 5  78 56 
85 53 5  43 24 62 67 63 4  95 94 95 46 42 73 
25 97 14 71 22 7  71 80 23 10 99 20 36 16 65 
13 18 99 25 37 85 19 32 32 62 77 61 53 45 32 
26 95 98 40 22 35 3  20 72 9  79 76 17 81 96 
96 6  18 68 90 55 78 81 84 99 39 77 99 2  63 
48 56 53 5  27 68 14 29 46 65 27 50 43 53 29 
2  26 94 85 5  17 95 78 32 3  36 47 64 90 93 
81 28 46 33 86 70 51 96 28 83 62 44 63 47 6