Question

16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading and writing files. Objectives Be able to reProvided output file: A single output file named myoutput.txt is provided that is initially empty. Description Create a progrSample Output to the File from the Provided Input File palindrome palindrome not a palindrome palindrome not a palindrome palLAB ACTIVITY UTY 16.42.1: Lab 13B: Palindromes with Files 0/20 Downloadable files myinput.txt Download main.py Load default t

In Python Please!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

#opening input file in read mode, output file in write mode
input_file=open('myinput.txt','r')
output_file=open('myoutput.txt','w')

#looping through each line in input_file
for line in input_file:
    #removing trailing/leading newline characters from the line
    line=line.strip()
    #removing all spaces from line to create a new variable
    nospaces=line.replace(' ','')
    #nospaces[::-1] returns the reverse of a string using slicing. if you do not want to use
    #slicing, let me know, i'll replace this with a loop
    #checking nospaces equals the reverse of it
    if nospaces==nospaces[::-1]:
        #palindrome, writing to output_file and console
        output_file.write('palindrome\n')
        print('{} ==> {}'.format(line,'palindrome'))
    else:
        #not palindrome, writing to output_file and console
        output_file.write('not a palindrome\n')
        print('{} ==> {}'.format(line, 'not a palindrome'))

#closing both files, saving changes in output_file
input_file.close()
output_file.close()

#output

bob ==> palindrome
sees ==> palindrome
over the moon ==> not a palindrome
never odd or even ==> palindrome
statistics ==> not a palindrome
dr awkward ==> palindrome

Add a comment
Know the answer?
Add Answer to:
In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 16.43 Lab 13C: Palindromes with Files and Functions Overview This is a demonstration of reading and...

    16.43 Lab 13C: Palindromes with Files and Functions Overview This is a demonstration of reading and writing files, along with using user-defined functions. Objectives Be able to read from an input file, perform string manipulation on each line of the file within a function, and write to an output file. Provided input file: A single input file named myinput.txt is provided that contains a few lines of text. bob sees over the moon never odd or even statistics dr awkward...

  • How do I complete this problem? * CENGAGE MINDIAP > Terminal Opening Files and Performing FileInput...

    How do I complete this problem? * CENGAGE MINDIAP > Terminal Opening Files and Performing FileInput in C++ 0 Opening Files and Performing File Input Flowers.cpp flowers.dat 1 // Flowers.cpp - This program reads names of flowers and whether they are grown in shade or sun from an input 2 // file and prints the information to the user's screen. 3.// Tnput: flowers.dat. 4 // Output: Names of flowers and the words sun or shade. Summary In this lab, you...

  • Reading and Writing Complete Files in C: The first part of the lab is to write...

    Reading and Writing Complete Files in C: The first part of the lab is to write a program to read the complete contents of a file to a string. This code will be used in subsequent coding problems. You will need 3 functions: main(), read_file() and write_file(). The main function contains the driver code. The read_file() function reads the complete contents of a file to a string. The write_file() writes the complete contents of a string to a file. The...

  • Hint: User will enter certain number of random words, you need to read the input as...

    Hint: User will enter certain number of random words, you need to read the input as string and evaluate based on the following instructions. Write a void method called palindromeCheck that takes NO argument. The method should have functionality to check whether or not the word is a palindrome and print to the screen all of the palindromes, one per line. Also, the last line of the output should have the message: “There are x palindromes out of y words...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and...

    C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and backward. Some examples of palindromes are: "radar" "able was i ere i saw elba" and, if you ignore blanks: "a man a plan a canal panama" Write a recursive function testPalindrome that returns 1 if the string stored in the array is a palindrome and 0 otherwise. The function should ignore spaces and punctuation in the string. Use the function in a complete program...

  • Please Explain the following question in detail so that I can understand how to solve it:...

    Please Explain the following question in detail so that I can understand how to solve it: Assume that you want to use both an input file called "myinput.txt" and an output file called "myoutput.txt" which are located in a folder called "mydata" on the g: drive. In the program below, fill in all the missing details so that you can read a series of values (one by one) from the input file into the variable "num". Continue to read values...

  • In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 st...

    In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 standard input (stdin) 1 standard output (stdout) 2 standard error (stderr) By default, the command interpreter (shell) reads keyboard input from file descriptor 0 (stdin) and writes output to file descriptor 1 (stdout), which appears on the screen. As you explored in Lab 2, input/output can be...

  • in Java and also follow rubric please 4. Write a complete program to do the following:...

    in Java and also follow rubric please 4. Write a complete program to do the following: Using an input and output files, write a program that will read 20 numbers from an input file called InFile. Sum all even numbers and multiply all odd numbers. Print the numbers read from the input file to an output file called OutFile. Also print the sum of the even numbers and the product of the odd numbers to the output file. Rubric: •...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT