Assignments V: Files & Regular Expression

Write the answers of the following questions in the available space.Part A
  1. Try to open the provided file sqroot2_10kdigits.txt. Then print the contents.
  2. Make a new empty string called sqroot_2_string. Note that there's a space between every 10 digits.
    Instead of using .rstrip() method, try using .replace(" ", "") to remove all the spaces in the file and save it in the empty string you just made. Check the length of the string as well, it should be 10002. Then print the first 10 digits followed by .... Here's an example:
    The first 10 digit of square root of 2 is 1.4142135623...
  3. Ask the user to input 4 digits using input and while loop. The loop keeps running until the user input q. Make sure the user never see the Traceback if error occurred. Here's an example output:
    Please enter four digits. Enter 'q' to quit.
    Enter four digits: 3210
    Enter four digits: 3333
    Enter four digits: 332
    Sorry, you inputted 3 digit(s).
    Enter four digits: 32AB
    Please input only numerical values.
    Enter four digits: q
    <- This quit the program.
  4. Still inside no. 3 while True block:
    • When the user input exactly 4 digits, check whether the inputted number appears in the first 10000 digits of square root of 2 and tell the user its position using .find() method.
    • Print the following:
      Digits {inputted digits} appears in the first 10000 digits of square root of 2.
      It appears starting on the {starting position}th position.
      Be careful on deciding when to use st, nd, rd, or th.
      Example:
      Enter four digits: 1421
      Digits 1421 appears in the first 10000 digits of square root of 2, starting on the 2nd position after comma.
      Enter four digits: _
    • If the digits does not appear, say:
      Sorry, digits {inputted digits} does not appear in the first 10000 digits of square root of 2.
      Enter four digits: _
  5. Make a new file called inputted_number.txt. Modify the code in no.4 so that all the valid input is saved in that files. Inside that file should look something like this:
    3210
    3222
    4771
    and so on. Use appending instead of writing, so when you run the program again, the file still saves the previously inputted numbers. Save at least 5 numbers there before submitting the code.


Part BSave the macOS.txt into a variable text. (This file came from a Wikipedia page.) Then do the following:
  1. Find all the instances of macOS, Mac OS, and OS X in the text. Put the result in one list.
    Print the list of those words then print the following:
    There are {length of list} words mentioning macOS, Mac OS, or OS X in the text.
  2. Find all the word ended with OS. This does not include OS itself or macOS. It should include the following:
    watchOS
    iOS etc.
    Print the list of those words then print the following:
    There are {length of list} words ended with OS (other than macOS) in the text.
  3. Find all the years mentioned in the text. It should include the following:
    2011
    1984 etc.
    Print the list of all those years then print the following:
    There are {length of list} number of years mentioned in the text.
  4. Delete all the references numbers in the text (including its brackets). It should delete the following:
    [8] etc.
    Before deleting them, print the list of those references then print the following:
    There are {length of list} references numbers to be deleted.
  5. Using the new text from no.4, split the text to check how many sentence the text has. Careful, do not split the period in something like the followings:
    by Apple Inc. since 2001
    OS X 10.1 etc.
    Then print the following:
    There are {length of list} sentences in the text.
Submission Deadline

Tuesday,
November 3, 2020
16:20