Project Euler 55 - Answer Only Off By 3 (strange!) [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was solving this problem (found at http://projecteuler.net/problem=55) and I could not get it right so I searched for the answer.
It seems the answer my code is giving is only off by 3 (mine: 246 , right: 249).
It would be nice for someone to to spot the mistake. I have been trying for 3 hours now...
Here is my code:
from time import time
def rev(x):
return int(str(x)[::-1])
def Palindrome(x):
if x == rev(x): return True
else : return False
def test(x):
steps = 0
while True :
if not Palindrome(x):
steps += 1
else:
return False
if steps > 50 :
return True
x += rev(x)
def main():
starttime = time()
lychrel = 0
for i in range(1,10000):
if test(i) : lychrel += 1
elapsed = time() - starttime
print "The answer is %d found in %f seconds" %(lychrel,elapsed)
if __name__ == "__main__":
main()
Thanks in advance!

From the description at http://projecteuler.net/problem=55
Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.
Your code does not recognize 4994 as a Lychrel number.

Related

can you explain how this Loop works and how do we get that output? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
if __name__ == '__main__':
n = int(input())
for i in range (0,n):
result = i**2
print(result)
#input : 5
#output : 0 1 4 9 16
range 0,n = 0,1,2,3,4 and
we gave i**2 but how we got 0,1,4,9,16 as output?
range(start, stop, step)
** = square of Number
start Optional. An integer number specifying at which position to
start. Default is 0
stop Required. An integer number specifying at which position to
stop (not included).
step Optional. An integer number specifying the incrementation. Default is 1
you are passing required parameter as 5 which will not be included in the loop. so as per your calculation it will start from 0
result = 0**2 = 0
result = 1**2 = 1
result = 2**2 = 4
result = 3**2 = 9
result = 4**2 = 16
'i' will not reach 5 because of non-inclusive nature of range() operator.

dunno what i do, splitting number like lul [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
the desired output
splitnumber(-9)----->-5,-4
my code
def spliiiiit(angka):
hasil1 = angka/2
hasil1 = int(hasil1)
batas = angka%2
hasil2 = 0
if batas == 0:
hasil2 = hasil1
elif angka < 0:
hasil1 = hasil1 - 1
hasil2 = hasil1 + 1
else:
hasil2 = (angka/2)+0.5
hasilakhir = []
hasilakhir.insert(0,hasil1)
hasilakhir.insert(1, int(hasil2))
print(hasilakhir)
can some one tell me how the right way to solve that
the function output is like this
splitnumber(-9) ----> [-5,-4] or splitnumber(9) ----> [4,5]
def spliiiiit(angka):
x = angka//2
return(x, angka-x)

In Python, how can I make it so the first 2 elements of a list are added together, then the first 4, then the first 6, and so on? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am making a game where two players take turns adding a number 1 to 10 to the running total. The player who adds to get 100 wins. I stored the inputs of the players in a list, and at the end of the game, I want to display the total after every turn. The list is in the format:
allTurns = [player1move1, player2move1, player1move2, player2move2, ...]
How can I add only the first 2 elements and display them, then add only the first 4 elements and display them, and then the first 6, and so on?
for i in range(0, len(list)):
if i % 2 == 0:
print(sum(list[:i+1]))
Or
sums = []
for i in range(0, len(list)):
if i % 2 == 0:
sums.append(sums[-1] + list[i-1] + list[i])
Try this:
allTurns = [1,2,3,6,12,23]
out = []
for n in range(len(allTurns)):
if n % 2 is 1:
out.append(allTurns[n] + allTurns[n-1])
if len(allTurns) % 2 is 1:
out.append(allTurns[-1])
print(out)
I've coded it so that it could work for any situation, and not just yours.
Try this:
allTurns = [1,2,3,6,12,23,4]
out = []
for n in allTurns:
if allTurns.index(n) % 2 is 0:
i = 0
for a in range(allTurns.index(n)+2):
try:
i=i+allTurns[a]
except IndexError:
pass
out.append(i)
print(out)
This is probably as simple as it can get.

Extract n first consecutive numbers from a string in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Let's consider this string :
st = 'text1text6253text'
How could I please extract the two first consecutive figures ?
Expected output :
62
You can either use regex with \d{2}and return that, or go over the string:
st = 'text1text6253text'
for i in range(len(st)-1):
if st[i].isdigit() and st[i+1].isdigit():
print(st[i]+st[i+1])
break
import re
def find_con(n, s):
result = re.search('\d{%s}'%n, s)
return result.group(0) if result else result
st = 'text1text6253text'
print(find_con(2, st))
st = 'text1text6253text'
lst = list(st)
lst2 = []
for i,v in enumerate(lst):
if lst[i].isdigit() and lst[i+1].isdigit():
lst2.append(lst[i])
lst2.append(lst[i+1])
ans = int(lst2[0] + lst2[1])
print(ans)
Thanks to your answers, I built a general function that I propose you below :
def extract_n_consecutive_numbers(st,nb):
for i in range(len(st)-nb+1):
is_numeric = True
for j in range(nb):
is_numeric = is_numeric & st[i+j].isdigit()
if is_numeric :
output = ""
for j in range(nb):
output += st[i+j]
return output
return ""
Example :
extract_n_consecutive_numbers('text1text6253text',2)
Out[1]: 62

is there possible to resolve this star pyramid [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Today, i have trying to resolve a small star pyramid :
Input:
5 1
Output:
*
**
***
****
Code:
x = 1
y = 0
m, e = map(int, raw_input().split())
while x < m:
print "\n" * y, "*" * e
m -= 1
e += 1
I did that but there is a better solution?? Thanks =)
I think this can be solved more easily:
stop, first = map(int, raw_input().split())
for i in range(stop - 1):
print '*' * (i + first)
just for fun >:)
class c:
def __init__(s,m,e):
s.e , s.m = sorted([e, m])
s.r = 42
def __iter__(s):
return s
def next(s):
if s.m < s.e:
t = "".join(chr(s.r) for _ in range(s.m))
s.m += 1
return t
else:
raise StopIteration
print "\n".join(c(*map(int,raw_input().split())))
n = int(raw_input())
for i in range(n): print "*"*i
This appears to do what your program intends to do, however I can't quite tell because of the issues I raised in my comment above.

Categories

Resources