2

I need to do this:

"""
Program a function

    def increasing (m)

that will be able, for any matrix m of positive integers, to check whether the sums of the rows in this array are increasing.

examples
1 3 2 5        The sums of the rows        2 8 4 1        The sums of the rows
7 9 4 1        are 11, 21 and 23.          6 2 8 5        are 15, 21 and 15.
3 5 6 9        --> increasing              8 4 2 1        --> not increasing
"""

So, I want to use sum(), which is perfectly doable, I guess.

I started like this:

def increasing (m):
    for row in m:
        row[1]

But I know that row[1] will just output the numbers in index 1 of each row. What I have in my mind is this:

def increasing (m):
    for row in m:
        if sum(row)[first_row] > sum(row)[second_row]:
           return False

But that is just slicing it, so I don't know how to count the rows so that I can compare them.

I don't want to use any module or whatsoever, just plain simple Python. Can someone point me in the right direction? I just need it to be as simple as possible.

Input format sample:

increasing_l = [
    [1, 3, 2, 5],
    [7, 9, 4, 1],
    [3, 5, 6, 9]
]

not_increasing_l = [
    [2, 8, 4, 1],
    [6, 2, 8, 5],
    [8, 4, 2, 1]
]

test1 = increasing(increasing_l)
test2 = increasing(not_increasing_l)

print "should be True: %s" % test1
print "should be False: %s" % test2
2
  • 1
    what is your input format like? Commented Dec 16, 2017 at 15:26
  • 1
    Added that @HamedTemsah Commented Dec 16, 2017 at 15:27

6 Answers 6

10

You can do the following:

def increasing(m):
    return all(sum(r1) < sum(r2) for r1, r2 in zip(m, m[1:]))

This uses zip to pair adjacent rows and all to efficiently do the pairwise sum comparison.

Without zip:

return all(sum(m[i-1]) < sum(m[i]) for i in range(1, len(m)))
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thank you. What if I wouldn't want to use zip? What is the alternative?
This does do more sums than necessary
@JoeIddon True, but I'll keep it or it will lose some of its conciseness and intuitive readability.
@schwobaseggl Yeah, it's definitely a very clever answer as it stands :)
7

Assuming that you have a function "sum" which returns the sum of given row. You can use a temp variable to keep the sum of your current row and use it for verification. For exmaple:

def increasing (m):
    prevRow = 0
    currentRow = 0
    for row in m:
        currentRow = sum(row)
        if (currentRow <= prevRow):
           return False
        prevRow= currentRow
    else:
        return True

2 Comments

Ah, even though it's a bit longer than other answers, I like the clear and concise way of doing it. Thank you very much!
Glad to have helped, just wanted to clarify that schwobaseggl's answer is more 'Pythonic' whilst mine is more vanilla. FYI.
3

It is enough to store the latest sum in order to get an answer:

def increasing(m):
    last_sum = -float('inf')
    for row_sum in map(sum, m):
        if row_sum < last_sum:
            return False
        last_sum = row_sum

    return True

3 Comments

What does - float('inf') even do? I don't get it.
@Siyah, the expression is equivalent to "minus infinity". Here, it is used to avoid special treatment to the first row.
Interesting. Never heard of that before. Thanks @Elisha!
2

You could first create a list of the sums of the rows, and then check whether that list is increasing.

def increasing(m):
    sums = [sum(r) for r in m]
    return all(sums[i] < sums[i+1] for i in range(len(m)-1))

and we can test this with:

m1  = [[1, 3, 2, 5],
       [7, 9, 4, 1],
       [3, 5, 6, 9]]

m2  = [[2, 8, 4, 1],
       [6, 2, 8, 5],
       [8, 4, 2, 1]]

which produces the correct results:

>>> increasing(m1)
True
>>> increasing(m2)
False

Comments

1

just simply do

row[0]+row[1]+row[2]+row[3]

for summation process and the problem of not knowing line numbers is handled by iterating on rows, which you will not have any problem

1 Comment

I don't get how that works. When I do that, it just shows me the first numbers in every row, which is not what I want.
0

from sklearn import preprocessing

import csv, sys

with open("m.txt") as f:

reader = csv.reader(f)
next(reader) # skip header
data = [r for r in reader]
data.pop(0)
print(type(data))
a=np.asarray(data)
print(np.nanvar(a,ddof=1))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.