Question

python

Write a function that reads a single line of numbers from a file named 
"question1.txt", and returns the average of the numbers read. For example, if 
the file is as follows:

----- question1.txt -----
1 3 5 -2 4 0 6 -2 -3
-------------------------

then the function must return 12/9 = 1.33333
"""

def file_average():
    return # Remove this line to answer this question


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

program

import os
from os import path

def text2dict(filename):
data_dict={}
if path.exists(filename):
f = open(filename, "r")
data=f.read()
print(data)
for i in data:
if i!='\n':
if i in data_dict:
data_dict[i]=data_dict[i]+1
else:
data_dict[i]=1
  
  
return data_dict

  
filename="C:/Users/user/IR_WORK/text.txt"
dict=text2dict(filename)
print(dict)

output

runfile('C:/Users/user/IR_WORK/chegg.py', wdir='C:/Users/user/IR_WORK')
hello
name
{'h': 1, 'e': 2, 'l': 2, 'o': 1, 'n': 1, 'a': 1, 'm': 1}

C:\Users\user\IR_WORK Е, 2 ас chegg.py X Name Type Size Value dict dict 7 {h:1, e:2, l:2, o:1, n:1, a:1, m:1} f


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
python
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
  • python

    I this question, you will be given a list of integers, and you have to  write in a text file named "question3.txt" a barplot corresponding to the  given numbers, as shown below. For example, if you are given [3,5,2,6,4],  then you will write in the file the following plot: ----- question3.txt -----       |   |   |   |   | | | |   | | | | | | | | | | | | 3 5 2 6 4 ------------------------- The given integers will be between 0 and 9 (both inclusive).  """ def file_plot(l1):     return # Remove this line to answer this question

  • python

    Write a function that takes two lists of integers X and Y as input, and  computes and writes a table into a file named "question2.txt" as follows: - On each line the computations for an X,Y pair is given  - Each such possible pair must exist in the file in the same order of values in X and Y - Next on each line, you provide X+Y, X*Y and X^2 + Y^2, all separated by a  comma (and no spaces or other characters must exist). Note that X^2 represents  "square of X".  For example, given X = [1,3] and Y=[-1,1,0], "question2.txt" should contain the following table: ----- question2.txt ----- 1,-1,0,-1,2 1,1,2,1,2 1,0,1,0,1 3,-1,2,-3,10 3,1,4,3,10 3,0,3,0,9 ------------------------- As you can see, the first line is computed for X=1, Y=-1. The second line is  built for X=1, Y=1; and the third line is for X=1, Y=0. It goes on like this,  until X=3, Y=0.  """ def op2file(X, Y):     return # Remove this line to answer this question

  • python

    Complete the below function that takes a file name and a list as arguments and reads the text within the corresponding file, then creates a dictionary whose keys are the characters of the provided list, and the values are the counts of these characters within the text. If a character in the list is not included in  the text, "0" should be written in the dictionary as the value of this  character. The function returns the created dictionary as a result. If the file  does not exist, then the function returns an empty dictionary. Here is an example case, assuming a file named "sample.txt" exists with the  following content: ----- sample.txt ----- This is an example sentence. This is yet another sentence. ------------------------- >>> text2dict("sample.txt", ['a', 'b', 'c', 't']) {'a':3, 'b':0, 'c':2, 't':4} """ def text2dict(filename, characters):     return # Remove this line to answer this question

  • python

    pythonComplete the below function that takes the name of two files, inFilename andoutFilename as arguments and reads the text from the inFilename. In this fileeach line contains the Turkish Republic Identity Number (TCNO), name, surnameand telephone number of a person. Your function should sort all personsaccording to their TCNO, write the sorted data into outFilename. If the fileinFilename does not exist, then the function must create an empty file namedoutFilename.For example, if the function is called such asreadText("in.txt", "out.txt")and in.txt...

  • Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a...

    Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a function named, file_commands, that takes the name of a file as a parameter. The function processes the contents the file as follows: For file lines that begin with the letter 'a', calculate & print the integer average of the numbers on the line. For file lines that begin with the letter 'u', print the upper case format for each word following the u. Sample...

  • I'm a bit confused on how to get this program to run right. Here are the...

    I'm a bit confused on how to get this program to run right. Here are the directions: Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed. For example, ‘This line has extra space characters ‘  ‘This line has extra space characters’ Function name: reduceWhitespace Number of parameters: one string line Return value: one string line The main file should handle the...

  • In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading...

    In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading and writing files. Objectives Be able to read from an input file, perform string manipulation on each line of the file, 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 Provided output file: A single output file...

  • Please write the following code as simple as possible in python: You will need to define...

    Please write the following code as simple as possible in python: You will need to define a function with four arguments. Here is what I used: > def find_matches(file1.txt, output1.txt, strings, value): file1.txt will contain a list of various strings. The program must copy from the first argument, and it should be written in the second argument (the second file, "output1.txt"). The third and fourth arguments will determine which specific strings will be copied over to the second file. For...

  • Question 10 Not complete Not graded Generalize the function you wrote previously so you can pass ...

    = Question 10 Not complete Not graded Generalize the function you wrote previously so you can pass in the name of a function as a parameter For example def tabulate_execution_cost(rows, funct_name): Flag question temp, count funct_name (data) To reproduce the previous table, you would call this function using tabulate_execution_cost (10, max_duplicates_in_1ist) For example Test Result tabulate_execution_cost (2, max_duplicates_in_list) NI COUNT| LOGN /N NLOGN /N* *2 13.0 6.5 13 13.0 26 26.0 13.0 tabulate_execution_cost (2, find_difference) COUNT | /LOGN N NLOGN...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

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