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 5 days ago.
Improve this question
I´m trying to solve this problem from the Qualification Round of the contest. It successfully solves the sample, but I receive a WA (Wrong Answer) for the cases. Can anyone figure out what is wrong with my code?
I tried my best looked solutions on google, but I could not find any difference between my code and the code of other contestants.
offline = True
if(offline):
f = open("Input")
f = f.read()
f = f.split("\n")
numCases = int(f[0])
readingLine = 1
else:
numCases = int(input())
myFun = []
IPointTo = []
pointsToMe = [[] for _ in range(1)]
def greater(a,b):
if(a>b): return a
return b
def fun(i):
#Returns a pair [fun, max]
#fun = a + b
# a = fun is the sum of the fun() of all children, except the one with the lowest max
# b = greater(fun of myself, max of the child with the lowest max)
# "max" is the value greater(myfun, max of the child with lowest max)
global myFun,IPointTo,pointsToMe
if (pointsToMe[i].__len__() == 0): return [myFun[i],myFun[i]]
a = 0
for j in range(pointsToMe[i].__len__()):
child = pointsToMe[i][j]
aux = fun(child)
a += aux[0]
if(j==0 or aux[1] < lowestMax):
funOfTheChildWithLowestMax = aux[0]
lowestMax = aux[1]
a -= funOfTheChildWithLowestMax
b = greater(myFun[i],lowestMax)
return [a+b,greater(myFun[i],lowestMax)]
for case in range(numCases):
myFun = []
IPointTo = []
for i in range(3):
if(offline):
line = f[readingLine]
readingLine += 1
else:
line = input()
if(i==0):
N = int(line)
if(i==1):
line = line.split(" ")
for x in line: myFun.append(int(x))
if(i==2):
line = line.split(" ")
for x in line: IPointTo.append(int(x)-1)
pointsToMe = [[] for _ in range(N)]
for i in range(N):
if(IPointTo[i] != -1): pointsToMe[IPointTo[i]].append(i)
sum = 0
for i in range(N):
if(IPointTo[i] == -1):
aux = fun(i)
sum += aux[0]
print("Case #{}: {}".format(case + 1, sum))
I'm making a quiz in Python where a user can add himself the questions and answers and specify the correct one.
I already did this
# Do you want to add another question version
from Question import Question
question_prompt = []
answer_list = []
def add_Question():
question = input('Add your question: ')
print(question)
test = int(input('If your question is correct. Enter 1 else enter 2: '))
if test == 1:
print('Type the answers propositions: ')
a = input('(a): ')
b = input('(b): ')
c = input('(c): ')
d = input('(d): ')
# print(a + '\n' + b + '\n' + c + '\n' + d )
answer = input('Which one is the correct answer? (a) (b) (c) or (d). Just type the letter ie. a: ').lower()
question_insert = f"{question} \n {a} \n {b} \n {c} \n {d}\n\n"
question_prompt.append(question_insert)
answer_list.append(answer)
listing = tuple(zip(question_prompt, answer_list))
questions = [Question(question_prompt[i],answer_list[i]) for i in range(0, len(listing))]
else :
question = input('add your question: ')
print(question)
test = int(input('if your question is correct. Enter 1 else enter 2: '))
return question, a,b,c,d,answer
def insert_question_prompt() :
again = int(input("Do you want to add another question. 1 = 'yes' and 2 = 'no' "))
while again == 1:
add_Question()
again = int(input("Do you want to add another question. 1 = 'yes' and 2 = 'no' "))
print(question_prompt)
def run_test(questions):
score = 0
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score += 1
print("Hey You got " + str(score) + "out of " + str(len(questions)) + "questions correct")
# questions = [Question(question_prompt[i],answer_list[i]) for i in range(0, len(listing))]
add_Question()
insert_question_prompt()
print(question_prompt)
print(answer_list)
In the function def insert_question_prompt() : I ask the user if he wants to insert another question and repeat the add_Question function.
The run_test function is to test the quiz. Play and access it
But I am unable to access question_prompt and answer_list in the run_test function even though they are not empty.
Basically, I'm blocked at level where the user can test the quiz ie Play and have the score.
Thanks for your help
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Im trying to put in valid numbers into the board and meet all the conditions for the game.
The code doesnt run for some reason.
board = [
[0,0,6,9,0,5,0,1,0],
[9,7,0,0,1,2,3,0,5],
[0,2,0,0,0,4,8,6,0],
[5,0,3,8,0,0,0,2,0],
[0,0,0,0,0,0,0,0,0],
[0,8,0,0,0,1,9,0,7],
[0,5,4,1,0,0,0,7,0],
[2,0,7,4,5,0,0,9,3],
[0,6,0,7,0,3,1,0,0]
]
#cheking that the 3x3 squares dont have sum of 45, game continues if False
def check_conditions():
a = board[0][0:3] + board[1][0:3] + board[2][0:3]
b = board[3][0:3] + board[4][0:3] + board[5][0:3]
c = board[6][0:3] + board[7][0:3] + board[8][0:3]
d = board[0][3:6] + board[1][1:6] + board[2][1:6]
e = board[3][3:6] + board[4][1:6] + board[5][1:6]
f = board[6][3:6] + board[7][1:6] + board[8][1:6]
g = board[0][6:9] + board[1][6:9] + board[2][6:9]
h = board[3][6:9] + board[4][6:9] + board[5][6:9]
i = board[6][6:9] + board[7][6:9] + board[8][6:9]
all_squares = [a,b,c,d,e,f,g,h,i]
for i in all_squares:
if sum(i) != 45:
return False
#checking for duplicate in row, this function is under check_conditions()
for i in board:
if len(i) != len(set(i)):
return False
#checking that there no zeros in the board
def zero_condition():
for i in board:
for j in i:
if j == 0:
return False
#checking for duplicates in column
def get_column(col, board):
column = []
for i in range(len(board)):
column.append(board[i][col])
return column
all_column = [get_column(0, board), get_column(1, board), get_column(2, board), get_column(3, board), get_column(4, board), get_column(5, board), get_column(6, board), get_column(7, board), get_column(8, board)]
def check_column():
for i in all_column:
if len(i) != len(set(i)):
return False
#putting numbers into chosen rows and column
def choose_cell():
row = int(input("choose row:"))
column = int(input("choose column:"))
choose_num = int(input("choose a num between 1 og 9:"))
if 0< choose_num <=9:
board[row][column] = choose_num
else:
print("illegal num try again")
choose_cell()
#this is where the game starts
def begin_soduko():
answer= input("play soduko?")
if answer == "yes":
while zero_condition() == False and check_conditions == False and check_column() == False:
choose_cell()
print("you have finished soduko!!!")
I copy pasted all your code into a script and attempted to run it. If this is all your code, the first thing you are missing is a call to "begin_sudoku()" before your finishing print statement.
Afterwards, I ran it and was able to be asked whether to play, but entering "yes" still ended the game despite all three conditions being false. I found the last reason your game isn't running is because you are forgetting the parenthesis on "check_conditions()" when checking conditions in the while loop. I added the () to the end of it along with the call to begin_sudoku() and the game ran fine for me.
def begin_soduko():
answer= input("play soduko?")
if answer == "yes":
while zero_condition() == False and check_conditions() == False and check_column() == False:
choose_cell()
begin_soduko()
print("you have finished soduko!!!")
Replace your last code block with this and it should finally run.
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 2 years ago.
Improve this question
import pyperclip
#Cesar Chiper
#The String to be encrypt or decrypted
message = "This is my secret message"
#The encryption /decryption key
key = 13
#Tell the program to encrypt/decrypt
mode = "encrypt"
#Every possible sympol to encrypt or decrypt from the message
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#Stores message
translated=""
message = message.upper()
#run the encryption/decryption code in each symbol in the message string
for symbol in message:
if symbol in letters:
#get the encypted or deycrypted number for this symbol
num = letters.find(symbol)
if mode == "encrypt":
num = num + key
elif mode == "decrypt":
num = num - key
#handle the wrap around if num is larger than lenght of letters or less than 0
if num >= len(letters):
num = num - len(letters)
elif num < 0:
num = num + len(letters)
#ad encrypted/decrypted number symbol at the end of translted
translated = translated + letters[num]
else:
#Just add the symbol without encrypting/decrypting
translated = translated + symbol
#print the encrypted/decrypted to screen
print(translated)
#copy the encrypted/decrypted string to clipoard
pyperclip.copy(translated)
Hei. I see that no problem in your code but the indent.
First. The code wit comment #handle the wrap around if num is larger than lenght of letters or less than 0 have to remove one indent(these must take in the same block with 2 desicions before). The translated = translated + letters[num] have to put at the same block with previous desicion
Second. The last else statement have to put at the same block with first if(if symbol in letters)
Here it is:
import pyperclip
message = "This is my secret message"
key = 13
mode = "encrypt"
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
translated=""
message = message.upper()
for symbol in message:
if symbol in letters:
num = letters.find(symbol)
if mode == "encrypt":
num = num + key
elif mode == "decrypt":
num = num - key
if num >= len(letters):
num = num - len(letters)
elif num < 0:
num = num + len(letters)
translated = translated + letters[num]
else:
translated = translated + symbol
print(translated)
pyperclip.copy(translated)
The Output will be: GUVF VF ZL FRPERG ZRFFNTR
(Sorry bad english)
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have implemented the following code which works perfectly without any problem. But I am not satisfied with it because it doesn't look pretty? More than anything I feel like it doesn't look like pythonic way to do it.
So I thought I ll take suggestions from the stackoverflow community. This metod is getting its data from sql query which is in another method that method return a dictionary and based the data in that dictionary I am doing pattern match and counting process. I would like to do this in a pythonic way and return a better data structure.
Here is the code:
def getLaguageUserCount(self):
bots = self.getBotUsers()
user_template_dic = self.getEnglishTemplateUsers()
print user_template_dic
user_by_language = {}
en1Users = []
en2Users = []
en3Users=[]
en3Users=[]
en4Users=[]
en5Users=[]
en_N_Users=[]
en1 = 0
en2 = 0
en3 = 0
en4 = 0
en5 = 0
enN = 0
lang_regx = re.compile(r'User_en-([1-5n])', re.M|re.I)
for userId, langCode in user_template_dic.iteritems():
if userId not in bots:
print 'printing key value'
for item in langCode:
item = item.replace('--','-')
match_lang_obj = lang_regx.match(item)
if match_lang_obj is not None:
if match_lang_obj.group(1) == '1':
en1 += 1
en1Users.append(userId)
if match_lang_obj.group(1) == '2':
en2 += 1
en2Users.append(userId)
if match_lang_obj.group(1) == '3':
en3 += 1
en3Users.append(userId)
if match_lang_obj.group(1) == '4':
en4 += 1
en4Users.append(userId)
if match_lang_obj.group(1) == '5':
en5 += 1
en5Users.append(userId)
if match_lang_obj.group(1) == 'N':
enN += 1
en_N_Users.append(userId)
else:
print "Group didn't match our regex: " + item
else:
print userId + ' is a bot'
language_count = {}
user_by_language['en-1-users'] = en1Users
user_by_language['en-2-users'] = en2Users
user_by_language['en-3-users'] = en3Users
user_by_language['en-4-users'] = en4Users
user_by_language['en-5-users'] = en5Users
user_by_language['en-N-users'] = en_N_Users
user_by_language['en-1'] = en1
user_by_language['en-2'] = en2
user_by_language['en-3'] = en3
user_by_language['en-4'] = en4
user_by_language['en-5'] = en5
user_by_language['en-n'] = enN
return user_by_language
You can avoid all these lists and add the data directly to the user_by_language dict.
I would define it as:
user_by_language = collections.defaultdict(list)
After matching the regex, just do this:
user_by_language['en-%s-users' % match_lang_obj.group(1)].append(userId)
In the end, you grab all the lengths of these elements and save them as en-1, en-2...