This commit is contained in:
Yavuz Sava 2024-12-30 19:08:34 +03:00
parent c44e7fcd3c
commit 04283eec4a

View File

@ -561,12 +561,117 @@
"----"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sabrina Green,+1-802-867-5309,System Administrator\n",
"Eli Jones,+1-684-3481127,IT specialist\n",
"Melody Daniels,+1-846-687-7436,Programmer\n",
"Charlie Rivera,+1-698-746-3357,Web Developer\n"
]
}
],
"source": [
"# Question 1 X -> V\n",
"# Youre working with a CSV file that contains employee information. Each record has a name field, followed by a phone number field, and a role field. The phone number field contains U.S. phone numbers and needs to be modified to the international format, with +1- in front of the phone number. The rest of the phone number should not change. Fill in the regular expression, using groups, to use the transform_record() function to do that.\n",
"\n",
"import re\n",
"def transform_record(record):\n",
" new_record = re.sub(r'(\\w+ \\w+),(\\d{3}-\\d{7}|\\d{3}-\\d{3}-\\d{4}),(\\w+\\s*\\w+)', r'\\1,+1-\\2,\\3', record)\n",
" return new_record\n",
"\n",
"print(transform_record(\"Sabrina Green,802-867-5309,System Administrator\")) # Sabrina Green,+1-802-867-5309,System Administrator\n",
"print(transform_record(\"Eli Jones,684-3481127,IT specialist\")) # Eli Jones,+1-684-3481127,IT specialist\n",
"print(transform_record(\"Melody Daniels,846-687-7436,Programmer\")) # Melody Daniels,+1-846-687-7436,Programmer\n",
"print(transform_record(\"Charlie Rivera,698-746-3357,Web Developer\")) # Charlie Rivera,+1-698-746-3357,Web Developer\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"# Question 2 V\n",
"# The multi_vowel_words() function returns all words with 3 or more consecutive vowels (a, e, i, o, u). Fill in the regular expression to do that.\n",
"import re\n",
"def multi_vowel_words(text):\n",
" pattern = r'\\b\\w*[aeiou]{3}\\w*\\b'\n",
" result = re.findall(pattern, text)\n",
" return result\n",
"\n",
"print(multi_vowel_words(\"Life is beautiful\")) # ['beautiful']\n",
"print(multi_vowel_words(\"Obviously, the queen is courageous and gracious.\")) # ['Obviously', 'queen', 'courageous', 'gracious']\n",
"print(multi_vowel_words(\"The rambunctious children had to sit quietly and await their delicious dinner.\")) # ['rambunctious', 'quietly', 'delicious']\n",
"print(multi_vowel_words(\"The order of a data queue is First In First Out (FIFO)\")) # ['queue']\n",
"print(multi_vowel_words(\"Hello world!\")) # []"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"// Start of program\n",
" number = 0 // Initialize the variable\n",
" number += 1 // Increment the variable\n",
" return(number)\n"
]
}
],
"source": [
"# Question 4 X -> V\n",
"# The transform_comments() function converts comments in a Python script into those usable by a C compiler. This means looking for text that begins with a hash mark (#) and replacing it with double slashes (//), which is the C single-line comment indicator. For the purpose of this exercise, we'll ignore the possibility of a hash mark embedded inside of a Python command, and assume that it's only used to indicate a comment. We also want to treat repetitive hash marks (##), (###), etc., as a single comment indicator, to be replaced with just (//) and not (#//) or (//#). Fill in the parameters of the substitution method to complete this function: \n",
"import re\n",
"def transform_comments(line_of_code):\n",
" result = re.sub('#+', '//', line_of_code)\n",
" return result\n",
"\n",
"print(transform_comments(\"### Start of program\")) # Should be \"// Start of program\"\n",
"print(transform_comments(\" number = 0 ## Initialize the variable\")) # Should be \" number = 0 // Initialize the variable\"\n",
"print(transform_comments(\" number += 1 # Increment the variable\")) # Should be \" number += 1 // Increment the variable\"\n",
"print(transform_comments(\" return(number)\")) # Should be \" return(number)\"\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"My number is (212) 345-9999.\n",
"Please call (888) 555-1234\n",
"123-123-12345\n",
"Phone number of Buckingham Palace is +44 303 123 7300\n"
]
}
],
"source": [
"# Question 5\n",
"# The convert_phone_number() function checks for a U.S. phone number format: XXX-XXX-XXXX (3 digits followed by a dash, 3 more digits followed by a dash, and 4 digits), and converts it to a more formal format that looks like this: (XXX) XXX-XXXX. Fill in the regular expression to complete this function.\n",
"import re\n",
"def convert_phone_number(phone):\n",
" result = re.sub(r\"\\b(\\d{3})-(\\d{3})-(\\d{4})\\b\", r\"(\\1) \\2-\\3\", phone)\n",
" return result\n",
"\n",
"print(convert_phone_number(\"My number is 212-345-9999.\")) # My number is (212) 345-9999.\n",
"print(convert_phone_number(\"Please call 888-555-1234\")) # Please call (888) 555-1234\n",
"print(convert_phone_number(\"123-123-12345\")) # 123-123-12345\n",
"print(convert_phone_number(\"Phone number of Buckingham Palace is +44 303 123 7300\")) # Phone number of Buckingham Palace is +44 303 123 7300"
]
}
],
"metadata": {