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!
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 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 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 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
Write a program (without using loops) which given a string, of size less
than 5, and a positive integer less than 100, prints both of them with
enough dots in between so that the whole string has 20 characters in them.
I know that if I am not using loops I have to use print(..., sep="") supresses the blank between the objects ,printed.
Can somebody tell me How do I restrict new string length to 20 characters?
So you need to know the length of the string and the number of digits in your integer, subtract those from 20, and you know how many dots to place in-between. This shouldn't be too hard (check out the len() and str() functions).
Did you know that you can get a string of n dots by doing '.' * n?
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 7 years ago.
Improve this question
My code, stuck at 6561, unable to continue. I have tried printing B, I've tried substituting while True for a counter of greater value than the amount being printed. I have also tried different numbers for the original values, this could sometimes make it worse; eg. "a, b = 9, 10" will only print 9 and 10. Question, how do I unstick it?
the code
UPDATE: It appears that if you run this code in idle, then create a new python file, python stops responding.
It's not a bug with the code, it's a direct consequence of the size of the generated numbers.
8 ** 6561 is a 5926 digit number. The next step in the sequence is 6561 raised to the power of that 5926 number. That's where it gets stuck - the computation is simply too large for Python to handle in a reasonable amount of time.
Python supports arbitrary sized integers beyond the native size supported by the hardware, and the implementation requires memory to store the digits of the integer. The result is that your program will consume more and more memory until either the answer is produced, your system/process runs out of memory, or you run out of patience.
You will find that it works with multiplication and addition simply because the numbers involved are smaller, and so it will take a lot longer to reach the huge sizes produced by exponentiation.