HELP! HOW WOULD I WRITE THIS IN PSEUDOCODE?
from itertools import accumulate
from collections import Counter
def addvalues(a, b):
if type(a) is str:
a = a.strip("\n")
if type(b) is str:
b = b.strip("\n")
return int(a) + int(b)
def maxValues(a, b):
if type(a) is str:
a = a.strip("\n")
if type(b) is str:
b = b.strip("\n")
a = int(a)
b = int(b)
return max(a, b)
def main():
counter = 0
total = 0
maximum_cars = 0
file = open("MainAndState.dat", "r")
maximum = accumulate(file, maxValues)
for maximum_cars in maximum
pass
file.seek(0)
result = accumulate(file, addvalues)
for total in result:
counter+=1
print("Maximum number of cars observed per day:
{}".format(maximum_cars))
print("Total Cars: {}".format(total))
print("Total days observed: {}".format(counter))
print("Average number of cars observed per day:
{}".format(total/counter))
file.close()
if __name__ == "__main__":
main()
PSEUDO-CODE:
1. DEFINE FUNCITON addvalues(a,b)
(i) Check if a is of string type:
if so strip it
from the rest of line i.e '\n character.' and store it in a.
(ii) check if b is of string type:
if so strip it
from the rest of line i.e '\n character.' and store it in a.
(iii) return a + b after casting them in to integers
by using int() function.
2. Define Function maxValues(a,b)
(i) Check if a is of string type:
if so strip it
from the rest of line i.e '\n character.' and store it in a.
(ii) check if b is of string type:
if so strip it
from the rest of line i.e '\n character.' and store it in a.
(iii) return max(a,b) after casting them in to
integers by using int() function.
#see you haven't defined logic for returning max (a,b)
i.e if a>b:
return a;
else: return b;
3. Define main() function in which actual process is written:
(i) initialize counter, max, maximum_cars with
0.
(ii) Open file "MainAndState.dat" in read mode.
(iii) assign iterator accumulate of file by passing
file and function maxValues as its object to find max of every line
in the file.
(iv) now iterating he maximum values we add values the
variable result using accumulate iterating by passing file and
fucntion object addValues as parameter.
(v) update the counter at every iteration. and go to
next element in maximum list i.e step iv.
(vi) print maximum_cars,total,counter,and average =
total /counter to the output each in newLine.
(vii) close the opened file in step (ii).
4. call main() in __main__ system call.
HELP! HOW WOULD I WRITE THIS IN PSEUDOCODE? from itertools import accumulate from collections import Counter...
I need help with my python class. thanks! Question 12 Code Example 4-4 main program: import arithmetic as a def multiply(num1, num2): product = num1 * num2 result = a.add(product, product) return result def main(): num1 = 4 num2 = 3 answer = multiply(num1, num2) print("The answer is", answer) if __name__ == "__main__": main() arithmetic module: def add(x, y): z = x + y return z Refer to Code...
Number 1) Which of the following statements imports a module into the default namespace? a. from temperature import * b. import temperature as t c. import temperature as temp d. import temperature Number 2) Which of the following statements imports a module into the global namespace? a.from temperature import * b. import temperature as temp c. import temperature as global d. import temperature Number 3) Code Example 4-2 def get_volume(width, height, length=2): volume = width * height * length...
I highlighted the row I am having a constant syntax error issue. import struct record = open("block.dd") def open(file): try: record = open(block.dd, "rb") record = bytearray() record = file.read(2048) file.close() finally: return record def search_record(x, y): try: search = x[y] except(IOError): print('Cannot search') else: return search status = 0x1BE if status == 0x80: print("Status: Active") else: print("Status: Not active") ptype = record[0x1BE+4] print("The partition type is " + str(search_file(record, ptype)) address = struct.unpack("<i", record[0x1BE+8:0x1BE+12]) print("Address of the first sector...
Write a function that takes, as an argument, the name of a file, fileName . Your program should open (and read through) the file specified, and return the maximum value in the file. Name this function maxValueInFile(fileName). def openFile(): # Prompt for the file name filename =input("Enter a file name: " ) # Open the file for reading file = open(filename) # Prompt for the target value target =input("Enter a threshold value: ") # Initialize the counter counter = 0...
Hi, can someone offer input on how to address these 4 remain parts the zybook python questions? 4: Tests that get_num_items_in_cart() returns 6 (ShoppingCart) Your output ADD ITEM TO CART Enter the item name: Traceback (most recent call last): File "zyLabsUnitTestRunner.py", line 10, in <module> passed = test_passed(test_passed_output_file) File "/home/runner/local/submission/unit_test_student_code/zyLabsUnitTest.py", line 8, in test_passed cart.add_item(item1) File "/home/runner/local/submission/unit_test_student_code/main.py", line 30, in add_item item_name = str(input('Enter the item name:\n')) EOFError: EOF when reading a line 5: Test that get_cost_of_cart() returns 10 (ShoppingCart)...
I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...
Please write a code in python! Define and test a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function, but it uses the given RGB values instead of black. images.py import tkinter import os, os.path tk = tkinter _root = None class ImageView(tk.Canvas): def __init__(self, image, title = "New Image", autoflush=False): master = tk.Toplevel(_root) master.protocol("WM_DELETE_WINDOW", self.close) tk.Canvas.__init__(self, master, width = image.getWidth(), height = image.getHeight())...
Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt, verbs. txt, articles.txt, and prepositions.txt. (HINT: Define a single new function, getWords. This function should expect a filename as an argument. The function should open an input file with this name, define a temporary list, read words from the file, and add them to the list. The function should then convert the list...
Using python 3.6 How do I incorporate 0 or negative input to raise an error and let the user try again? like <=0 #loop to check valid input option. while True: try: choice = int(input('Enter choice: ')) if choice>len(header): print("Invalid choice!! Please try again and select value either:",end=" ") for i in range(1,len(header)+1): print(i,end=",") else: break choice = int(input('\nEnter choice: ')) except ValueError as ve:print('Invalid response, You need to select the number choice from the option menu') # Catch your...
Code Example 14-3 class Multiplier: def __init__(self): self.num1 = 0 self.num2 = 0 def getProduct(self): return self.num1 * self.num2 def main(): m = Multiplier() m.num1 = 7 m.num2 = 3 print(m.num1, "X", m.num2, "=", m.getProduct()) if __name__ == "__main__": main() Refer to Code Example 14-3: When this code is executed, what does it print to the console? a. 7 X 3 = 0 b. 3 X 7 = 0 c. 7 X 3...