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
For an online assignment to be submitted for a MOOC, I have written a python program that takes in text data (The name, roll numbers, grades, etc of a list of students) in a particular format and prints the output after processing the data. The output is printed from a list of tuples using a for loop to iterate through the list and using the str.join() function to join the elements in the tuples.
The list of tuples is sorted already and when I run it in the Spyder IDE console, it prints the output in the order that it appears in the list. But, when I submit in the window of the online judge and run it, the output is obtained in a random order and my answer doesn't get accepted. Can anyone please help? I am all ready to clarify further if my question isn't clear.
Thank you.
As #Epo rightly pointed out, I tested my input with alphabetically sorted data while the online judge used the same data sorted differently as input. Adding a "sorted()" function to the input did the job.
Related
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 last year.
Improve this question
enter image description here hi so like i have this notepad document full of words I want to use for my python code, but when I try to use random.choice() it only extracts certain letters instead of the whole word. what can I change to make it extract a whole word instead?
You're reading your file into one big string. It's choosing a random letter from that string. If you split that string into a list of words, you can then choose a word randomly from that list.
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 3 years ago.
Improve this question
I've been away from Python for awhile, please forgive the broad/basic question.
Let's say I have a list of items, which can be anything really.
I want to write a program that prints two of these at a time, in every possible combination of two, and lets the user choose a preference/winner. Then at the end print the full list with some sort of numerical preference value for each. I don't know enough about the mathematics of rank or preference to know what that number would even look like...
Basically I'm having writer's block right from the start here. Any advice on how to structure this or what I should be looking into?
A very simple way of doing it would look something like this:
initialize a scoring_list
for every element in a list
for every element in a list
while valid input:
ask the question via raw_input()
if input matches the first item
store winner in some sort of scoring list
tell program input is valid
else if input matches the second item
store winner in some sort of scoring list
tell program input is valid
else
tell program input is not valid and repeat question
do some math with the scoring_list (e.g. normalization)
print( scoring_list )
Obviously a lot of detail is left out intentionally, because it depends on programming style and objectives. For example, it may be better to do the loops via indices so that you can have a scoring list with indices that matches your original list. With this structure, you can be creative with how you are tracking the "ranks" with something more statistically rigorous than a straight count.
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 4 years ago.
Improve this question
I am trying to create a random order generator. I have a list of ten names and want to write a program that returns the same list in a random order.
Here is my code which I'm told has invalid syntax in line 11
.
I am trying to complete the problem using filtering and recursion to further my understanding of these tools.
random.shuffle() is what you are looking for.
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 6 years ago.
Improve this question
I am currently working on a script, and I need to insert a value for each key in a dictionary containing large-scale data
for key, value in dict.items():
score = somefunction(value)
value.append(score)
the dictionary has nearly 100 thousands keys so it takes a lot of time to compute..
My friend says "value.append" takes too much time.
If it does not work with value.append,
how can I improve this script to boost the speed for computation?
(I have searched on the internet and it seems that a lot of people use value.append under this situation)
Your answer is very much appreciated.
value.append is not slow by itself, in fact it has an amortized O(1) runtime.
The problem is in your algorithm itself, as far as I can tell, you have 100k different lists for which you compute a different score and update one by one. If all your lists are unique, I'm afraid there is no way to improve the speed, except for fundamentally changing the way you store the scores.
Regardless, I'm afraid your bottleneck is in your some_function with which you calculate the score, since iterating through 100k lists should be trivial. Once you start going over more lists though (in the million/billions), you might want to think of another way. Could you post the function here?
If your lists are identical, it might be a good idea to create one main list and save it to every key. Then, because every key references the same list, changing the list for one of the keys will change it for every other key.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
good I'm doing a program which calculates genetically that man inside a list is the fittest to survive but when people eliminate by the command. sends me this error pop can not concatenate 'str' and 'int' objects
how can fix this?
Somewhere in your Python script you have two values being added together, where one value is a string and the other value is an integer. This is an error. Possibly you meant to convert the integer to a string, and concatenate the two strings, or you meant to convert the string to an integer and add the two integers together.
It appears that you are trying to concatenate, add, a string and an integer together. This is a no go. Try using the command, i = int(s), to convert your string, s, to an integer or use the commands = str(i) to convert your integer, i, to a string.