[Solved]Lab6 Due March 13rd 11 55 Pm Cs 497 Introduction Python Programming Instructions 0 Remembe Q37025605

![I In [13]| | ## Lab 7 import random from string import ascii_letters ##File stuff file nameusdec.txt file_input -open(file_](https://media.cheggcdn.com/media%2F961%2F961eb044-f37d-471e-bdba-bc65b89de07c%2FphpVxrXeY.png)
![In [191: def mark twain function(string input): ##Your code goes here pass In [15]: ##Does this Look familiar? :) def word_re](https://media.cheggcdn.com/media%2F24b%2F24b09da0-43ca-459b-be98-40641a43c39f%2FphpAVGSPW.png)
Lab6 due March 13rd at 11:55 PM CS 497 Introduction to Python Programming Instructions 0. Remember to comment. If your code fails to perform the task at hand but your comments are describing proper code execution I can award a substanial amount of points for logical errors in your program. Think of comments as the equivalent of showing your work in a math class 1. Read all instructions carefully, ask questions if anything is confusing 2. Fill in the code/text blocks to answer each question 3. Do not change any of the existing code provided. The code is specifically there to help you! 4. Run the entire notebook before submitting it on Sakai to make sure that the code actually runs without errors 5. Important: Any question for which your code fails to run will receive 0 points 6. Have fun! 7. DO NOT USE ANY PACKAGES, other than the ones I have imported. Part of learning is to struggle through the “hard way” before getting to do things the easy way. Any packages I have imported are fair game and you may use any functions unless otherwise specified. If I’ve imported them after a function I’ve deliberately made it so you cannot retroactively use them in an earlier problem 8. If I’ve used return(None) for a function, you may definitely change the return type. It’s just a placeholder 9. While you may discuss the concepts of the problems with your peers your code will need to be yours and yours alone 10. Replace CS497 with your BUID (ie if I was turning in this assignment my file would be called dbrennan-lab#6) 11. Do not zip your lab file with the txt, I do no want your generated txt (I will make it myself!) I In [13]| | ## Lab 7 import random from string import ascii_letters ##File stuff file name”usdec.txt” file_input -open(file_name,”r”) lines_of file – file_input.readlines() file_output -open(“output.txt”,”a”) It is a well-known phenomenon that most people are easily able to read a text whose words have two characters flipped, provided the first and last letter of each word are not changed Let’s look below: dn’ot gvie a dman for a man taht can olny sepll a wrod one way. (Mrak Taiwn) Real: I don’t give a damn for a man that can only spell a word one way. (Mark Twain) You are going to build this function and translate a document I’ve included. I’ve handled all the file processing, splitting the words out of the text. You merely need to take in a word (string input) and return a word (string output) having shifted some things around. My code is designed to give your function a word, it will handle the spacing/punctuation/etc. If contains anything other than letters, return empty string!!! Import to note: Words of length 1/2/3 will remain the same. 1/2 are trivial, but words of length 3 have one character than can swap, therefore it can only swap with itself. Words of length 4 are when the fun begins, you’ll need to preserve the first and last letter. Choose two random middle letters and swap them. It’s pretty easy to pick a word where this could fail, choose any palindrome like racecar. The r’s are excluded from selection but it’s certainly plausible the a’s switch places. For the sake of simplicity if the letters are identical and get swapped, we’ll put a ” (the single slash) in front of the first one to denote it’s been swapped So “racecar with the a’s both being randomly selected turns into “racecar”. It becoems a little less readable, but it alerts me that you’ve swapped the characters appropriately Despite the example clearly moving the apostrophe, don’t worry about anything other than the two letters switching. Your life will be made easier just handling the swap of two characters. Starting from now on, with labs (maybe some homework problems) I’ll put a bounty on my code. l’Il give you the number of lines in my solution (WHICH IS NOT YOUR GOAL). I did not design my code to be particularly compact, nor hyper-efficient, I restricted myself to the knowledge you have at this point in the course. If you are using python code we haven’t discussed your solution will not be counted as I did not make use of advanced data structures to do mine! If you ae feeling the spirit of a challenge if you beat (not tie) my code l’ll start awarding prizes. I of course will not show you my code, but you can trust me (we can have another professor verify the authenticity of my claim if we must). Your code must work flawlessly, receive full credit and not do anything crazy to accomplish it’s goal. If you feel you’ve beat me make a note above your function. Record to beat: 12 In [191: def mark twain function(string input): ##Your code goes here pass In [15]: ##Does this Look familiar? 🙂 def word_replacer(string): output_string temp-word ##We know we’ve “seen a word” when we find the first non-alpha character after finding some prior Letters for character in string: #If it’s not alpha, we merely need to preserve it if character in ascii letters: temp_word+-character else: #First find the word #FinaLLy reset the temp word flipped_word – mark_twain_function(temp_word) output_string+-mark_twain function (temp_word)+character temp_word #HandLe Last character not being newline and we currently have a word output_string+-mark_twain function (temp word) return output_string In [16]: ##Process file for i in lines_of file: print(word_replacer(i),file-file_output) file_input.close) file_output.close) Show transcribed image text Lab6 due March 13rd at 11:55 PM CS 497 Introduction to Python Programming Instructions 0. Remember to comment. If your code fails to perform the task at hand but your comments are describing proper code execution I can award a substanial amount of points for logical errors in your program. Think of comments as the equivalent of showing your work in a math class 1. Read all instructions carefully, ask questions if anything is confusing 2. Fill in the code/text blocks to answer each question 3. Do not change any of the existing code provided. The code is specifically there to help you! 4. Run the entire notebook before submitting it on Sakai to make sure that the code actually runs without errors 5. Important: Any question for which your code fails to run will receive 0 points 6. Have fun! 7. DO NOT USE ANY PACKAGES, other than the ones I have imported. Part of learning is to struggle through the “hard way” before getting to do things the easy way. Any packages I have imported are fair game and you may use any functions unless otherwise specified. If I’ve imported them after a function I’ve deliberately made it so you cannot retroactively use them in an earlier problem 8. If I’ve used return(None) for a function, you may definitely change the return type. It’s just a placeholder 9. While you may discuss the concepts of the problems with your peers your code will need to be yours and yours alone 10. Replace CS497 with your BUID (ie if I was turning in this assignment my file would be called dbrennan-lab#6) 11. Do not zip your lab file with the txt, I do no want your generated txt (I will make it myself!)
I In [13]| | ## Lab 7 import random from string import ascii_letters ##File stuff file name”usdec.txt” file_input -open(file_name,”r”) lines_of file – file_input.readlines() file_output -open(“output.txt”,”a”) It is a well-known phenomenon that most people are easily able to read a text whose words have two characters flipped, provided the first and last letter of each word are not changed Let’s look below: dn’ot gvie a dman for a man taht can olny sepll a wrod one way. (Mrak Taiwn) Real: I don’t give a damn for a man that can only spell a word one way. (Mark Twain) You are going to build this function and translate a document I’ve included. I’ve handled all the file processing, splitting the words out of the text. You merely need to take in a word (string input) and return a word (string output) having shifted some things around. My code is designed to give your function a word, it will handle the spacing/punctuation/etc. If contains anything other than letters, return empty string!!! Import to note: Words of length 1/2/3 will remain the same. 1/2 are trivial, but words of length 3 have one character than can swap, therefore it can only swap with itself. Words of length 4 are when the fun begins, you’ll need to preserve the first and last letter. Choose two random middle letters and swap them. It’s pretty easy to pick a word where this could fail, choose any palindrome like racecar. The r’s are excluded from selection but it’s certainly plausible the a’s switch places. For the sake of simplicity if the letters are identical and get swapped, we’ll put a ” (the single slash) in front of the first one to denote it’s been swapped So “racecar with the a’s both being randomly selected turns into “racecar”. It becoems a little less readable, but it alerts me that you’ve swapped the characters appropriately Despite the example clearly moving the apostrophe, don’t worry about anything other than the two letters switching. Your life will be made easier just handling the swap of two characters. Starting from now on, with labs (maybe some homework problems) I’ll put a bounty on my code. l’Il give you the number of lines in my solution (WHICH IS NOT YOUR GOAL). I did not design my code to be particularly compact, nor hyper-efficient, I restricted myself to the knowledge you have at this point in the course. If you are using python code we haven’t discussed your solution will not be counted as I did not make use of advanced data structures to do mine! If you ae feeling the spirit of a challenge if you beat (not tie) my code l’ll start awarding prizes. I of course will not show you my code, but you can trust me (we can have another professor verify the authenticity of my claim if we must). Your code must work flawlessly, receive full credit and not do anything crazy to accomplish it’s goal. If you feel you’ve beat me make a note above your function. Record to beat: 12
In [191: def mark twain function(string input): ##Your code goes here pass In [15]: ##Does this Look familiar? 🙂 def word_replacer(string): output_string temp-word ##We know we’ve “seen a word” when we find the first non-alpha character after finding some prior Letters for character in string: #If it’s not alpha, we merely need to preserve it if character in ascii letters: temp_word+-character else: #First find the word #FinaLLy reset the temp word flipped_word – mark_twain_function(temp_word) output_string+-mark_twain function (temp_word)+character temp_word #HandLe Last character not being newline and we currently have a word output_string+-mark_twain function (temp word) return output_string In [16]: ##Process file for i in lines_of file: print(word_replacer(i),file-file_output) file_input.close) file_output.close)
Expert Answer
Answer to Lab6 due March 13rd at 11:55 PM CS 497 Introduction to Python Programming Instructions 0. Remember to comment. If your c… . . .
OR

