use the same function to read a file called “example.txt”, filled in with a few sentences or a paragraph. Then write a function called typoglycemia, which scrambles the letters in the words in the file leaving the first and last characters unchanged and then writes the results to a new file called “scrambled.txt”
write in python 3
Before going to program this problem, first we need to define certain rules which is necessary to follow before implementing the problem.
Rules to be followed:-
- Words less than or equal to 3 characters need not be scrambled.
- Don't scramble first and last char, so Scrambling can become Srbmnailcg or Srbmnailcg or Snmbracilg, i.e letters except first and last can be scrambled in any order.
- Punctuation at the end of the word to be maintained as is i.e. "Suprising, " cound become "Spsirnirug, " but not "Spsirn,irug".
- Following punctuation marks are to be supported - Comma, Question mark, Full Stop, Semicolon, Exclamation
- Do this for a file and maintain sequences of lines.
Below is the implementation:-
def typoglycemia():
import random
punct = (".", ";", "!", "?", ",")
count = 0
new_word = ""
inputfile = input("Enter input file name:")
with open(inputfile, 'r') as fin:
for line in fin.readlines(): #Read line by line in txt file
for word in line.split(): # Read word by word in each line
if len(word) > 3: # If word length >3
'''If word ends with punctuation, Remove first letter, last letter and punctuation
shuffle the words: Add the removed letters at their respective positions'''
if word.endswith(punct):
word1 = word[1:-2]
word1 = random.sample(word1, len(word1))
word1.insert(0, word[0])
word1.append(word[-2])
word1.append(word[-1])
''' If there is no punctuation mark: Remove first and last letter.
Shuffle the word then add removed letters at their respective position'''
else:
word1 = word[1:-1]
word1 = random.sample(word1, len(word1))
word1.insert(0, word[0])
word1.append(word[-1])
new_word = new_word + ''.join(word1) + " "
''' If word length <3, just append the word and " " to the the previous words'''
else:
new_word = new_word + word + " "
with open((inputfile[:-4] + "scrambled.txt), 'a+') as fout:
fout.write(new_word + "\n")
new_word = ""
Call the function by typoglycemia() and give the input file file name. It will return you the scrambled.txt file as output.
Example:-
Input : example.txt ->
Scrambling words is very interesting. Because even if they are scrambled, it doesn't impact our reading.
Because we don't read letter by letter, we read the word as a whole.
Output : scrambled.txt ->
Srbmnacilg wrods is very itrensientg. Bscauee even if tehy are srelabcmd, it dosn'et ipcmat our raidneg.
Bacusee we dn'ot raed lteetr by letetr, we raed the wrod as a wolhe.
use the same function to read a file called “example.txt”, filled in with a few sentences...
You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to...
In
python
Write a function called removeExtensión that is given the name of a file as a string and returns the same string without the extension (the last "" and any following characters); if the given string has no extension, return the given string without changing it. For example, removeExtension"Chapter 3.9.txt") should return "Chapter 3.9" while removeExtension("Hello") should return "Hello"
Python Problem Write a .py file like nameFiles.py. In that .py, create a function that strips the first letters from all file names in a directory. If the new name is a duplicate of an existing name, Python will throw an exception. Handle that exception by adding “dup” a number as part of the files new name. Call that function from a Jupyter Notebook, passing in the number of characters to delete and the file path.
python program please
Write a function called backwards which takes two parameters. The first is a file object that has already been opened in read mode called essayfile. The second is called filename, which contains the filename for the output file that will need to be properly opened and closed in your function. The function should read from already opened file object line by line. Each line in the file should have the words on the line reversed. For example,...
Python 3.6
Question 12 (2θ points) write a function named uniqueWords that counts how many different words there are in each line of an input file and writes that count to a corresponding line of an output file. The input file already exists when uniqueWords is called. uniquewords creates the output file. Input. The function uniquewords takes two parameters: ◆ inFile, a string that is the name of text file that is to be read. The file that inFile refers...
•Write use pytohn a function File2List(filename) that: –Read line by line from a file –For each line of words, –change all characters to lowercase –remove all the punctuations –remove whitespaces except the blank – ‘ ’ –split the words within the line by the blank and form a list of separated words –Merge all the list of separated words together and the function should return a wordlist of individual words
Write a program that loads a file called "sample.txt" in read mode, reads its content, and closes the file. Use exception handling to catch any errors. 2. Compute the letter and punctuation distribution in the file. That is, output the number of a’s, the number of b’s, etc. and the number of commas, dashes, and periods. Ignore case when computing this, so ‘A’ and ‘a’ are the same letter. Use lists. 3. Compute the number of words in the file....
Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...
please
write a C++ code
Problem A: Word Shadow Source file: shadow.cpp f or java, or.cj Input file: shadow.in in reality, when we read an English word we normally do not read every single letter of that word but rather the word's "shadow" recalls its pronunciation and meaning from our brain. The word's shadow consists of the same number of letters that compose the actual word with first and last letters (of the word) in their original positions while the...
Write another function called checkdiag2 (int checkdiag2 (int matrix[][100], int size)) that will return 1 if all the numbers on the antidiagonal are the same and 0 otherwise. Rewrite your main program to use 2D dynamic allocation instead for your array. Your array will have rows and columns depending of the first integer read from the file. Call your function from the main program. A typical report would look like The matrix is 8x8 and all the numbers on the...