Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python 3.7.8 #3)The following programs are not working correctly. Find and resolve the error(s) so they work as intended. Above each code snippet is a

python 3.7.8

#3)The following programs are not working correctly. Find and resolve the error(s) so they work as intended. Above each code snippet is a description of how the program should function.

#3.1) The following function takes a filename as a parameter and returns the number of words in the file.

def wordcount(fname): countofwords = 0 infile = open(fname,'r') s = infile.readline() infile.close() s = s.split() return len(s)

#3.2) The following function takes a string as a parameter and returns a list containing all of the digits in the string. The list contains integers rather than strings. If there are no digits in the string, an empty list is returned. If a digit appears multiple times, it is included in the list multiple times.

def findDigits(s): newLst=[] for char in s: for i in range (0,10): if str(i) = char: newLst.append(i) return newLst

#Example output: #>>>findDigits(3 2 1 blastoff!) #[3,2,1] #>>> findDigits(Testing 1, testing 12, testing 123.) #[1,1,2,1,2,3] #>>> findDigits(There are no digits here) #[]

#3.3) The following function HALF takes a list of numbers and returns True of each element is exactly half the previous, and False otherwise. #Test with the following: #a = [7,5,3,1] returns False #b = [4,3,2,1] returns False #c = [20,10,5] returns True

HALF(nums): for i in range(0,len(nums)-1): if nums[i+1] == (nums[i]*2): pass else: return False return True

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions