This code is for the Google codejam competition. The below code is compiling correctly on my PC and gives the correct result for the sample code. However it is showing runtime error whenever I try to run it on the google website. I have been messing with it for one hour and still have no idea what's wrong with it.
def reversort(reverList):
global totalScore
length = len(reverList)
score = 0
for i in range(length - 1):
minimum = reverList.index(min(reverList[i: ]))
tempList = reverList[i:minimum + 1]
tempList.reverse()
reverList[i: minimum + 1] = tempList
score += minimum - i + 1
totalScore.append(score)
if __name__ == "__main__":
t = int(input())
totalScore = []
rev = []
for i in range(t):
n = int(input())
apen = []
for j in range(n):
apen.append(int(input()))
reversort(apen)
print("Case #{}: {}".format(i+1,totalScore[i]))
rev.append(apen)
try this
reverse = int(input())
for i in range(1, reverse + 1):
a = int(input())
b = list(map(int, input().split()))
out = 0
for index in range(a-1):
min_index = b.index(min(b[index:a]))
b[index: min_index + 1] = reversed(b[index: min_index + 1])
out += (min_index) - (index) + 1
print("Case #{}: {}".format(i, out))
Related
The code works, but at the same time it outputs a string in which the same value can be reversed. Therefore, it gives an error in tests
Here is the code itself:
def order_weight(strng):
weight = strng.split(' ')
t_weight = weight.copy()
answ = ''
t_arr = []
for i in range(len(weight)):
t_min = 2**64
t_index = 0
t_num = 0
for i, num in enumerate(t_weight):
t_sum = 0
for j in num:
t_sum += int(j)
if t_sum <= t_min:
t_min = t_sum
t_index = i
t_num = num
t_arr.append(t_num)
t_weight.pop(t_index)
answ = ' '.join(t_arr)
return answ
enter image description here
Find it's harder to follow your logic, but this will be my simple approach:
Please read and compare with yours. It's passed all tests too.
def count_weight(s):
return (sum(int(c) for c in s), s)
def order_weight(s):
return ' '.join(sorted(s.split(' '), key=count_weight)) # use the *weight* as the sort_key here
I was practicing on google kick start Round A 2016 ( Country Leader ) but iam getting error that says Runtime error and can't figure what is wrong.
here is my code
First one :
T = int(input().strip())
tries = []
for i in range(1, T + 1):
N = int(input().strip())
persons = list()
for t in range(1, N + 1):
persons.append(input().strip())
tries.append(persons)
winners = []
for t in tries:
points = 0
temp = ''
for per in t:
per_ltr = per.replace(' ','')
if len(set(per_ltr)) > points:
points = len(set(per_ltr))
temp = per
winners.append(temp)
num = 1
for one in winners:
print(f'Case #{num}: {one}')
num += 1
another one :
T = int(input())
winner = []
for i in range(T):
N = int(input())
persons = []
for j in range(N):
persons.append(input())
points = len(set(persons[0]))
temp = persons[0]
for per in persons:
if len(set(per)) > points:
points = len(set(per))
temp = per
winner.append(temp)
num = 1
for one in winner:
print(f'Case #{num}: {one}')
num += 1
my answer for finding the number of combinations given drawing 3 cards out of 52 is off by a spot. Like 0 cards from 52 should = 1, 1 should = 52, 2 = 1326 and so on. but I have 0 = 1, 1 = 1, 2 = 52 and so on. What would I modify to reach the desired result? I think the error is in def factorial() but I can not seem to fix/ find the issue, no matter what I try.
def factorial(num):
i = 2
if num == 0:
num = 1
print(num)
elif num > 1:
for i in range(i, num):
num = num * i
return num
def combinations(n,r):
l = n-r
nn = factorial(n)
rn = factorial(r)
ln = factorial(l)
result = nn / (rn * ln)
print(result)
return result
def main():
h = 52
a = 0
while a<4:
combinations(h,a)
a = a + 1
You're printing extra stuff in factorial which may lead to the confusion. I suggest you print out your final result with comparison to your a variable at the end of the combinations function like so:
print("For a=" + str(r) + ", result=" + str(result))
Here is the overall edited code:
def factorial(num):
if num == 0:
num = 1
elif num > 1:
for i in range(2, num): # Setting i=2 at the start is redundant
num = num * i
return num
def combinations(n,r):
l = n-r
nn = factorial(n)
rn = factorial(r)
ln = factorial(l)
result = nn / (rn*ln)
print("For a=" + str(r) + ", result=" + str(result))
return
h = 52
a = 0
while a<4:
combinations(h,a)
a = a + 1
I'm a Python beginner. I'm trying to solve the 3n+1 problem on UVa Online Judge. The program worked fine with the input files. However, I submitted several times but still got Runtime error.
import sys
def compute(i, j):
maxCycleLength = 1
if i <= j/2:
k = j/2+1
else:
k = i
for n in range(k, j+1):
num = n
cycleLength = 1
while (num != 1):
if num%2 != 0:
num = 3*num+1
else:
num = num/2
cycleLength += 1
if cycleLength > maxCycleLength:
maxCycleLength = cycleLength
return maxCycleLength
while True:
nums = sorted(int(x) for x in next(sys.stdin).split())
m = compute(nums[0], nums[1])
print("{} {} {}\n".format(nums[0], nums[1], m))
This is the part of my code which doesn't work. It says "there's an error in your code: invalid syntax."
edit: this is the part of code that is broken: for i in range(0, len(marks)):
def histogram(data):
def getFrequency(marks):
freqList = []
for i in range(0,101):
freqList.append(0)
for i in range(0, 101):
starsList = []
for i in range(0, data[i]):
starsList.append("*")
pad
if data[i] < 10:
pad = " "
elif data[i] < 100:
stars = "" .join(starsList)
print("%d | %s %d" %(i, starsList)
for i in range(0, len(marks)):
mark = marks[i]
freqList[mark] += 1
return freqList
freq = getFrequency(marks)
mode = maximum(freq)
#print (freq)
this is the rest of the code (which is above the part with the error). It may or may not be riddled with errors. I put it here in case it is relevant.
import random
def bubbleSort(data):
count = 0
for i in range(0,len(data) - 1):
for j in range(0, len(data) - 1):
count += 1
if data[j] > data[j+1]:
#swap
temp = data[j]
data[j] = data[j + 1]
data[j + 1] = temp
print(count)
return data
data = [5,4,3,2,1]
data = bubbleSort(data)
print(data)
def getData():
data = []
for i in range(0, 100):
data.append(random.randint(0,100))
return data
def mean(data):
total = 0
for i in range (0, len(data)):
#add data[i]
total = total + data[i]
return total/ len(data)
def maximum(data):
maximum = data[0]
for i in range(0, len(data)):
if maximum < data[i]:
maximum = data[i]
return maximum
def minimum(data):
minimum = data[0]
for i in range(0, len(data)):
if minimum > data[i]:
minimum = data[i]
return minimum
#def mode(data):
marks = getData()
You're simply missing a right parenthesis here:
print("%d | %s %d" %(i, starsList)
You have to add a parenthesis in print line, inside for cicle:
for i in range(0, len(marks)):
....
print("%d | %s %d" %(i, starsList))