I'm trying to write code for this and I'm completely lost. I know the answer but am new to code and don't know my way around. Can someone point me in the right direction even if its just a similar example.
A cash drawer contains 160 bills, all 10s and 50s. The total value of the 10s and 50s is $1,760.
How many of each type of bill are in the drawer? You can figure this out by trial and error (or by doing algebra with pencil and paper), but try to use loops and conditionals to check a plausible possibilities and stop when you find the correct one.
I've tried to write it out in code but keep getting syntax errors. I tried looking up loops and conditionals on google but im still confused. I think the range function is what I'm looking for but I don't know how to key it in. I don't want the answer I just want resources for finding it.
print("Print integers within given start and stop number using range
(function")
for x in range (1,160)
print(x, end=',')
Indentation error: Expected an indented block
Its has to have loop, i would suggest you use while loops, and have a variable for the dollars and take one down and add one when it doesn't match.
Look up while loops, variables and if statements and you should get it.
Related
This is the question:
One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually
offered several problems during programming contests. Long before the
start the friends decided that they will implement a problem if at
least two of them are sure about the solution. Otherwise, the friends
won't write the problem's solution.
This contest offers n problems to the participants. For each problem
we know, which friend is sure about the solution. Help the friends
find the number of problems for which they will write a solution.
Input The first input line contains a single integer n (1 ≤ n ≤ 1000)
— the number of problems in the contest. Then n lines contain three
integers each, each integer is either 0 or 1. If the first number in
the line equals 1, then Petya is sure about the problem's solution,
otherwise he isn't sure. The second number shows Vasya's view on the
solution, the third number shows Tonya's view. The numbers on the
lines are separated by spaces.
Output Print a single integer — the number of problems the friends
will implement on the contest.
This is the code that I wrote for this:
n=input()
a=0
for i in range (0,n):
l,m,o= int(input().split())
if l+m+o>1:
a+=1
else:
a+=0
print (a)
But it always shows runtime error. I am unable to figure out what exactly is wrong here. I thought it was a global-local variable problem but apparently its not. I also tried taking inputs in the form of a list and then summing up integers but that didnt work either.
PS This is my first question here so I apologize in advance for silly errors.
In this quiz, we aren't asking you to solve problems. Instead, you are
given some problems and their incorrect solutions, and your task is to
find tests where these solutions actually work incorrectly — by
returning incorrect answer, working for too long, crashing, taking too
much memory — anything that would give a verdict other than
"Accepted". Consider that the time limit is 2 seconds, and the memory
limit is 256 megabytes for all of the following problems.
This exercise will help you imagine tests for your own code. And it's
actually very similar to "challenges" on many competitive programming
platforms (e.g. Topcoder, Codeforces) — where after solving a problem
you could earn additional score by breaking other's solutions to it.
We hope you'll like it!
Next follows the first problem and the incorrect solution to it.
You are given a non-empty list of integers, and you need to find the
maximum value among them. The length of the list is not greater than
100, and the absolute value of each element is not greater than 1000.
def solve(a):
max = 0
for x in a:
if x > max:
max = x
return max
Implement a function called getTest. It should return a list on which
the solve function works incorrectly. Note that the returned list must
fit the restrictions in the statement.
The function is to be implemented in Python 3, but if you don't know
this language, it's no problem — the sample code should give you the
idea of how to do what you need.
def getTest():
return [1,2,3];
I need to find the list where solve function fails but I am unable to do it since I am new to programming. Can anyone help me understand it?
Since this smells like homework, I'll give you a hint rather than the full solution. You don't need to worry about being new to programming, you can figure this out without writing a single line of code :-)
In their "solution", they make an assumption about the values in the list that may not always hold. Can you figure out what this assumption is?
def getTest():
return [-1]*100
This will make sure it can't find the max value and it will also return 0.
I need help with a homework problem that i'm not sure how to go about since we didn't have time to go over it before our exam.
"write a program that asks the user for the cost of lunch for five working days and prints out the average cost with two digits after the decimal point. You need to take input in the same line, where different values are separated by a space."
I believe that the first step would be to assign a variable to an input then use the split function but im stuck at that step
the question is available here http://courses.cse.tamu.edu/davidkebo/csce-110/labs/lab_2.pdf
lunch_cost = input("enter lunch cost: ").split()
We shouldn't really give you too much help with this, because as #MyNamelsCaleb said in a comment, it states in linked PDF that you are not allowed to copy from another source.
So to get around this, I'll give you a basic idea of how to do this. All of these steps should be easy to work out or google.
Your method for getting input is correct, but you need to split it at the comma.
You then need to calculate the length of the list and save that to the variable.
Then you can use the sum() function which will add the list together for you.
Finally, you can compute the average by dividing the number sum returns by the length of the list.
To size the output properly, that can be solved with a quick google.
How do I (efficiently) loop through huge ranges in Python? Whenever I try to run something like-
for x in range(105, 10000000000000):
#do stuff
it takes an eternity to complete. I've tried many things like iter, set, xrange but still no success. Is there any external module which can help me do that?
Given the range, I think it's safe to say that almost regardless of what you do or how you do it, it's going to take a while.
To be more specific, you're executing approximately 1E+13 iterations of your loop. With decent code and a reasonable fast processor, you might be able to execute around 4E+9 instructions per second (e.g., 2 instructions per clock cycle at 2 GHz). That obviously isn't exact, but for the moment let's just go with it, and see where we end up.
So, even if we assume each iteration of the loop requires executing only one, single-cycle instruction, it's going to take approximately: 1E+13/4E+9 = 2.5E3 seconds = ~42 minutes. Given that you're doing some work inside the loop, and there's some overhead for the loop itself, we're clearly going to be execution more than one machine-code instruction per iteration. If we have to execute, say, 1000 instructions per iteration (may be a reasonable first approximation, given that you haven't told us anything about what you're doing), then we're looking at something like 600-700 hours for the loop to execute.
Bottom line: changing how you represent your range may help, but if you need to iterate across a range this large, there's no getting away from the fact that it's going to take a while.
Assuming you have a 3GHz processor, and that you just need one cycle for each entry in the range, you would need ~3,333 seconds (~1 hour) to process it.
This leads me to think that there's somehting fundamentally wrong with what you're doing. Maybe you should restructure your problem.
I'm looking at the Double Metaphone algorithm. I just want to calculate the efficiency class that the algorithm falls under. Here it is in Python. This is the most readable code I could find.
I'm not great an analyzing algorithms' times, but I know that the while loop in this algorithm should take the most time. In this case, the program looks at each character in a string from left to right:
while pos <= last :
I would assume that this will take n steps, where n is the length of the string.
However, the algorithm has a LOT of 'if' and 'elif' statements inside of this loop; I don't know if they would significantly affect the time. Could anyone step me through this? I'd like to work out the time efficiency with summations.
To attempt to answer my own question, I think the best efficiency of it would be a string of length 1, where it has to enter as few 'if' statements as possible. On the other hand, the worst case scenario is probably just an extremely long string.
Thank you!
Without looking at every condition, this looks linear to me. A simple rule with some exceptions below is that if/then statements don't affect the asymptotic runtime of the algorithm unless they do some sort of looping or recursion. Everything I saw looks like constant time operations.
Here's how I would approach it if I really needed to be sure. If it's not straightforwardly dependent on the condition statement, you need to look for a few things
break statements and other things that alter the flow of control - this might lessen asymptotic time
Irregular modifications of the variable in the while or for statement, this might make the algorithm take more time. Here, this would mean either pos or last get modified.
This isn't a comprehensive list, but it might aid you.