I need to create a program that takes in an array of numbers, check if each number is greater than the sum of all previous numbers, and output true if the condition is met, and false, if not. My trial is presented below:
import fileinput
a0 = [int(i) for i in fileinput.input()]
a = a0[1:]
b=[]
for i in range(1, a0[0]+1):
b.append(a[i])
if (a[i+1] > sum(b)):
print("true")
break
else:
print ("false")
break
My program works for half of the test cases but fails for the other test. Could you please help me figure out what i am doing wrongly. Many thanks for your assistance.
You are breaking too early in the true case. Only because the first element checks, doesn't mean all others will, too:
for i in range(1, a0[0]+1):
b.append(a[i])
if (a[i+1] <= sum(b)):
print ("false")
break
else: # for-else: else is executed only if loop isn't `break`ed out of
print("true")
Only once the loop has finished without finding a counter example, you can be sure that it holds for the entire list.
A more concise of writing this, would be:
import fileinput
_, *a = (int(i) for i in fileinput.input())
s = 0 # just keep track of total
for num in a:
if (num <= s):
print("false")
break
s += num
else:
print("true")
Related
I have some code which gives me the answer I want, but I'm having trouble stopping it once I get the
answer I want.
Here's my code:
multiples = range(1,10)
n = 1
while n>0:
for i in multiples:
if n%i!=0:
n = n + 1
continue
elif n%10==0:
print(n)
This is an attempt at solving problem 5 of Project Euler. Essentially, I'm supposed to get the smallest multiple of all the digits within a given range.
Now, when i run the above code using the example given (1-10 should yield 2520 as the smallest multiple), i get the answer right. However, the code continues to run infinitely and print the answer without breaking. Also, the moment I add the break statement to the end like so:
multiples = range(1,10)
n = 1
while n>0:
for i in multiples:
if n%i!=0:
n = n + 1
continue
elif n%10==0:
print(n)
break
The code just keeps spamming the number 30. Any ideas why this is happening. For the record, I'm not really looking for an alternative solution to this question (the goal is to learn after all), but those are welcome. What I want to know most of all is where I went wrong.
You never break out of your while loop. The for is the entire while body. break interrupts only the innermost loop; you have no mechanism to leave the while loop. Note that your continue doesn't do anything; it applies to the for loop, which is about to continue, anyway, since that's the last statement in the loop (in that control flow).
I can't really suggest a repair for this, since it's not clear how you expect this to solve the stated problem. In general, though, I think that you're a little confused: you use one loop to control n and the other to step through divisors, but you haven't properly tracked your algorithm to your code.
One way to deal with this is to have an exception. At best a custom one.
multiples = range(1,10)
n = 1
class MyBreak(Exception):
pass
while n>0:
try:
for i in multiples:
if n%i!=0:
n = n + 1
continue
elif n%10==0:
print(n)
raise MyBreak()
except MyBreak:
# now you are free :)
break
With this brake you stop only for loop, to exit whole cycle you should create trigger variable, for example:
multiples = range(1,10)
n = 1
tg = 0
while n>0:
for i in multiples:
if n%i!=0:
n = n + 1
continue
elif n%10==0:
print(n)
tg = 1
break
if tg != 0:
break
Or it'll be better to use a function and stop a cycle by return:
def func():
multiples = range(1,10)
n = 1
while n>0:
for i in multiples:
if n%i!=0:
n = n + 1
continue
elif n%10==0:
print(n)
return n
I'm new to python and I am trying to make a code to print all the square numbers until the square of the desired value entered by the user.
n = raw_input("Enter number")
a = 1
while a < n:
a = 1
print(a*a)
a += 1
if a > n:
break
When I run this code it infinitely prints "1" ... I'm guessing that the value of a does not increase by += so it's a=1 forever. How do I fix this?
There are some problems. First, your input (what raw_input() returns) is a string, so you must convert it to integer:
n = int(raw_input(...))
Second, you are setting a = 1 each iteration, so, since the loop condition is a < n, the loop will run forever ( if n > 1). You should delete the line
a = 1
Finally, it's not necesary to check if a > n, because the loop condition will handle it:
while a < n:
print a * a
a += 1
# 'if' is not necessary
There is a small error in your code:
while a < n:
a=1 # `a` is always 1 :)
print a*a
a += 1
if a > n:
break
You're setting the value of a back to 1 on every iteration of the loop, so every time it checks against n, the value is 2. Remove the a=1 line.
As others have noted, your specific problem is resetting a each time you loop. A much more Pythonic approach to this is the for loop:
for a in range(1, n):
print(a ** 2)
This means you don't have to manually increment a or decide when to break or otherwise exit a while loop, and is generally less prone to mistakes like resetting a.
Also, note that raw_input returns a string, you need to make it into an int:
n = int(raw_input("Enter number: "))
an even better idea is to make a simple function
def do_square(x):
return x*x
then just run a list comprehension on it
n = int(raw_input("Enter #:")) #this is your problem with the original code
#notice we made it into an integer
squares = [do_square(i) for i in range(1,n+1)]
this is a more pythonic way to do what you are trying to do
you really want to use functions to define functional blocks that are easy to digest and potentially can be reused
you can extend this concept and create a function to get input from the user and do some validation on it
def get_user_int():
#a function to make sure the user enters an integer > 0
while True:
try:
n = int(raw_input("Enter a number greater than zero:"))
except TypeError:
print "Error!! expecting a number!"
continue;
if n > 0:
return n
print "Error: Expecting a number greater than zero!"
and then you can build your input right into your list
squares = [do_square(i) for i in range(1,get_user_int()+1)]
and really do_square is such a simple function we could easily just do it in our loop
squares = [x*x for x in range(1,get_user_int())]
The first line in your loop sets's a to one on every iteration.
You assign a=1 inside the loop. That means it's overwriting the a+=1.
try this:
n = eval(raw_input("Enter number"))
a=1
while a < n:
print a*a
a += 1
The issue here is that the value of a gets overridden every time you enter in the loop
Problem is in the condition of the while loop, to print squares of numbers upto a limiting value, try this..
def powers(x):
n=1
while((n**2)<=x):
print(n**2, end =' ')
n +=1
So I'm trying to find the 10,001st prime #. Here's my code -
counter = 3
primes = [1]
while len(primes) < 10002:
for i in range(2, counter):
if counter % i == 0:
counter += 1
else:
primes.append(counter)
counter += 1
print counter
So what I get as an output in primes is a list of numbers, the first few numbers are 1, 3, 5, 7, 11... so far, so good... 13, 17, 19, 23, 27... wait, 27? So at that point it breaks down and starts returning mostly primes but not all primes. And it takes forever.
I'm new to programming, made it through CodeAcademy's Python course and now trying to figure out how to get past what was essentially just an introduction to the grammar. I don't come from a math background, so while I know what a prime is, I know there are far better ways to go about this. If there's anyone in a similar boat who wants to "partner up" and work together on learning Py2.7, I'm more than happy to.
I won't implement anything for you, since that's why you're doing Project Euler, but I will point you strongly in the direction of The Sieve of Eratosthenes. It will calculate in seconds what your code will do in hours.
It works as such: (in pseudocode)
for known_prime in a huge list of numbers:
k=2
while known_prime*k < the biggest number:
known_prime*k is not prime
k += 1
Once you've made it through sqrt of the list, you've found every prime number within the list.
Since you're doing a project Euler puzzle, you obviously want comments on your code rather than the solution.
Your code:
counter = 3
primes = [1]
while len(primes) < 10002:
for i in range(2, counter):
if counter % i == 0:
counter += 1
else: # Mis-aligned else (assuming it's intended for the if)
primes.append(counter)
counter += 1
print counter
Your else is mis-aligned with the if
Your for loop goes all the way to counter, and testing divisibility against itself is bound to find remainder 0.
Your else clause will hit for each non-factor of counter. Most numbers will not be a factor, so you don't want to take action in that case. Instead break out of the loop when the if part triggers.
After exiting the for loop, you'll want to check if a factor was found and if not, add counter to your list of primes.
Looks like you are trying to implement a naive un-optimized brute force search, and that is fine.
Maybe try and write out the algorithm in words or pseudo code before coding it.
As an exercise for you instead of writing for you a simple step by step code I will write a complex line of code with a solution to a similar problem.
Try to figure out what is going on here in this line as an exercise to understand python.
This code will find the prime numbers until n number not the first n prime numbers that you want
def primes(n):
return sorted(set(range(2, n+1)) - set([p*i for p in range(2, n+1) for i in range(p, n+1)]))
If you want to write an efficient and smart code, you may use the Sieve of Eratosthenes, a good algorithm for finding prime numbers in a given range.
For more information, read this:
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
import time
start_time = time.time()
def is_prime(limit):
aval = True
for j in range(2,int(limit**0.5)+1):
if limit == 2:
aval = True
break
elif limit % j == 0:
aval = False
break
return aval
counter = 0
for i in range(2,1000000):
if is_prime(i):
counter += 1
if counter == 10001:
print("the 10001th prime number is: ",i)
break
print("seconds: ", time.time() - start_time)
Here is my solution to this problem. It may not be so quick but it is not precisely for problem 7.
import time
start = time.time()
n = int(input('Please enter a number: '))
def nth_prime(n):
primes = [2]
x = 3
max_amount = int(input('How much would you like to go for?: '))
while True:
try:
while x < max_amount:
for i in primes:
if x % i == 0:
break
else:
primes.append(x)
x += 2
print(primes[n])
except IndexError:
max_amount = int(input("Please enter a greater number: "))
continue
else:
print(f('Good job. You\'ve found the {n+1}st prime number :) ')
break
nth_prime(n)
end = time.time()
print(str(float(end - start)) + " seconds")
I have the following code, and I am trying to find the largest prime factor of the number that is square rooted in variable a. When I do some print testing, it doesn't seem my iterator is increasing. The code just runs infinitely it seems. Any tips? Am I missing a point where this becomes an infinite loop?
import math
a = math.sqrt(600851475143)
primefactors = [0]
i=1
while i<=a:
if a%i == 0:
if i%2 != 0 and i%3 != 0 and i%4 != 0 and i%5 != 0 and i%6 != 0 and i%7 != 0 and i%8 != 0 and i%9 != 0:
primefactors.append(i)
print(primefactors)
i=i+1
edit: just realized my mistake of using a as both upper-bound and the test value. Thank you for all the answers!
a is 775146.099225, so a%i==0 is never true, so even if your iterator worked (and it is working for me), you won't get anything added to prime factors.
The first thing I would try is printing out your i variable at the end of the loop after you increment and make sure it increments.
Your programs should loop 775146 times. This could possible take some time. I would try it with a smaller number first and then see what happens.
I am trying to create a simple program to apply the statement of the Collatz Conjecture to an integer that the user can enter, I have:
def collatz(n):
print n,
if n % 2 ==0:
n = n / 2
elif n == 0:
Print "Collatz Conjecture true for" , 'n'
else:
n = n *3 + 1
input("\n\nInsert a positive integer:")
def collatz(n)
However it is saying there is a syntax error in the line:
Print "Collatz Conjecture true for" , 'n'
I can't see what mistake ther is in this line.
Also as I haven't been able to test it yet, does this look as though it will work ok?
Python is case sensitive. Use "print" not "Print".
Well, your syntax error is that python is case-sensitive, so you need print rather than Print.
But you've got more problems:
'n' prints the string n. I think what you want is n to print the value of the variable (or if not, then you can just make a single string "... true for n").
Finally (I think), in order to run the function collatz, you don't need the def; that's just for the definition.
More problems:
The stopping condition should be n == 1, not n == 0.
You have to recur or iterate, as is you're only making one step.
Check the input, make sure it really is a positive number.
def collatz_steps(n):
steps=0
if n==1:
return 0
else:
while n!=1:
if n%2==0:
n=n/2
steps+=1
else:
n = 3*n+1
steps+=1
return steps