-
Notifications
You must be signed in to change notification settings - Fork 0
Completed the aobut_string_manipulation koan #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: devel
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job. Two questions:
words = pattern.split(string) | ||
|
||
self.assertEqual([__, __, __, __], words) | ||
self.assertEqual(["the", "rain", "in", "spain"], words) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
50 pattern = re.compile(',|;')
I'm also not sure how this works
This is an example of a Regular Expression (regexp
, for short). Notice that the re
module is imported first. See this for an introduction. You'll see more of this later, don't worry.
In short, split
splits the string into sub-strings separated by instances of pattern
, whereas the pattern ,|;
matches whenever you find a ,
OR a ;
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Say I have a string with a human readable list of ingredients: "flour, tomato souse, 2 eggs, cheese, ham, and olives"
. Find a regex pattern that will split that string into a list of ingredients for a machine, such as [ "flour", "tomato souse", "2 eggs", "cheese", "ham", "olives"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could that be pattern=re.compile(,)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nop, there are spaces and and 'and" too! You'll need a pattern that matches those too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think my best option is pattern=re.compile(,|and)
, because including the space separates the 2
and the eggs
def test_strings_can_be_joined(self): | ||
words = ["Now", "is", "the", "time"] | ||
self.assertEqual(__, ' '.join(words)) | ||
self.assertEqual("Now is the time", ' '.join(words)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice the difference between " ".join(words)
and "".join(words)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string at the start is the separator, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right. If you execute 'SEP'.join(words) you'll get 'NowSEPisSEPtheSEPtime'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with any other string.
string = "The square root of 5 is {0:.{1}f}".format(math.sqrt(5), \ | ||
decimal_places) | ||
self.assertEqual(__, string) | ||
self.assertEqual("The square root of 5 is 2.2361", string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
27 string = "The square root of 5 is {0:.{1}f}".format(math.sqrt(5), \decimal_places)
I'm not sure about what the {0:.{1}f exactly does
In short {0:.{1}f}
is saying: get the argument {0}
, but add formatting to it, interpreting as a floating point value (the f
) and such that the precision (number of digits after the decimal point) is whatever the value of the {1}
argument says (in this case, 4).
I hope that helps. Play with it a little, it's very similar to C-language string formatting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given:
days = dict()
days["Mercury"] = 87.96
days["Venus"] = 224.68
days["Earth"] = 365.26
days["Mars"] = 686.98
Printing this with:
for key,value in days.items():
print("{0} {1}".format(key, value))
Looks ugly for a user interface (try). Give me a code snippet that prints this in tidy columns, using 10 characters for the planet name field width, and 20 for the period revolution days, with 3 for the precision. It should look like this:
Mercury 87.960
Earth 365.260
Venus 224.680
Mars 686.980
PS: Notice the rows are not in the same order as I added them to the dict...why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think is ordering by the key with a higher value, but I'm not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think is ordering by the key with a higher value, but I'm not sure.
Think again https://github.com/stevendes/python_koans/pull/6/files#diff-7ff6c7eb24ff198b3e2fab6c93748dd2R36
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the code I asked above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it's because the dictionary doesn't really have an order?
Completed and ready for review!
I also have some questions in the following regards:
27 string = "The square root of 5 is {0:.{1}f}".format(math.sqrt(5), \decimal_places)
I'm not sure about what the
{0:.{1}f
exactly does50 pattern = re.compile(',|;')
I'm also not sure how this works