Python problem
If a variable training_text represents a file open for reading, then training_text.read() returns a string containing the entire contents of the file. You can use the str method split to make your list.
from typing import TextIO, List
def get_words(training_text: TextIO) -> List[str]:
'''
Return a new list containing the words from training_text in the name order as they appear in training_text.
'''
from typing import *
def get_words(training_text: TextIO) -> List[str]:
"""
Return a new list containing the words from training_text in the name order as they appear in training_text.
"""
data = training_text.read()
return data.split()
Python problem If a variable training_text represents a file open for reading, then training_text.read() returns a...
Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: valiant paris The valiant Paris seeks for his love. -Enter the text that you want to search for, or DONE when finished: Romeo and Juliet Enter Romeo and Juliet aloft, at the WIndow -- Enter the text that you want to search for, DONE when finished: DONE # copy the following two lines into any # program...
Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...
Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...
Task 1: Reading files Part A - Read a word Implement a function word from.file(file) that takes a file as input and returns the first word in the file. Hint: the string methods split and strip will be useful. Example: calling word from file('files/task1A.txt') returns 'Once'. Part B - Read a table Implement a function nested int.list from file(file) that takes a file containing lines of integers that are separated by commas as input and returns the file contents formatted...
IN PYTHON Ask the user for a file name Open the requested file Split the contents of the file into words Iterate over each word Translate the word to Pig Latin Output the translated word to the screen How to Speak Pig Latin https://www.wikihow.com/Speak-Pig-Latin
Could someone help with this function Python? It involves opening and reading from a text file. Write a function that returns a list k of words that start at word i of the text. def extractWord(text, i=0, k=None)
python
Create a program to open a text file for reading, find the maximum number in the file, determine if that maximum number is even, and write to an output text file. You should either write Yes if the number is even, otherwise write the maximum number. You should note the following: • Your input file must be named input.txt • The input file has one integer number per line • Your output file must be named output.txt • Your...
this is a python3 question every_second_line Complete the following function according to its docstring description. Notes: report is a file open for reading, not a string representing the name of a file. the method str.strip() may come in handy! def every_second_line(report): """ (Open File for reading) -> list of str Return a list containing every second line (with leading and trailing whitespace removed) in report, starting with the first line. """ I really got stuck on this question:( could...
Convert Python to Java
def read_fsm(filename):
fh = open('fsm.txt','r')
contents = fh.readlines()
sigma = list(contents[0].rstrip().split(' '))
table = {}
n = int(contents[1])
for i in range(n):
table[i] = {}
final = list(map(int,contents[2].rstrip().split(' ')))
for line in contents[3:]:
fro,ip,to = line.split(' ')
fro = int(fro)
to = int(to)
table[fro][ip] = to
print(table)
fh.close()
return table,final
def runString(table,final,string):
current = 0
for i in string:
current = table[current][i]
if current in final:
print(string,'--> ACCEPT')
else:
print(string,'--> REJECT')
def readInput(table,final):
fh = open('Strings.txt','r')...
In Python 3 only please. A simple function that will be used on
a file.
commonpair(str) – Takes a single string
argument, representing the first word. This function should return
the word that most frequently followed the given argument word (or
one of, in case of ties). If the argument word does not appear in
the text at all, or is never followed by another word (i.e., is the
last word in the file), this function should return None.
I...