Question

please help in pythonSuppose you have been tasked to write a Python program that will accomplish the following tasks as related to the daily gas p

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

https://onlinegdb.com/Bks5c75GU

def binarySearch (arr, l, r, x):
  
# Check base case
if r >= l:
  
mid = l + (r - l) // 2
  
# If element is present at the middle itself
if arr[mid] == x:
return mid
  
# If element is smaller than mid, then it
# can only be present in left subarray
elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)
  
# Else the element can only be present
# in right subarray
else:
return binarySearch(arr, mid + 1, r, x)
  
else:
# Element is not present in the array
return -1
k = open("gas.txt",'r');
L=[]
L1=[];
lines = k.readlines() #i am reading lines here
for line in lines: #taking each line
conv_int = float(line) #converting string to int
L.append(conv_int) #update counter
L1.append(conv_int) #update counter

L.sort();
print(L1);
print("Lowest 5 are");
for i in range(5):
print(L[i]);
print("Highest 5 are");
for i in range(5):
print(L[len(L)-i-1]);
x=float(input("Enter price you want to search: "));
ind=binarySearch(L,0,len(L)-1,x);
if(ind==-1):
print("It is not in list");
else:
print("Price is in list");

Add a comment
Know the answer?
Add Answer to:
please help in python Suppose you have been tasked to write a Python program that will...
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 Help Please! This is a problem that I have been stuck on.I am only suppose...

    Python Help Please! This is a problem that I have been stuck on.I am only suppose to use the basic python coding principles, including for loops, if statements, elif statements, lists, counters, functions, nested statements, .read, .write, while, local variables or global variables, etc. Thank you! I am using python 3.4.1. ***( The bottom photo is a continuation of the first one)**** Problem statement For this program, you are to design and implement text search engine, similar to the one...

  • 13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program...

    13inary Search Tree Using a binary search tree, you are tasked with building a dictionary program which you can store a word with its definition. Each node of the tree will contain the word and definition. The word is what will be used as the key to sort our data. The dictionary should allow you to search for a word. If the word exist then the definition will display. You can also add words to the dictionary. For testing you...

  • Must be done in python and using linux mint Write a program to create a text...

    Must be done in python and using linux mint Write a program to create a text file which contains a sequence of test scores. Each score will be between 0 and 100 inclusive. There will be one value per line, with no additional text in the file. Stop adding information to the file when the user enters -1 The program will first ask for a file name and then the scores. Use a sentinel of -1 to indicate the user...

  • For each problem, you must: Write a Python program Test, debug, and execute the Python program...

    For each problem, you must: Write a Python program Test, debug, and execute the Python program Save your program in a .py file and submit your commented code to your Student Page. Note regarding comments: For every class, method or function you create, you must provide a brief description of the what the code does as well as the specific details of the interface (input and output) of the function or method. (25 pts.) Write a program that reads in...

  • THIS IS A PYTHON PROGRAM Part 1 – Stocks Research and Program Setup Find five (5)...

    THIS IS A PYTHON PROGRAM Part 1 – Stocks Research and Program Setup Find five (5) of your favorite stocks. In your research you will find the daily closing prices of those stocks from February 11, 2019 through February 15, 2019. Write prices out in a table (5 rows by 5 columns). Part 2 – Python Program Begin writing a Python program that will do the following: create a 2D list named stocks to store the data for each of...

  • Topics: list, file input/output (Python) You will write a program that allows the user to read...

    Topics: list, file input/output (Python) You will write a program that allows the user to read grade data from a text file, view computed statistical values based on the data, and to save the computed statistics to a text file. You will use a list to store the data read in, and for computing the statistics. You must use functions. The data: The user has the option to load a data file. The data consists of integer values representing student...

  • Can you help me write a Python 3.7 code for this question? Write a program using...

    Can you help me write a Python 3.7 code for this question? Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The...

  • Python please For this program, we will be making lists for the local Zoo. You have...

    Python please For this program, we will be making lists for the local Zoo. You have been provided with 4 lists of data already created for you. Using this data, we update the data at this Zoo. YOU MUST USE LIST FUNCTIONS FOR REMOVING TO RECEIVE YOUR FULL POINTS! Input: A type of animal -- This information should be provided in response the question: "What animal are we removing?" A number corresponding to the selection of a menu number (see...

  • Answer in python 3 and can you please include a screenshot of the code and the...

    Answer in python 3 and can you please include a screenshot of the code and the output Problem 4 (Quicksort) For this problem you must implement the recursive quicksort algorithm covered in lecture to sort points based on their distance from the point (0, 0). Crete a Python file called quicksort.py and implement a function called quicksort(list) that does the following: COMP 1005/1405A – S19 – A5 Due Saturday, June 15th at 11:59 PM 3 1. Takes a 2D list...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

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