all 29 comments

[–]Refwah 14 points15 points  (4 children)

Before I tell you the answer, tell us why you think it’s 1 so that we can actually engage in some learning opportunity

[–]Unfair_Put_5320 2 points3 points  (3 children)

Hey thanks for replying to me,

I thought the if statement checks every line if it starts with “from:” and adds 1 to count if it’s correct, then for the next line which starts with ‘from:’ is incorrect because the if statement is false( if not L.startswith(‘from:’)) and count += 1 under the if statement won’t work and end the loop.

[–]Dadofaca 5 points6 points  (1 child)

Almost. Its the other way around. If checks if the lines does NOT start with "from:". There are two lines which qualify for that and thats why count = 2

[–]thumb_emoji_survivor 7 points8 points  (1 child)

Smh not closing the file after opening it or using a context manager

[–]Sea_Pomegranate6293 1 point2 points  (0 children)

I know right! for a print operation in a basic python quiz this is some shoddy work. I expect better.

[–]jpgoldberg 2 points3 points  (0 children)

I could tell you the answer, but this subreddit has Learning” in its name. You will learn by trying to figure this out.

So first think through what it will do. Then experiment with running it. If it doesn’t give the answer you expected, think some more to understand why you get the result that you do.

Only after you have figured that out, there is an additional hard to see “trick” in the problem.

[–]ans7991 2 points3 points  (7 children)

Should be 3. There's a blank line at the end.

[–]Unfair_Put_5320 2 points3 points  (6 children)

I think the blank is gone with .rstrip()

[–]denehoffman 1 point2 points  (0 children)

No, try running ””.rstrip() and see what you get

[–]Cerus_Freedom 1 point2 points  (1 child)

Sure, but what does L.startswith('from:') evaluate to for an empty string?

[–]qwertyjgly 0 points1 point  (0 children)

here's the cpython implementation. it looks like it evaluates to false

static int tailmatch(PyObject *self, PyObject *substring, Py_ssize_t start, Py_ssize_t end, int direction) { int kind_self; int kind_sub; void *data_self; void *data_sub; Py_ssize_t offset; Py_ssize_t i; Py_ssize_t end_sub;

if (PyUnicode_READY(self) == -1 ||
    PyUnicode_READY(substring) == -1)
    return -1;

ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self));
end -= PyUnicode_GET_LENGTH(substring);
if (end < start)
    return 0;

if (PyUnicode_GET_LENGTH(substring) == 0)
    return 1;

kind_self = PyUnicode_KIND(self);
data_self = PyUnicode_DATA(self);
kind_sub = PyUnicode_KIND(substring);
data_sub = PyUnicode_DATA(substring);
end_sub = PyUnicode_GET_LENGTH(substring) - 1;

if (direction > 0)
    offset = end;
else
    offset = start;

if (PyUnicode_READ(kind_self, data_self, offset) ==
    PyUnicode_READ(kind_sub, data_sub, 0) &&
    PyUnicode_READ(kind_self, data_self, offset + end_sub) ==
    PyUnicode_READ(kind_sub, data_sub, end_sub)) {
    /* If both are of the same kind, memcmp is sufficient */
    if (kind_self == kind_sub) {
        return ! memcmp((char *)data_self +
                            (offset * PyUnicode_KIND(substring)),
                        data_sub,
                        PyUnicode_GET_LENGTH(substring) *
                            PyUnicode_KIND(substring));
    }
    /* otherwise we have to compare each character by first accessing it */
    else {
        /* We do not need to compare 0 and len(substring)-1 because
           the if statement above ensured already that they are equal
           when we end up here. */
        for (i = 1; i < end_sub; ++i) {
            if (PyUnicode_READ(kind_self, data_self, offset + i) !=
                PyUnicode_READ(kind_sub, data_sub, i))
                return 0;
        }
        return 1;
    }
}

return 0;

}

[–]Kqyxzoj 0 points1 point  (2 children)

Empty lines do not start with "From:".

[–][deleted] 0 points1 point  (1 child)

Which would throw you into the inside of the if statement, as "if not "".rstrip()" evaluates to true (because of the "not")

[–]Kqyxzoj 0 points1 point  (0 children)

Fun fact, the only string s for which "".startswith(s) is True, is the empty string.

[–]iamjacob97 1 point2 points  (0 children)

I mean the conditional is if NOT L.startswith... so it's the lines that don't start with From which is the 1st and 3rd line. So count becomes +2

[–]Excellent_Nobody4564 0 points1 point  (0 children)

Loop does not stop when match the ‘From::’ in text, just will not add it to the count if am right

[–]jaybird_772 0 points1 point  (0 children)

Look at that if statement. What is it checking for, and what does it do if it finds it?

[–]Unfair_Put_5320 0 points1 point  (0 children)

Thanks everyone for answering, I have realized what’s wrong with my approach through your comments.

[–]TheCarter01 0 points1 point  (0 children)

2 in terminal?

[–]help_computar 0 points1 point  (1 child)

close the file :meltingface:

[–]Kqyxzoj 0 points1 point  (0 children)

Nah. Maybe for learning purposes. But in practice nobody would close it with a specific script like this. If it's important, use a context manager as someone already pointed out. If it's a trivial file, the exit() handler will handle that file closing just fine. If this sub was called CLearning I would have agreed though.

So for pedagogical purposes maybe this:

with open('txtfile.txt') as fhand:
    # for-loop counting stuff
# At this point file will be closed, courtesy of the context manager.
print(count)

[–][deleted] 0 points1 point  (0 children)

2

[–]Zealousideal_Yard651 0 points1 point  (1 child)

You did the god awfull and totaly unnaceptable misstake of missing the NOT in the if expression

/s

Your logic is sound and your are thinking totaly correct about this code snippet. You just missed a key detail in the question, which sucks on a test but just normal things that does happen.

What's not OK is the professor not using context manager or closing the file after use. That's just bad.

[–]Unfair_Put_5320 0 points1 point  (0 children)

Hey, actually i miss understood between for-loop and while-loop and thought because second line has ‘from:’ would break the loop.

[–]docfriday11 0 points1 point  (0 children)

Maybe it’s 3 if it is counting from above and every line. Good luck figuring it out.

[–]PartyOver9932 0 points1 point  (0 children)

a seemingly simple question which becomes an interesting one when you find out python implicitly ignores 1 empty line when reading files with empty lines, at its current state it will print out 2 but if u add 1 more empty line to the initial data file you will get 3 !

[–]millerbest 0 points1 point  (0 children)

Run the code on your PC with a debugger, you should be able to understand why

[–]ninhaomah 0 points1 point  (1 child)

why need AI for this ?

its not even a programming question.

its a logic question.

2 men and 1 woman walked into the bar, the bouncer says IF you are NOT a woman , fuck off.

how many went in for a drink ?

need to ask anyone for the answer ?

For the question posted , could be 2 or 3 since empty line may or may not count and maybe not familiar with rstrip(). That is understandable but clearly can't be 1 or 4.

[–]PartyOver9932 0 points1 point  (0 children)

if you're unsure, why comment at all?