Showing posts with label chapter 23 exercises. Show all posts
Showing posts with label chapter 23 exercises. Show all posts

Monday, 30 May 2022

Chapter 23 // Exercises 15 , 16- Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 15

Describe a pattern that cannot be expressed as a regular expression.

HTML.

Chapter 23 // Exercise 16

For experts only: Prove that the pattern found in the previous exercise really isn't a regular expression.

Sunday, 29 May 2022

Chapter 23 // Exercise 14 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 14

Write a program that, like the one in section 23.8.7, can be used to experiment with pattern matching by typing in a pattern. However, have it read a file into memory (representing a line break with the new line character, '\n'), so that you can experiment with patterns spanning line breaks. Test it and document a dozen test patterns.


So I was confused with this one. I'm not sure if he meant, "create a new file with newlines replaced by \n" or "have a file with newlines in it". In the first case, the newline will need to be put as '\\n' in the regex pattern otherwise it will just match to the characters \ n, so the slash needs escaping. But in the second you can just have it as \n and it will continue searching the next line for the pattern.

With this in mind I decided to do both. Interestingly I discovered that if you type out a regex pattern at compile time, it will add the escape to the slash. Whereas if you type it in the console at runtime, it won't.

Saturday, 28 May 2022

Chapter 23 // Exercise 13 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 13

Does dot (.) match '\n'? Write a program to find out.


Using regex101.com, it shouldn't match. (.) and '\n' are completely seperate. I used regex_replace to also show how they don't match. 

A part of me though feels like this is trick question. Maybe it's not, it's one of those seemingly simple things designed to catch you out....or maybe I'm just overthinking.

Friday, 27 May 2022

Chapter 23 // Exercise 12 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 12

Write a program, based on the program that finds lines containing dates (exercise 6), that finds all dates and reformats them to the ISO yyyy-mm-dd format. The program should take an input file and produce and output file that is identical to the input file except for the changed date formatting.


Had a look for a replace type regex and found the standard libraries regex_replace()
This will replace all matches it finds on the same line in one go. Amazing.

I then went off down a tangent using substring to replace certain parts of the string, then attempted to convert the string to time_t to convert the time_t to a particular format. I then bashed my head against the wall and simply pushed the 3 dates into a vector then concatenated them into whatever format I wanted.

This exercise took me longer than expected as C# regex can simply replace one date format with another in 1 line....if only.

To save some time I decided not to do ALL dates and changed only the mm/dd/yyyy and dd/mm/yyyy formats.

Thursday, 26 May 2022

Chapter 23 // Exercise 11 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 11

Modify the table-checking program from section 23.9 to see if the number of students is increasing or decreasing over the years in question.


I used the iterators in the map created from the previous exercise to easily check the totals over the years and do a quick print out on whether it was increasing/decreasing.

Wednesday, 25 May 2022

Chapter 23 // Exercise 10 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 10

Modify the table-checking program from section 23.9 to write a new table where the rows with the same initial digit (indication the year: first grades start with 1) are merged.


I honestly was really puzzled by this one. I guess he meant do exactly the same but while checking the lines create a new file where years are merged? So instead of seperate lines for 1A and 1B they get merged into 1? I hope that's what he meant because that's what I did.

I tried to keep things relatively simple. I originally thought of pushing back the table into a vector then erasing lines to create the table but then remembered this chapter was also about maps, not just regex. Maps can store a unique key which was perfect for checking to see if the current line had already been pushed back with the class sizes.

Tuesday, 24 May 2022

Chapter 23 // Exercise 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 9

Using eof(), it is possible to determine which line of a table is the last. Use that to (try to) simplify the table-checking program from section 23.9. Be sure to test your program with files that end with empty lines after the table and with files that don't end with a newline at all.


from_string() also has to be created for the code to work. This is found on page 853 but I added it to my running version of std_lib_facilities.

This was an annoying one to read as it's hard to determine how many tab/spaces there are in the printed book. I also had to type up the table as he doesn't provide it and made sure it matched the patterns he gave.

I also misunderstood the question and thought that he was suggesting that you could use eof() to determine newlines?? My brain goes to strange places. Anyway, I added a simple check that looks for new lines anywhere to skip them.




Monday, 23 May 2022

Chapter 23 // Exercise 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 8

Modify the program from section 23.8.7 so that it takes as inputs a pattern and a file name. Its output should be the numbered lines (line-number: line) that contain a match of the pattern. If no matches are found, no output should be produced.


This didn't require much changing from the code given; just adding in the lines to get a filename and open it.

Sunday, 22 May 2022

Chapter 23 // Exercise 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 7

Write a program (similar to the one in the previous exercise) that finds credit card numbers in a file. Do a bit of research to find out what credit card formats are really used.


So I thought credit card numbers were straight forward; literally just 16 digits separated by a space (at least that's what it is on my credit card). I never realised they were all different, I've only ever had a mastercard.

I found this site:
and decided to write patterns for the 3 most common; visa, mastercard and amex. This site was also incredibly useful:

I got a load of test credit card numbers from this site and added some more to it:

Whilst doing this I saw more posts about how regex should not be used for data validation. I even found sites like paypal providing test credit card numbers and they had all spaces stripped out.

I did check for any whitespace separating characters after the max length of the number though because, well they should be separated in a text file anyway.

Saturday, 21 May 2022

Chapter 23 // Exercise 6 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 6

Write a program that finds dates in a text file. Write out each line containing at least one date in the format line-number: line. Start with a regular expression for a simple format, e.g., 12/24/2000, and test the program with that. Then, add more formats.


First I copied the text from this page: 
https://help.scribesoft.com/scribe/en/sol/general/datetime.htm into a text file for testing and used https://regex101.com/ to help me create the right patterns.

After that I ended up with 5 different patterns to test various date formats including ISO. I didn't add validation to the patterns as I feel validation should be done once you have the strings, regex is just for searching.

Sunday, 10 April 2022

Chapter 23 // Exercise 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 5

Find a large email message file (thousands of messages) and then time it as written with a multimap and with multimap replaced by an unordered_multimap. Note that our application does not take advantage of the ordering of the multimap.


This one was interesting. I used my random email generator to create a text file with 5555 emails in it.

Then created 2 functions and timed them as he shows on page 1015.

The multimap took 12.591 seconds.
The unordered multimap took 12.48 seconds. 

Saturday, 9 April 2022

Chapter 23 // Exercise 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 4

Find a real email message file (containing real email messages) and modify the email example to extract subject lines from sender names taken as input from the user.


I converted an email from my own account to text (hotmail) and it was very basic with just
From: Name <email>
Sent: DD Month YYYY HH:MM
To: Name <email>
Subject: Subject Message

Message

When a message is forwarded it's exactly the same but Fw: is added after Subject:

So, seeing as how the next exercise needs a text file with thousands of messages I decided to quickly create a "random email generator": 

You can use this to create a text file containing as many email messages as you want for testing.

I then went about changing everything to use wide strings instead so most currency symbols would work. I briefly tried to make emojis work but couldn't be bothered going down that rabbit hole.




Thursday, 7 April 2022

Chapter 23 // Exercise 3 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 3

Modify the email example from section 23.4 to use regular expressions to find the subject and sender.


So for this I used the site Regex101 to help craft the regex I needed:

This site is great as it gives explanations of what each part of the regex is doing and you can step through matches in the debugger.

For the subject, I assumed that we are finding it only in English and based off the email examples in the section. I got a bit annoyed as apparently std::regex doesn't support the very handy lookbehind feature. So I ended up just searching for "Subject:" then creating a string with everything in the message line after those 9 characters.

I then did the same for the sender.


Friday, 18 March 2022

Chapter 23 // Exercise 2 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 2

Add a multimap and have it hold subjects. Let the program take an input string from the keyboard and print out every message with that string as its subject.


2 months after starting this chapter I had almost forgotten everything and this took me longer than expected.

The hard part had already been provided by Bjarne though so it was just a case of creating a new multimap that stores messages and subjects and loops through that instead if there is a match.

Thursday, 20 January 2022

Chapter 23 // Exercise 1 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 1

Get the email file example to run; test it using a large file of your own creation. Be sure to include messages that are likely to trigger errors, such as messages with two address lines, several messages with the same address and/or same subject, and empty messages. Also test the program with something that simply isn't a message according to that program's specification, such as a large file contain no ---- lines.


I used the given website from the book to create a text file containing all the email examples:

I had to change the pointer to a reference in find_from_addr() to get the iterators to work with the for each loop but other than that, the code is exactly what's given in the book and it works.

I tested it with 2 files, the first contains lots of different emails from the website as well as messages that aren't emails (but still separated by ----) and it worked fine, finding all 7 emails sent from John Doe. 

The second text file has all the ---- lines removed and that found no emails from John Doe as it needs the ---- to tell it when to push back a message.