Here is the question:
Let us calculate sum of digits, as earlier, but multiplying each digit by its position (counting from the left, starting from 1). For example, given the value 1776 we calculate such weighted sum of digits (let us call it "wsd") as:
wsd(1776) = 1 * 1 + 7 * 2 + 7 * 3 + 6 * 4 = 60
Here is my code:
digitlist = []
numlist = []
def splitdigit(number):
numlist = []
digitlist = []
numlist.append(number)
while number >= 1:
number = number/10
numlist.append(number)
del numlist[-1]
for ele in numlist:
digitlist.append(ele%10)
return digitlist
# digit part
# test if the split digit work here:
# print (splitdigit(1234)) it works
times = int(input())
raw = raw_input()
string = raw.split()
nlist = []
outbox = []
rout = 0
res = 0
n = 0
for item in string:
nlist.append(int(item))
# print (nlist) [it worked]
for element in nlist:
# check for split method : checked
# formula to make the digit work: n = len(out) | while(n>1): n=n-1
# rout=out[-n]*n res=res+rout(res=0)
n = len(splitdigit(element))
print (n)
res = 0
while n >= 1:
rout = (splitdigit(element)[(n*(-1))]) * n # I HAVEN"T CHECK THIS FORMULA OUT !!!
res = res + rout
n = n + 1
outbox.append(res)
print (outbox)
print(" ".join(str(x) for x in outbox))
And here is my running error:
> 3
9 15 1776
1
Traceback (most recent call last):
File "13.py", line 39, in <module>
rout = splitdigit(element)[(n*(-1))] * n # I HAVEN"T CHECK THIS FORMULA OUT !!!
IndexError: list index out of range
and I checked it in interactive python. I think I am not asking for a item out of range but it gives me this error. I hope someone can help me out. Thank you, love you all.
You are thinking way too complicated.
def wsd(number):
digits = [int(i) for i in str(number)]
result = 0
for index, value in enumerate(digits):
result += (index + 1) * value
return result
print(wsd(1776))
Output:
60
Related
I'm trying to solve the Hackerrank Project Euler Problem #14 (Longest Collatz sequence) using Python 3. Following is my implementation.
cache_limit = 5000001
lookup = [0] * cache_limit
lookup[1] = 1
def collatz(num):
if num == 1:
return 1
elif num % 2 == 0:
return num >> 1
else:
return (3 * num) + 1
def compute(start):
global cache_limit
global lookup
cur = start
count = 1
while cur > 1:
count += 1
if cur < cache_limit:
retrieved_count = lookup[cur]
if retrieved_count > 0:
count = count + retrieved_count - 2
break
else:
cur = collatz(cur)
else:
cur = collatz(cur)
if start < cache_limit:
lookup[start] = count
return count
def main(tc):
test_cases = [int(input()) for _ in range(tc)]
bound = max(test_cases)
results = [0] * (bound + 1)
start = 1
maxCount = 1
for i in range(1, bound + 1):
count = compute(i)
if count >= maxCount:
maxCount = count
start = i
results[i] = start
for tc in test_cases:
print(results[tc])
if __name__ == "__main__":
tc = int(input())
main(tc)
There are 12 test cases. The above implementation passes till test case #8 but fails for test cases #9 through #12 with the following reason.
Terminated due to timeout
I'm stuck with this for a while now. Not sure what else can be done here.
What else can be optimized here so that I stop getting timed out?
Any help will be appreciated :)
Note: Using the above implementation, I'm able to solve the actual Project Euler Problem #14. It is giving timeout only for those 4 test cases in hackerrank.
Yes, there are things you can do to your code to optimize it. But I think, more importantly, there is a mathematical observation you need to consider which is at the heart of the problem:
whenever n is odd, then 3 * n + 1 is always even.
Given this, one can always divide (3 * n + 1) by 2. And that saves one a fair bit of time...
Here is an improvement (it takes 1.6 seconds): there is no need to compute the sequence of every number. You can create a dictionary and store the number of the elements of a sequence. If a number that has appeared already comes up, the sequence is computed as dic[original_number] = dic[n] + count - 1. This saves a lot of time.
import time
start = time.time()
def main(n,dic):
'''Counts the elements of the sequence starting at n and finishing at 1'''
count = 1
original_number = n
while True:
if n < original_number:
dic[original_number] = dic[n] + count - 1 #-1 because when n < original_number, n is counted twice otherwise
break
if n == 1:
dic[original_number] = count
break
if (n % 2 == 0):
n = n/2
else:
n = 3*n + 1
count += 1
return dic
limit = 10**6
dic = {n:0 for n in range(1,limit+1)}
if __name__ == '__main__':
n = 1
while n < limit:
dic=main(n,dic)
n += 1
print('Longest chain: ', max(dic.values()))
print('Number that gives the longest chain: ', max(dic, key=dic.get))
end = time.time()
print('Time taken:', end-start)
The trick to solve this question is to compute the answers for only largest input and save the result as lookup for all smaller inputs rather than calculating for extreme upper bound.
Here is my implementation which passes all the Test Cases.(Python3)
MAX = int(5 * 1e6)
ans = [0]
steps = [0]*(MAX+1)
def solve(N):
if N < MAX+1:
if steps[N] != 0:
return steps[N]
if N == 1:
return 0
else:
if N % 2 != 0:
result = 1+ solve(3*N + 1) # This is recursion
else:
result = 1 + solve(N>>1) # This is recursion
if N < MAX+1:
steps[N]=result # This is memoization
return result
inputs = [int(input()) for _ in range(int(input()))]
largest = max(inputs)
mx = 0
collatz=1
for i in range(1,largest+1):
curr_count=solve(i)
if curr_count >= mx:
mx = curr_count
collatz = i
ans.append(collatz)
for _ in inputs:
print(ans[_])
this is my brute force take:
'
#counter
C = 0
N = 0
for i in range(1,1000001):
n = i
c = 0
while n != 1:
if n % 2 == 0:
_next = n/2
else:
_next= 3*n+1
c = c + 1
n = _next
if c > C:
C = c
N = i
print(N,C)
Here's my implementation(for the question specifically on Project Euler website):
num = 1
limit = int(input())
seq_list = []
while num < limit:
sequence_num = 0
n = num
if n == 1:
sequence_num = 1
else:
while n != 1:
if n % 2 == 0:
n = n / 2
sequence_num += 1
else:
n = 3 * n + 1
sequence_num += 1
sequence_num += 1
seq_list.append(sequence_num)
num += 1
k = seq_list.index(max(seq_list))
print(k + 1)
It is a sample problem for practice (here), and is not getting accepted due to it giving 'wrong answer'. It is compiling fine, but might be for different test inputs failing on submission.
I just request comment for the same, as hope that the issue must be small.
The problem statement is:
The program should accept first line of input as no. of strings, s.t. it is less than 10. And the next lines should contain one string each of length less than 100 characters. Need find occurrence of "gfg" in the strings.
If no occurrence is found, -1 should be returned.
#code
t = int(input())
if t > 10 or t<0:
exit()
arr = [[0] for i in range(t)]
total = [[-1] for i in range(t)]
for i in range(t):
arr[i] = [k for k in input().split()[:1]]
for j in arr[i]:
total[i] = j.count("gfg")
if total[i]==0: total[i]=-1
print (total[i])
t = int(input())
if t not in range(10):
exit()
else:
pass
total = []
for i in range(t):
line = input()[:100]
if line.count("gfg") == 0:
total.append(-1)
else:
total.append(line.count("gfg"))
print('\n'.join(map(str, total)))
SOLUTION FOR YOUR TASK:
t = int(input())
total = []
for i in range(1, t + 1):
line = input()
if len(line)<=100:
count = 0
for i in range(0, len(line) - 3 + 1):
if line[i:i + 3] == "gfg":
count += 1
if count != 0:
total.append(count)
else:
total.append(-1)
for i in total:
print (i)
NOTE: your submitting was failing because of special cases
For example:
in string gfgfg, your substring "gfg" occurres 2 times, and in that case you can't use the string count() method
how you can see ,here line[i:i + 3] I am moving index by index and checking next 3 values (because your substing "gfg" have length 3)
You are erasing the value of total[i] each time you iterate in the last loop :
>>> for j in arr[i]:
>>> total[i] = j.count("gfg")
>>> if total[i]==0:
>>> total[i]=-1
>>> print (total[i])
Do you want to count the occurences in each word or in the whole sentence ? Because then you just have to write :
>>> for i in range(t):
>>> n_occ = input().count("gfg")
>>> if n_occ != 0:
>>> total[i] = n_occ
If there's no occurence you don't have to do anything because the value in total is -1 already.
Also, write:
>>> total = [-1]*t
not:
>>> total = [[-1] for i in range(t)]
The code intends to print a frequency table for a random input discrete data. Here's the code :
from math import log10
from random import randint
N = int(input("Enter number of observations:\n"))
l = [ randint(1,100) for var in range (N) ]
print(l)
l.sort()
print(l)
k = 1 + (3.332*log10(N))
k1 = round(k)
print ("Number of intervals should be = ",k1)
x = N//k1 + 1
print("S.No\t\tIntervals\t\tFrequency")
c = 1 #count
while c <= k:
a = (c-1)*x
b = c*x
count = 0
for v in range(a,b) in l:
count += 1
print(c,"\t\t","{}-{}".format(a,b),"\t\t",count)
c += 1
This shows the above cited error, how to resolve this?
The issue is that range(a,b) sets up a list of integers from a to b-1. What you are asking for is for the code to go through l and pick out numbers matching those criteria, which looks instead like:
for v in l:
if ((v>=a) and (v<b)):
count += 1
If you really want to use range, and your data are going to stay integers, then it would look like:
for v in l:
if v in range(int(a),int(b)):
count += 1
Also
x = N//k1 + 1
should be
x = 100//k1 + 1
I have this function that creates an empty 2D list. getal X getal wide and high.
Though when i execute the code i get this error
Traceback (most recent call last):
line 49, in
bord = rooster(5, "<>>>>>>>>>>>>>>>>>>>>>>>>")
line 38, in rooster
r = r + 1
TypeError: can only concatenate list (not "int") to list
r is a counter in this code that stands for rows of the board.
k is also a counter that stands for columns of the board.
reeks is a string that gets split into characters.
The goal of the code is to make the board getal X getal wide and then inserting all characters from 'reeks' into their own separate slot.
def rooster(getal, reeks):
#vierkant = [['']*getal]* getal
vierkant = [[0 for r in range(getal)] for k in range(getal)]
r = 0
k = 0
reekslist = list(reeks)
while r < getal:
k = 0
while k < getal:
vierkant[r][k] = reekslist[k + r*getal]
k += 1
for r in vierkant:
print(r)
r = r + 1
bord = rooster(5, "<>>>>>>>>>>>>>>>>>>>>>>>>")
Change your for loop before the increment to something like this:
for v in vierkant:
print(v)
if you use r in the for loop above it get assigned to the new value which is a row in vierkant, which is a list.
Not sure if it's the best title. The explanation of what the program is suposed to do is below, my version only works with the first example but it doesn't work in the second when you get for example 1 1 3 1 1 2 because i can't figure out a good way to handle so much variations especially if K is bigger than 3 and the limit is 50. My version:
N, K, M = map(int, input().split())
niz = list(map(int, input().split()))
nizk = list(range(1, K+1))
izlazi = []
for r in range(0, M):
operacija = list(map(int, input().split()))
index = 0
if operacija[0] == 2:
nizkk = []
for z in range(0, len(nizk)):
if nizk[z] in niz:
continue
else:
izlazi.append(-1)
break
for p in range(0, N):
if niz[p] not in nizkk:
nizkk.append(niz[p])
nizkk.sort()
if nizkk == nizk:
index = p
izlazi.append(index+1)
break
else:
continue
else:
index, repl = map(int, operacija[1:])
niz[index - 1] = repl
print(izlazi)
In the first line of the input there should be N, K, M (1 <= N, M <= 100k, 1 <= K <= 50, you don't need to actually check this the numbers that are tested will always be in those ranges). In the second line of input you put a list of numbers which are the lenght of N you entered earlier. M is the number of operations you will do in the following lines of input. There can be 2 operations. If you enter 1 p v(p = index of number you want to replace, v the number you replace it with) or if you enter 2 it needs to find the shortest array of numbers defined by range(1, K+1) in the list of numbers you entered in line 2 and possibly changed with operation 1. If it doesn't exist it should output -1 if it does it should output lenght of numbers in the array you look in(numbers can be like 2, 1, 3 if you're looking for 1, 2, 3, also if you're looking for 1, 2, 3 etc and you have 2, 1, 1, 3 as the shortest one that is the solution and it's lenght is 4). Also the replacement operation doesnt count from 0 but from 1. So watch out when managing lists.
These are the examples you can input in the program ulaz = input, izlaz = ouput:
I have the following idea:
Min length sequence either starts from first element or does not contain first element and hence equals to min length of the same sequence without first element.
So we have recursion here.
For sequence [1,1,3,2,1,1] and [1,2,3] we will have:
Min length from start element [1,1,3,2,1,1] is 4
Min length from start element __[1,3,2,1,1] is 3
Min length from start element ____[3,2,1,1] is 3
Min length from start element ______[2,1,1] is -1
Can stop here.
Result is minimum for [4,3,3] = 3
You have already implemented the part for min length, if it starts from the first element. Need now extract it as a function and create a recursive function.
Some metacode:
function GetMinLength(seq)
{
minLengthFromFirstElement = GetMinLenthFromFirstElement(seq)
minLengthFromRest = GetMinLength(seq[1:]) //recusive call
return Min(minLengthFromFirstElement, minLengthFromRest )//-1 results should not count, add extra code to handle it
}
Unfortunately I don't know python, but I can provide code on F# in case you need it.
EDIT:
Try this:
N, K, M = map(int, input().split())
niz = list(map(int, input().split()))
nizk = list(range(1, K+1))
izlazi = []
def GetMinLengthStartingFromFirstElement(seq):
nizkk = []
for z in range(0, len(seq)):
if seq[z] in nizk:
continue
else:
return -1
for p in range(0, len(seq)):
if seq[p] not in nizkk:
nizkk.append(seq[p])
nizkk.sort()
if nizkk == nizk:
index = p
return index+1
else:
continue
return -1
def GetMinLength(seq):
if len(seq) == 0:
return -1
else:
curMinLength = GetMinLengthStartingFromFirstElement(seq)
if curMinLength == -1:
return -1
minLengthFromRest = GetMinLength(seq[1:])
if minLengthFromRest > -1:
return min(curMinLength,minLengthFromRest)
else:
return curMinLength;
for r in range(0, M):
operacija = list(map(int, input().split()))
index = 0
if operacija[0] == 2:
minLength = GetMinLength(niz)
izlazi.append(minLength)
else:
index, repl = map(int, operacija[1:])
niz[index - 1] = repl
print(izlazi)