I am having a problem with coding this in python [closed] - python

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]

Related

Creating a List of a Range is slow, are there alternatives? [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
so I have to create a List of a big range and am trying to accomplish this with list(range(number1, number2))and the numbers number1 and number2 can have a big difference, for example 235 and 4323246376. Now, this takes a long time to process. Can this process be sped up?
If you're only looking to use the list as a filter to count items in another list, you should not be using a range at all.
For example:
count = numpy.sum(theList >= 235) - numpy.sum(theList >= 4323246376)
What use case are you going to achieve with this?
In case you are going to use this list once, don't typecast the range object into a list.
When you typecast the range object into a list, It stores all the numbers in a list which costs you memory.
You can just iterate over the range object.

Averaging the digits of pi in Python [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 1 year ago.
Improve this question
As a preface, I must admit that this is my first real attempt to code a simple program from scratch, and I've just begun my foray into learning python.
As a simple project, I wanted to write a program to find the average value of the digits of π. In other words, we all know that π=3.141529....., but I set out to determine the value of (3+1+4+1+5+....)/(# of digits summed).
This was the result:
#retrieve the value of pi
n=math.pi
#request the number of digits of pi to average over
mag=input("how many terms?")
mag=int(mag)-1
#generate a string populated with the whole number values of the digits of py
k=int(round(n*10**(mag),0))
digits=[int(d) for d in str(k)]
print(k)
#sum the specified values of the digits of pi
print(sum(digits))
#recall the length of the string
print(len(digits))
#calculate average
print((sum(digits)/len(digits)))
The code runs well enough, but I am curious about what tweaks I could make to improve the program or simplify it.
Most specifically, I would like to know if there there is a simpler or more direct way to cast the individual digits of pi into a list or string of integers for manipulation. For instance, is there a specific operator one could use to call individual digits of a given number, like digit(0) returns 3 with respect to pi.
Any advice is greatly appreciated!

Ranking preference of each item in a list [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 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.

About web scraping in python [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 4 years ago.
Improve this question
There are two major variables (calls and puts), and several sub-variables (e.g. bid, change, time etc.) For example, if there are total 5 data points. I know how to do separately:
data[u'options'][0]["calls"][0]["change"]['fmt'], data[u'options'][0]["calls"][1]["change"]['fmt'], data[u'options'][0]["calls"][2]["change"]['fmt'], data[u'options'][0]["calls"][3]["change"]['fmt'],data[u'options'][0]["calls"][4]["change"]['fmt']
but that spend too much time. I wonder how to choose multiple items in one code.
You can do this with a little bit of list comprehension if I understand your question properly.
For each value in data["options"][0]["calls"], it adds that value's ["change"]["fmt"] value to the list.
d = [call["change"]["fmt"] for call in data["options"][0]["calls"]]
If you want a list of EACH value from every set of options, you could do it like so:
d = [[call["change"]["fmt"] for call in option["calls"]] for option in data["options"]]
and now you can say
for option in d:
for call in option:
print(call)
[data[u'options'][0]["calls"][i]["change"]['fmt'] for i in range(5)]
I don't quite understand your problem, is this what you're after?

Python - big list issue [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 7 years ago.
Improve this question
I need to create and search a big list, for example list of 3.9594086612e+65 arrays of 10x10 (or bigger list of bigger arrays).
I need to create list of combinations. After I need to filter out some combinations according to some rule. This process should be repeated till only one combination is left.
If I try to create this list it crashes because of the memory after few minutes.
I can imagine, that solution should be to store the data in different way than list in memory.
What is possible, correct and easy way? SQL database? NoSQL database? Or just multiple text files opened and closed one after one? I need to run through this list multiple times.
Hy:
3.9594086612e+65
It's more than all computer memory in the world.
(And I do not multiply by 10)!

Categories