-2

I'm new to coding and python. I've taken an intro comp sci class but I still feel out of my depth when trying to understand most code so please forgive me if this question seems poorly placed.

I'm taking an algorithms class on Edx, which has an automatic grader. Starter code is provided for each problem and contains a section like the one below. This section is particularly difficult for me to understand.

I believe what will be fed into the function I write is a list that will look like this [1:2,4:6,7:10], but I'm not really sure.

I'm hoping someone could help me understand this code, so I can design a function around the data.

if __name__ == '__main__':
    input = sys.stdin.read()
    data = list(map(int, input.split()))
    n = data[0]
    m = data[1]
    starts = data[2:2 * n + 2:2]
    ends   = data[3:2 * n + 2:2]
    points = data[2 * n + 2:]
    #use fast_count_segments
    cnt = naive_count_segments(starts, ends, points)
    for x in cnt:
        print(x, end=' ')

Further, I don't really understand how to test this code on my own computer so that I can figure it out on my own. Any help would be much appreaciated. Thanks in advance.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

-4

The array slice notation (arr[1:2:1]) selects a portion of a sequence. The notation consists of three colon-separated expressions representing the start, stop, and optional step of the resulting sequence.

An expression like data[2:2 * n + 2:2] signifies a start index of 2, a stop index equal to 2 * n + 2, and a step equal to 2. The result will be a sequence starting from the 2nd index, stopping just before index 2 * n + 2, and proceeding in increments of the step of 2.

class slice(start, stop[, step]) Return a slice object representing the set of indices specified by range(start, stop, step). The start and step arguments default to None. Slice objects have read-only data attributes start, stop and step which merely return the argument values (or their default). They have no other explicit functionality; however they are used by Numerical Python and other third party extensions. Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] or a[start:stop, i]. See itertools.islice() for an alternate version that returns an iterator.

https://docs.python.org/3.4/library/functions.html#slice

jspcal
  • 50,847
  • 7
  • 72
  • 76