Special counting [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 6 years ago.
Improve this question
I want to fill an array in python with ones in a specific way. An example with 4 digits is the easiest way to explain:
0000, 0001, 0010, 0100, 1000, 1001, 1010, 1100, 1101, 1110, 1111
I need a list with 8 digits, so just writing them down is not an option. I really have no idea how to do that..
Edit: To clarify the problem. I need for the first entry all digits to be 0. Then all possibilities with just one 1, then all possibilities with two 1's and so forth. The last entry would be all 1.

An easy way is to sort all the binary representations by the number of 1s they have. Since python sort is stable, this also keeps the original order between numbers with the same number of 1s.
sorted(('{:08b}'.format(i) for i in xrange(256)), key=lambda x: x.count('1'))

Its not in the same order in your example, I assume you don't need the specific order:
for i in range(256):
print "{:08b}".format(i)
Also I assume you need values in strings, since you specified stuff like 000001 and that expression is meaningless in numbers (equals 1).

Related

Find the average of string 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 last year.
Improve this question
I have an string in python (with float and integer numbers)
string = '44,44,44,33'
I want to calculate the mean of that.
Note: I have one large dataframe, and in one column of the dataframe, I have such a string. I want to calculate the mean for all rows. I hope I there is an efficient way for that.
Thanks
Spilt string, covert to integers, and calculate mean:
import statistics
result = statistics.mean(map(int, string.split(",")))
l = string.split(',')
average = sum(int(k) for k in l)/len(l)

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!

Free function that gives a lexicographically bigger string each time [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 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.

String and dots filling [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
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?

How to represent "range" in python dict [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 6 years ago.
Improve this question
I would like to create dictionary like this:
1,2 = "A"
3,4 = "B"
5,6,7・・・(>=5) = "C"
so I tried below, but didnt works well..
dict = {1:"A", 2:"A", 3:"B", 4:"B", >=5:"C"}
how can I create a dictionary like this?
A lookup of a value where the key is in a sorted range is capped at O(log n). Hence using a dictionary directly is not viable. You can either do this by:
storing the ranges in list, sorting them, and doing a binary search, or...
using a tree data structure.
There are other creative solutions such as storing all the numbers in the range in the dictionary as integer keys and have duplicate references to the same value (which looks like what you have above). This would be O(1) lookup, but would have the negative side effects such as:
cannot modify the ranges easily without updating all the keys
wasted space for large ranges
cannot do lookups with floats
...which might be okay depending on your use case.
In summary, there is no direct solution.

Categories