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.
Related
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 trying to solve this problem forever now, I am having a problem when I do a dictionary for the days and for the students I can't store the birthdays since they are dictionaries or maybe I am just lost, how can I write efficient code fr this simple problem
In this simulation question, we will simulate the birthday problem for the case where at least
3 students have the same birthday in a class with 200 students.
For the simulation you can consider this scenario: Assume a bag filled with
numbers from 1 to 365. Then let 200 students pick a ball and then put it back. The
number the students pick is their birthday. Convert this process into a ’function’
in your program, where the return is an array of size with 200 and filled with the
birthdays.
You could use python's random module to sample 200 numbers in the range 0-365 without duplicates: https://docs.python.org/3/library/random.html#random.sample
Something like:
from random import sample
random.sample(range(365), 200)
Does it have to be a dictionary? This may be simpler to do by using a list. By creating a list you can append every birthday drawn (in the case of scenario of the simulation) to the list and have the indices of list correspond to each student. ie. student 1: index 0 would have the birthday: list[index]
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
For example, we have two methods that want to get the median of the lengths of the words in a sentence string input (this is just a simple example):
def get_median(sentence):
words = tokenize(sentence)
lengths_of_words = [len(word) for word in words]
median = statistics.median(lengths_of_words)
return median
This method is 4 lines long, but describes every component.
Its counterpart is:
def get_median(sentence):
return statistics.median([len(x) for x in tokenize(sentence)])
Even the second seems more pythonic and smooth, the first is more descriptive and compartmentalized. I can't seem to find a clear consensus on this, but what should be preferred generally and what is considered more readable? And why?
In my opinion the second way is better because its way less cluttered. I think that if you concerned that someone might not understand what the code snipped does you could just write a comment above explaining it. Why have cluttered code when you can do the same thing in a way thats more appealing to the eye when reading?
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 3 years ago.
Improve this question
I am implementing a toy db, and I need a free function that gives me a lexicophgraically bigger string each time it's called. It's for naming segment files.
Let's assume I have a max of 1000 files, and I'd prefer if the string was less than 10 characters long.
Could someone give me the easiest example of such a function in python? I'd really like to be a free function as I don't want to introduce complexity with state.
A function that returns a different value each time you call it will have to keep some sort of state. However, defining a generator makes that relatively simple to manage. Specifically, itertools.count will produce an infinite stream of increasing integers; you just need to produce a suitable string from each integer.
from itertools import count
next_label = map("{:010}".format, count()).__next__
Then
>>> next_label()
'0000000000'
>>> next_label()
'0000000001'
>>> next_label()
'0000000002'
and so on, for as many times as you need to call next_label.
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.
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 6 years ago.
Improve this question
Read each line in a text file, store the values in a list, and compute scores
The best approach this problem is to list all the functions you need to do the task. A typical example is:
Read file.
Read list.
Take list and get each string delimited by space.
Store string into an array.
...
Then go to the Python website and lookup how to do each function.
Example: To do input and output function in python:
https://docs.python.org/2/tutorial/inputoutput.html
Also, you can look up function by asking google. Google will then point you to answers to your questions:
Q: How to find mean of a list?
A: Finding the average of a list
If you do this enough times, eventually you will be able to write a problem that solves the problem listed.
Good Luck!