I am building a sudoku game, I found a site that can provide me 1 million pre generated games.
I downloaded the file (CSV) and want to prepare it for frontend use.
Each game is 81 numbers like
346179258187523964529648371965832417472916835813754629798261543631485792254397186
974183652651274389283596714129835476746912538835647921568329147317468295492751863
563472198219386754847195623472638519951247386638519472795864231324951867186723945
I would love to create JS or JSON file with all the puzzles in order to import it in my code.
Here is the ideal result for each game (line).
[
[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 7],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]
]
Here is what I managed to accomplish in python
import csv
import json
holder = []
rows = []
def divide_chunks(l, n):
# looping till length l
for i in range(0, len(l), n):
yield l[i:i + n]
with open('sudoku.csv', newline='') as csvfile:
counter = 0
n = 9
s=','
reader = csv.DictReader(csvfile)
for row in reader:
if counter > 10:
break
print(row['solutions'])
print(len(row['solutions']))
test = [int(str(row['solutions']))]
#chunk = divide_chunks(test, n)
# for val in enumerate(chunk):
# val = [val]
# # for index,item in enumerate(val):
# # item[index] = item+s
# # print(val)
holder.append(test)
counter +=1
print(holder)
with open('puzzles.json', 'w') as outputfile:
json.dump(holder,outputfile)
this is my output so far in puzzles.json
[
[864371259325849761971265843436192587198657432257483916689734125713528694542916378],
[346179258187523964529648371965832417472916835813754629798261543631485792254397186],
[695127384138459672724836915851264739273981546946573821317692458489715263562348197],
[497258316186439725253716498629381547375964182841572639962145873718623954534897261],
[465912378189473562327568149738645291954821637216397854573284916642159783891736425],
[194685237382974516657213489823491675541768923769352841215839764436527198978146352],
[289765431317924856645138729763891542521473968894652173432519687956387214178246395],
[894231657762495183351876942583624719219387564647159328128763495976542831435918276],
[563472198219386754847195623472638519951247386638519472795864231324951867186723945],
[163725948584693271729184365946358127371462589852917634498231756637549812215876493],
[974183652651274389283596714129835476746912538835647921568329147317468295492751863]
]
Any suggestions please?
Thanks
You can break the string into numbers by using a list comprehension
sudoku = 346179258187523964529648371965832417472916835813754629798261543631485792254397186
sudoku_list = [number for number in str(sudoku)]
And then you break that into chunks of length 9 by using another list comprehension
sudoku_final = [sudoku_list[9*i:9*i+9] for i in range(9)]
If you want the output to be integers instead of strings, use int(number) for number in str(sudoku) in the list comprehension
Using Python, you can separate all the numbers into a list by converting them into a string and back like so:
tuple(map(int, str(x))) # x is the 81 digit number.
With that, you can separate the list of 81 digits into a 9x9 grid like so (iterating through every multiple of 9 and taking the next 9 numbers):
[x[i: i + 9] for i in range(0, 81, 9)]
Related
I believe this is an easy problem to solve. I have searched and found a few similar answers but not an efficient way to exactly what I want to achieve.
Assuming the following list:
x = [6, 7, 8]
I want to create a new list by repeating each number k times. Assuming k=3, the result should be:
xr = [6, 6, 6, 7, 7, 7, 8, 8, 8]
I was able to accomplish this using nest loops, which I believe is very inefficient:
xr = []
for num in x: # for each number in the list
for t in range(3): # repeat 3 times
xx2.append(num)
I also tried:
[list(itertools.repeat(x[i], 3)) for i in range(len(x))]
but I get:
[[6, 6, 6], [7, 7, 7], [8, 8, 8]]
Is there a more efficient direct method to accomplish this?
You can use list comprehension:
x = [6, 7, 8]
k = 3
out = [v for v in x for _ in range(k)]
print(out)
Prints:
[6, 6, 6, 7, 7, 7, 8, 8, 8]
def repeat_k(l,k):
lo = []
for x in l:
for i in range(k):
lo.append(x)
return lo
print (repeat_k([1,2,3],5))
Output:
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3]
With list comprehension:
def repeat_k(l,k):
return [ x for x in l for i in range(k) ]
print (repeat_k([1,2,3],5))
Output:
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3]
Another possibility:
>>> x = [6, 7, 8]
>>> k = 3
>>> l = []
>>> for item in x:
... l += k * [item]
...
>>> l
[6, 6, 6, 7, 7, 7, 8, 8, 8]
You can create a convenient function:
def repeat(it, n):
for elem in it: yield from [elem] * n
Use it like:
>>> list(repeat(x, n=3))
[6, 6, 6, 7, 7, 7, 8, 8, 8]
Thanks, everyone for the answers.
It seems there is an easier and more direct way to solve this using Numpy.
np.repeat(x, 3).tolist()
prints exactly what I needed:
[6, 6, 6, 7, 7, 7, 8, 8, 8]
import itertools
x=[4,5,6]
k=3
res = list(itertools.chain.from_iterable(itertools.repeat(i, K) for i in test_list))
print (res)
It can also be solved using python inbuilt functions of itertools library. The repeat function does the task of repetition and grouping into a list is done by the from_iterable function.
how can I replace the numbers that are greater than 9 by their sum of digits?
right now the list multipliedlist =
[1, 4, 3, 8, 5, 12, 7, 16, 2]
I need to change it to (ex, num 12 and num 16 replaced to (3) and (7) )
[1, 4, 3, 8, 5, 3, 7, 7, 2]
I can use sum(map(int, str(number))) to add the digits but how can i change the values in the same list by their index?
def check_id_valid(id_number):
updatedid = map(int, str(id_number))
multipliedlist = [i * 1 if j % 2 == 0 else i * 2 for j, i in enumerate(updatedid)]
# for index, number in enumerate(multipliedlist):
# if multipliedlist[index] > 9:
# multipliedlist[index] = sum(map(int, str(number)))
# else:
# multipliedlist[index] == number #statement has no effect error.
print(check_id_valid(123456782))
New to python sorry if this is not explained as it's supposed to be
I appreciate any help,Thanks :)
Using a list comprehension
Ex:
data = [1, 4, 3, 8, 5, 12, 7, 16, 2]
print([sum(map(int, str(i))) if i > 9 else i for i in data])
Output:
[1, 4, 3, 8, 5, 3, 7, 7, 2]
Break your task into the constituent parts, namely
replacing a number with the sum of its digits
doing that for a list of numbers.
def sum_digits(number):
# Convert the number into a string (10 -> "10"),
# iterate over its characters to convert each of them
# back to an integer, then use the `sum()` builtin for
# summing.
return sum(int(digit_char) for digit_char in str(number))
def sum_all_digits(numbers):
return [sum_digits(number) for number in numbers]
print(sum_all_digits([1, 4, 3, 8, 5, 12, 7, 16, 2]))
outputs the expected
[1, 4, 3, 8, 5, 3, 7, 7, 2]
To change values by index you can use enumerate() function:
def sum_digits(n):
r = 0
while n:
r, n = r + n % 10, n // 10
return r
multipliedlist = [1, 4, 3, 8, 5, 12, 7, 16, 2]
for i, n in enumerate(multipliedlist):
multipliedlist[i] = sum_digits(multipliedlist[i])
print(multipliedlist)
[1, 4, 3, 8, 5, 3, 7, 7, 2]
I am unable to store unique 1D list into a 2D list.
I'm having trouble storing multiple randomly generated list into a 2D list. I can generate n number of unique 1D generated lists but when I try to store them in a 2D list, I get the same 1D list generated n number of time.
I've used the append() function on the list but instead get the same 1D list appended multiple times.
I've used the extend() function but it returns a 1D list.
I don't understand why the object is aliased when I'm using the shuffle() function.
import random
bd = list(range(10))
rng = random.Random()
rng.shuffle(bd)
d_list = []
number = 0
while number < 5:
d_list.append(bd)
number += 1
print(d_list)
I expected the results to be
[[6, 5, 3, 8, 2, 4, 0, 9, 1, 7],[7, 6, 9, 8, 5, 3, 4, 0, 2, 1], [2, 1, 0, 7, 3, 8, 6, 5, 4, 9], [1, 2, 7, 0, 3, 4, 5, 6, 8, 9],[7, 6, 3, 8, 4, 5, 1, 9, 0, 2]]
but instead the results are
[[7, 6, 3, 8, 4, 5, 1, 9, 0, 2], [7, 6, 3, 8, 4, 5, 1, 9, 0, 2], [7, 6, 3, 8, 4, 5, 1, 9, 0, 2], [7, 6, 3, 8, 4, 5, 1, 9, 0, 2], [7, 6, 3, 8, 4, 5, 1, 9, 0, 2]]
In your code:
while number < 5:
d_list.append(bd). #bd is the same and initialized one time
number += 1
You need to execute the randomizer within the while for you to truly have random lists each time.
Move your initialization of bd to your loop:
import random
d_list = []
number = 0
while number < 5:
bd = list(range(10))
rng = random.Random()
rng.shuffle(bd)
d_list.append(bd)
number += 1
print(d_list)
Right now, you're shuffling and adding the same list to d_list 5 times. Initializing the list inside the loop means that you're actually creating 5 different lists.
To solve your problem, shuffle bd at every iteration or use this one-liner to generate the same list:
d_list = [random.sample(range(10), 10) for i in range(5)]
It seems that you're appending 5 times the same list to an empty list. The result shows exactly that.
If you want a new random list to be generated at every iteration, I would recommend placing the list shuffle inside the loop.
import random
bd = list(range(10))
rng = random.Random()
rng.shuffle(bd)
d_list = []
number = 0
while number < 5:
rng.shuffle(bd)
d_list.append(bd)
number += 1
Note: I owuld also recomment using a for loop instead of a while info :
import random
bd = list(range(10))
rng = random.Random()
rng.shuffle(bd)
d_list = []
for range(5):
rng.shuffle(bd)
d_list.append(bd)
I am running a for loop and appending a value into a list for every file run in loop. When I use append(), during the second run through the for loop it appends the new values into the same list as in the first run through loop. Is there a way to append and create a new list everytime it runs through loop?
phaseresult_i =[]
for i in range(len(folder)):
data = np.loadtxt(dir + folder[i])
time = data[:,0]-2450000
magnitude = data[:,1]
print ('File:', folder[i],'\n','Time:',time,'\n', 'Magnitude:', magnitude)
print(len(time), len(magnitude))
for t in range(len(time)):
#print(t,time[t])
floor = math.floor((time[t]-time[0])/Period)
phase_i = ((time[t]-time[0])/Period)-floor
phaseresult_i.append(phase_i)
print(len(time), len(phaseresult_i))
The length of the array of time and length of array of phase result is not the same after the second time through loop.
An mcve for creating a new list on each iteration of the outer loop then append to that list in the inner loop.
x = []
for n in range(4):
q = []
x.append(q)
#other stuff
for t in range(10):
#other stuff
q.append(t)
>>> from pprint import pprint
>>> pprint(x)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>>
I have a string 12345678 and I want to convert it into a list [1,2,3,4,5,6,7,8] in python.
I tried this method :
You can use map:
list(map(int, '12345678')) # [1, 2, 3, 4, 5, 6, 7, 8]
Or a list comprehension:
[int(x) for x in '12345678'] # [1, 2, 3, 4, 5, 6, 7, 8]
If you want without loop or map, You can try:
final_=[]
def recursive(string1):
if not string1:
return 0
else:
final_.append(int(string1[0]))
return recursive(string1[1:])
recursive('12345678')
print(final_)
output:
[1, 2, 3, 4, 5, 6, 7, 8]