Skip to content

Conversation

stevendes
Copy link
Owner

@stevendes stevendes commented Dec 10, 2019

Completed and ready for review!

I also have some questions in the following regards:

  • First

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

  • Second

50 pattern = re.compile(',|;')

I'm also not sure how this works

@stevendes stevendes requested a review from glpuga December 10, 2019 20:48
Copy link
Collaborator

@glpuga glpuga left a 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)
Copy link
Collaborator

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 ;.

Copy link
Collaborator

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"]

Copy link
Owner Author

@stevendes stevendes Dec 11, 2019

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(,) ?

Copy link
Collaborator

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.

Copy link
Owner Author

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))
Copy link
Collaborator

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).

Copy link
Owner Author

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?

Copy link
Collaborator

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'.

Copy link
Collaborator

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)
Copy link
Collaborator

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

See this and this.

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.

Copy link
Collaborator

@glpuga glpuga Dec 11, 2019

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?

Copy link
Owner Author

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.

Copy link
Collaborator

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

Copy link
Collaborator

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?

Copy link
Owner Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants