Reputation: 11
Hello I wanted to create a list of letters randomized using the values of pi. I'm quite stuck right now. Just want to ask suggestions to work this code out.
import re
import string
#I searched values of pi on the web and set it as x.
#I used this for loop to get a list of pi values with range from 1 to 100k
pi_list = []
for num in range(0, 10000):
pi_list.append(x[num])
#I used this method to join the values into one entry of string
#I think setting it to string would help for slicing later.
pi_values = "".join(pi_list)
#reference list to be used later
a = list(string.ascii_lowercase)
#string of pi values
b = pi_values
#empty list I want to put return values
c = []
#This is by far my code right now.
while len(c) < 27: #27 because alphabet has 27 letters
x = 2 #This x is for concatenation
for i in range(len(a)):
#I want to use pi values from b to return numbers to c then I will assign c list numbers later on to
#objects inside the list a.
while int(b[i]) % 27 in c:
b[i] = int(b[i:i+x])
x+=1
i+=1
#The objective here is to concatenate the next pi value if the current one exists in c and set that as
#the new value for i when the object already exists in list c. Then I use modulus 27 for values with 3
#digits and above. I want the concatenation to continue until it gets a new value that's not in c yet.
else:
c.append(int(b[i]))
print(c)
I want the program to return a set of 27 characters of the alphabet, 1 time for each letter. Just like in the sample in the image.
Right now, it returns these values.
['3', '1', '4', '1', '5', '9', '2', '6', '5', '3', '5', '8', '9', '7', '9', '3', '2', '3', '8', '4', '6', '2', '6', '4', '3', '3', '3', '1', '4', '1', '5', '9', '2', '6', '5', '3', '5', '8', '9', '7', '9', '3', '2', '3', '8', '4', '6', '2', '6', '4', '3', '3']
It repeats itself after 27 times, it seems. Not sure what to do here. Will be glad for some help.
Upvotes: 0
Views: 123
Reputation: 921
I recommend you give you program some more structure. Perhaps start by defining some concepts and then building each of them separately:
def pi_digits(n):
"""get pi with n n-1 decimal places: pi_digits(3) returns 3.14"""
# build this function first
my_num = ...
return my_num
def digit_to_char(d):
"""convert digit to character: digit_to_char(0) returns 'a'"""
# build this function next
my_char = ...
return my_char
# test both functions
print(pi_digits(10))
print(digit_to_char(2))
Ensure this works. Then expand to iteration.
Once you have this, try iterating the digits of Pi:
pi_digits = str(pi_digits)
my_output_str = ''
for char_digit in pi_digits:
if char_digit == '.':
continue # skip if the character is a decimal point
print('digit:', digit)
char = char_digit
# convert character to numerical digit (int)
char = int(char)
# convert numerical digit to alphabetical char
char = digit_to_char(char)
my_output_str += char
Note: as digits only go from 0-9 but there are 27 alphabetical characters.
Either way, you can wrap the code in another function like get_ndigit_picode(n)
.
Upvotes: 1