HankerRank Problem: Unable to get a query from the array [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 2 days ago.
Improve this question
def compute_scores(history, queries):
scores = []
for q in queries:
score = helper(history, q)
scores.append(score)
return scores
def helper(history, query, level = 1):
points = 10
for match in history:
if match[0] == query:
if len(match) == 1:
points = points
else:
for opponent in match[1:]:
opponent_score = helper(history, opponent, level)
points += int(0.2 * level * opponent_score)
level += 1
return points
history = [[1], [2, 5, 4], [3], [4], [5, 3, 1]]
queries = [2, 1, 5]
print(compute_scores(history, queries))
It passes the first test and produces the following output in a array: 17 10 16. It failed the other test cases. Not sure why. Please help!

Related

getting the total of 3rd items on a list [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
I'm trying to write a program that adds only the third item in a list with dictionaries. Please help me. Here is my code so far.
# SalesManager Code
# pseudo sales list
sales = {"Meat Loaf": [50, 69, 19],
"Mineral Water": [5, 15, 10]
}
for key, value in sales.items():
total = total + value[2]
print(total)
Define total outside the loop:
total = 0
for key, value in sales.items():
total = total + value[2]
print(total)
print(total) # 29
you can use below one liner code to achieve this:
print(sum(v[2] for v in sales.values()))
You first need to declare your variable total:
sales = {"Meat Loaf": [50, 69, 19],
"Mineral Water": [5, 15, 10]
}
total = 0
for key, value in sales.items():
total = total + value[2]
print(total)

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.

How can I do this effectively? [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
This problem:
Input: 123456
Result:
1+2+3+4+5+6 = 21
2+1 = 3
Return: 3
This is my code:
num = input()
print(sum(list(map(int, list(num)))))
I don't know how to do until it is 1 digit.
Try this (example in IPython):
In [1]: s = '123456'
Out[1]: '123456'
In [2]: digits = [int(j) for j in s]
Out[2]: [1, 2, 3, 4, 5, 6]
In [3]: s = str(sum(digits))
Out[3]: '21'
Repeat steps 2 and three until len(s) == 1.
One way:
while len(str(ans))>1:
ans = sum(map(int, str(ans)))
Full code:
num = 45637
ans = num
while len(str(ans))>1:
ans = sum(map(int, str(ans)))
print(ans)
Output for input 45637:
7
You can try this:
s = input()
while(len(s)>1):
s = str(sum(list(map(int,s))))
One way to do it using sum(), list comprehension and recursion,
def simulated_sum(input):
"""This is a recursive function
to find the simulated sum of an integer"""
if len(str(input)) == 1:
return input
else:
input_digits = [int(x) for x in str(input)]
latest_sum = sum(input_digits)
return simulated_sum(latest_sum)
input = int(input('Enter a number'))
print(simulated_sum(input))
DEMO: https://rextester.com/WCBXIL71483
Is this what you want? (instructions unclear):
def myfunction(number):
total = 0
answertotal = 0
for i in str(number):
total += int(i)
for i in str(total):
answertotal += int(i)
return answertotal
myfunction(123456)
This function returns 3

Python code for deleting a list is not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
The following function should delete a element in an list if it has an overlap with a first list.
However it only works with the first example(a and b1). With others it does not even send an error message and I have no idea where there problem lies. Can someone point me in the right direction?
def funct(firstone, secondone):
counter = 0
while secondone != [] and counter < len(firstone):
if firstone[counter] in secondone:
del(secondone[secondone.index(firstone[counter])])
counter += 1
return secondone
a = [0, 1, 2]
b1 = [1, 2, 0]
b2 = [-1, 1, 1]
b3 = [0, 0, 2]
print(funct(a, b1))
print(funct(a, b2))
print(funct(b3, a))
i think last line in your code "return" must be in the same level with "while" loop like that
def funct(firstone, secondone ):
counter = 0
while secondone != [] and counter < len(firstone):
if firstone [counter] in secondone :
del(secondone[ secondone .index(firstone[counter ])])
counter += 1
return secondone
You need to continue the for loop when the condition is False otherwise you will always return on the first iteration
while secondone != [] and counter < len(firstone):
if firstone[counter] in secondone :
del(secondone[secondone.index(firstone[counter])])
counter += 1
else:
continue
return secondone

Categories

Resources