#๐ why does this return 1 even though 4-4 = 0
31 messages ยท Page 1 of 1 (latest)
@reef condor
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
You expect valid to not accrue anything for 4 4 because both tests are false.
But the numbers string is two lines: a blank line and a 4 4 line. So the first, empty line has a empty list for nogaps. And all() of an empty sequence is True:
>>> all(())
True
>>> numbers = '''
... 4 4
... '''
>>> numbers.splitlines()
['', '4 4']
So either test would test as True for this blank line. Since you're using an elif, you only test the first one and add 1 for that test.
So you add 1 for the first blank line and 0 for the second 4 4 line. Thus totalling 1.
@reef condor
Ok ty
so its coz of the newline that counts as a true
Effectively. It's the blank lines and thus the empty list of numbers.
wait then why isnt it 2
isnt
"""
4 4
"""
one extra line from
"""
4 4"""
!e
print(repr("""
4 4
"""))
print(repr("""
4 4"""))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | '\n4 4\n'
002 | '\n4 4'
'\n 4 4\n '
'\n 4 4'```
it is
!e
'''
4 4
'''.splitlines()
:warning: Your 3.12 eval job has completed with return code 0.
[No output]
['', '4 4']```
:warning: Your 3.12 eval job has completed with return code 0.
[No output]
'\n4 4\n'```
print ๐
The short answer: to .splitlines(), the last line may or may not include a newline.
i see
!e
Whereas this is different:
print('''
4 4
'''.split('\n'))
Generic error (code: 1): invalid syntax (<string>, line 2)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
['', '4 4', '']
Miss stating AOC from title
||It looks perfectly similar if not exactly same to AOC 2024 D2P1||
Definition of end of file in posix says that all files should end in newline character and then EOF.
So that empty line at the end is seen as not a full line.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.