Data Science Tools & Software - Assignment No. 1


I. Regular Expressions (MCQs)

  1. Which module in Python supports regular expressions?

    1. re
    2. regex
    3. pyregex
    4. None of the mentioned
  2. Which of the following creates a pattern object?

    1. re.create(str)
    2. re.regex(str)
    3. re.compile(str)
    4. re.assemble(str)
  3. What does the function re.match do?

    1. Matches a pattern at the start of the string
    2. Matches a pattern at any position in the string
    3. Such a function does not exist
    4. None of the mentioned
  4. What does the function re.search do?

    1. Matches a pattern at the start of the string
    2. Matches a pattern at any position in the string
    3. Such a function does not exist
    4. None of the mentioned
  5. What will be the output of the following Python code?

    sentence = 'we are humans'
    matched = re.match(r'( .* ) ( .*? ) ( .* )', sentence)
    print(matched.groups())
    
    1. ('we', 'are', 'humans')
    2. (we, are, humans)
    3. ('we', 'humans')
    4. 'we are humans'
  6. What will be the output of the following Python code?

    sentence = 'we are humans'
    matched = re.match(r'( .* ) ( .*? ) ( .* )', sentence)
    print(matched.group())
    
    1. ('we', 'are', 'humans')
    2. (we, are, humans)
    3. ('we', 'humans')
    4. 'we are humans'
  7. What will be the output of the following Python code?

    sentence = 'we are humans'
    matched = re.match(r'( .* ) ( .*? ) ( .* )', sentence)
    print(matched.group(2))
    
    1. 'are'
    2. 'we'
    3. 'humans'
    4. 'we are humans'
  8. What will be the output of the following Python code?

    sentence = 'horses are fast'
    regex = re.compile('(?P<animal>\\w+) (?P<verb>\\w+) (?P<adjective>\\w+)')
    matched = re.search(regex, sentence)
    print(matched.groupdict())
    
    1. {'animal': 'horses', 'verb': 'are', 'adjective': 'fast'}
    2. ('horses', 'are', 'fast')
    3. 'horses are fast'
    4. 'are'
  9. What will be the output of the following Python code?

    sentence = 'horses are fast'
    regex = re.compile('(?P<animal>\\w+) (?P<verb>\\w+) (?P<adjective>\\w+)')
    matched = re.search(regex, sentence)
    print(matched.groups())
    
    1. {'animal': 'horses', 'verb': 'are', 'adjective': 'fast'}
    2. ('horses', 'are', 'fast')
    3. 'horses are fast'
    4. 'are'
  10. What will be the output of the following Python code?

    sentence = 'horses are fast'
    regex = re.compile('(?P<animal>\\w+) (?P<verb>\\w+) (?P<adjective>\\w+)')
    matched = re.search(regex, sentence)
    print(matched.group(2))
    
    1. {'animal': 'horses', 'verb': 'are', 'adjective': 'fast'}
    2. ('horses', 'are', 'fast')
    3. 'horses are fast'
    4. 'are'
  11. Which of the following is not an HTTP method that can change some state on the server?

    1. response = requests.put( ... )
    2. response = requests.post( ... )
    3. response = requests.delete( ... )
    4. response = requests.copy( ... )
  12. Which module in Python supports XML?

    1. BeautifulSoup
    2. numpy
    3. pyxmlex
    4. xmlrequest

II. Writing Python Script (MCQs)

  1. Data Frame Merging

    Given the dictionaries:

    Car_Price = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'],
                 'Price': [23845, 17995, 135925, 71400]}
    car_Horsepower = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'],
                      'horsepower': [141, 80, 182, 160]}
    

    Which of the following approaches correctly creates two data frames, merges them on the "Company" column, and appends the second data frame as a new column so that the final (expected) output is:

    index Company Price horsepower
    0 Toyota 23845 141
    1 Honda 17995 80
    2 BMV 135925 182
    3 Audi 71400 160
    1. Use pd.DataFrame() on each dictionary and merge using pd.merge(df1, df2, on="Company")
    2. Create data frames and append using pd.concat([df1, df2], axis=1)
    3. Convert dictionaries to Series and join via df1.join(df2.set_index('Company'), on='Company')
    4. Both (a) and (c) are correct methods
  2. HTTP Request Execution

    Which snippet correctly executes an HTTP GET request to:

    https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=9&cad=rja&uact=8?

     ```python
     import requests
     response = requests.get("<https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=9&cad=rja&uact=8>")
     ```
    
    1. All of the above work correctly
  3. CSV and XML Representation