#๐Ÿ”’ why does this return 1 even though 4-4 = 0

31 messages ยท Page 1 of 1 (latest)

reef condor
hallow oreBOT
#

@reef condor

Python help channel opened

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.

tawny flame
#

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

reef condor
#

so its coz of the newline that counts as a true

tawny flame
#

Effectively. It's the blank lines and thus the empty list of numbers.

haughty hedge
#

isnt


"""
4 4
"""

one extra line from

"""
4 4"""
#

!e


print(repr("""
4 4
"""))

print(repr("""
4 4"""))
hallow oreBOT
gloomy harness
#
'\n    4 4\n    '
'\n    4 4'```
haughty hedge
#

it is

tawny flame
#

!e

'''
4 4
'''.splitlines()
hallow oreBOT
gloomy harness
#
['', '4 4']```
tawny flame
#

!e

repr("""
4 4
""")
#

!e

repr("""
4 4
""")
hallow oreBOT
gloomy harness
#
'\n4 4\n'```
haughty hedge
tawny flame
#

The short answer: to .splitlines(), the last line may or may not include a newline.

haughty hedge
#

i see

tawny flame
#

!e
Whereas this is different:

print('''
4 4
'''.split('\n'))
gloomy harness
#

Generic error (code: 1): invalid syntax (<string>, line 2)

hallow oreBOT
final furnace
#

Miss stating AOC from title

#

||It looks perfectly similar if not exactly same to AOC 2024 D2P1||

foggy crystal
hallow oreBOT
#
Python help channel closed

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.