calthoff / tstp

This is an old repository for the exercises in "The Self-Taught Programmer." Please see /selftaught.
167 stars 190 forks source link

Chapter Six, Challenge 4 produces incorrect result #2

Closed jonathan-j-stone closed 7 years ago

jonathan-j-stone commented 7 years ago

The provided answer to this challenge:

lst = "Where now? Who now? When now?".split("?") print(lst)

Produces the following in the shell window:

['Where now', ' Who now', ' When now', '']

First question: Where did the question mark go when the original string was split? In the book's example, you split a string at a period, and the period remained in the output.

Second question: Why is there a double quote mark at the end of the output?

cogginproductions commented 7 years ago

I didn't even notice that before when I ran the program, so I went back and tried it again and you are right @ILeftTheLaw. I am also confused by this.

I messed around with different things and can't get the output shown in the solution. I would also like to know what happened?

jlyons210 commented 7 years ago

I had to put together a few things learned in the chapter to get the clean output including question marks.

I split the word list by space giving me a list of six strings:

words = "Where now? Who now? When now?"
word_list = words.split(" ")

output:

['Where', 'now?', 'Who', 'now?', 'When', 'now?']

...then used join and slicing to assemble a new list of 3 strings.

print([ " ".join(word_list[0:2]),
        " ".join(word_list[2:4]),
        " ".join(word_list[4:6]) ]
)

output:

['Where now?', 'Who now?', 'When now?']
jonathan-j-stone commented 7 years ago

Very creative way to solve the problem, @heyjammo . Wish I'd come up with it! :)

jlyons210 commented 7 years ago

I hope it helps!

I just saw your second question above. Maybe this example will help a bit.

The .split() method is going to split a string into fields wherever the delimiter resides in the string and will also eliminate the delimiter. If the delimiter is at the end of the string (like the "?" in your post, or "," below), there will be an empty field at the end of the list.

>>> csv = "cat,dog,horse,pig,cow"
>>> csv.split(",")
['cat', 'dog', 'horse', 'pig', 'cow']

vs.

>>> csv = "cat,dog,horse,pig,cow,"
>>> csv.split(",")
['cat', 'dog', 'horse', 'pig', 'cow', '']

Comma-separated values (or CSVs) are a common place you'll see this done. You will often have multiple fields with and without data but need to count on them being in the same position when you reference them.

>>> contact1 = "John,Doe,123 Main St.,Austin,Texas,78000,512-555-1234"
>>> contact2 = "Jane,Johnson,,,,,512-555-4321"

>>> contact1 = contact1.split(",")
>>> contact1
['John', 'Doe', '123 Main St.', 'Austin', 'Texas', '78000', '512-555-1234']

>>> contact2 = contact2.split(",")
>>> contact2
['Jane', 'Johnson', '', '', '', '', '512-555-4321']

>>> contact1[6]  # phone number is always in position 6
'512-555-1234'
>>> contact2[6]  # phone number is always in position 6
'512-555-4321'
calthoff commented 7 years ago

@heyjammo @cogginproductions Hey everyone! This is the old GitHub repo, which means you have an outdated version of the book. You can update by turning on Kindle Automatic Updates: https://www.amazon.com/gp/help/customer/display.html?nodeId=201252670. If you have any questions, feel free to email me at cory[at]theselftaughtprogrammer.io.

jlyons210 commented 7 years ago

Hi Cory, I have version 1.9 of the book. The tinyurl link on the split() example in the book takes you to the new repo, but the solutions link at the end of the ch06 exercises takes you to this project. I think that's how OP ended up here instead.

jonathan-j-stone commented 7 years ago

Kindle Automatic Updates has always been switched to the ON position for me, (I checked today as you requested in your PM, @calthoff ) but as @heyjammo said, the tinyurl link took me here and still does.

calthoff commented 7 years ago

@heyjammo @heyjammo Ahh! You are right. Thanks so much for pointing this out. I will fix it and update the book. Thanks again.