Project Euler-Problem 11 data taking

  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Data Project
Click For Summary
The discussion revolves around solving Project Euler Problem 11, which requires converting a grid of numbers into a usable format for calculations. Participants emphasize the importance of structuring the data as a matrix, specifically using a numpy array, rather than a string. Techniques for formatting the data include using Excel to remove spaces and save it as a CSV file, or directly copying data into a text file for import into Python. There is a consensus that understanding the algorithm is crucial before diving into coding, and it is suggested to start with smaller arrays to simplify the problem-solving process. Ultimately, the thread highlights the learning curve involved in programming and data manipulation for algorithmic challenges.
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
1. The problem statement, all variables, and given/known data
https://projecteuler.net/problem=11

2. Homework Equations 3. The Attempt at a Solution
I need to take those data and I want to convert them into a string to proceed.
But I can't turn it into the string since there are gaps between data so I should just remove the gaps between data? Or is there a way to convert it into the string just like that. Also, I think that turning this data to matrix would be nicer to solve the question but again is there an easy way to do that?
 
Physics news on Phys.org
The problem statement makes no mention of using a computer program to find the answer.
 
anorlunda said:
The problem statement makes no mention of using a computer program to find the answer.
Thanks but that doesn't help me
 
I'm not trying to be insulting, but your question as states makes no sense. You don't mention any specific programming language, so I must assume you don' t know any.

Since you don't know enough programming to put the numbers in a string or a matrix, then it should be easier for you to take a pocket calculator and start calculating products manually, than it would be to learn to use a programming language.
 
Arman777 said:
Thanks but that doesn't help me
You are supposed to do basic things like tell people you are trying to solve this in Python so they know how you are approaching the problem. I think this is one of the (many) reasons why there's a "show your work" portion of the template -- so people can figure out how you're thinking about and approaching the problem -- including the language in this case.
- - - -
You don't want a string here -- you want a matrix -- specifically a numpy array will work. You'll have to play around with the formatting a good bit and eventually get there. Consider copying and pasting into a spreadsheet to get the array correct, then saving as a csv and using numpy's "load text" method.

Unfortunately for a problem like this (and there are a few more array problems which I enjoyed a lot) its a bit tedious to get the setup -- with space delimmitting the array entries-- correct in Python. It is extremely easy to do in Julia as matrices there are a naturally space delimited (and there's no legacy issues of leading zeros in a number potentially denoting octals like they did in Python 2.x).

edit:
Since there are several array based problems like this on Project Euler you may want to spend some extra time to create an efficient approach to getting the array 'into' your script -- if you figure out a good, flexible process now, you can just re-use it when future comparable problems come up.
 
anorlunda said:
You don't mention any specific programming language, so I must assume you don' t know any.
Sorry for that.
StoneTemplePython said:
Unfortunately for a problem like this (and there are a few more array problems which I enjoyed a lot) its a bit tedious to get the setup -- with space delimmitting the array entries-- correct in Python.
yes exaclty...
StoneTemplePython said:
Since there are several array based problems like this on Project Euler you may want to spend some extra time to create an efficient approach to getting the array 'into' your script -- if you figure out a good, flexible process now, you can just re-use it when future comparable problems come up.
Hmm okay then I ll try to think more about it. I don't have numpy but I ll download it now, well but yes in any case I have to deleted spaces I guess..or as you said I ll think
 
StoneTemplePython said:
You don't want a string here -- you want a matrix -- specifically a numpy array will work. You'll have to play around with the formatting a good bit and eventually get there. Consider copying and pasting into a spreadsheet to get the array correct, then saving as a csv and using numpy's "load text" method.

Unfortunately for a problem like this (and there are a few more array problems which I enjoyed a lot) its a bit tedious to get the setup -- with space delimmitting the array entries-- correct in Python. It is extremely easy to do in Julia as matrices there are a naturally space delimited (and there's no legacy issues of leading zeros in a number potentially denoting octals like they did in Python 2.x).

Okay I did something like this;
I get the data from the page. I copied and pasted on the excel then I used ctrl+H and I repleced the "space" with "," and it worked here the image,

1.png


Then I saved it as a csv file but when I went python I couldn't upload it. I donwloaded anaconda for the numpy also it might be usefull in the future..So umm I tried several codes that I looked online but none of them worked.

import csv
with open ('ad.csv') as csvfile:
readCSV=csv.reader(csvfile,delimiter=',')
print(readCSV)

for row in readCSV:
print(row)
or

import numpy
numpy.loadtxt(ad.csv)

I am not sure how to extract the file, also my file name is ad
 

Attachments

  • 1.png
    1.png
    56.2 KB · Views: 573
That is not what a spreadsheet should look like -- everything needs to be in it's own cell. Then save as a CSV. The thing about Python is there are tons of fully worked examples on Stackoverflow of people working through csvs and loading with numpy, pandas, etc. Not fun stuff but it needs to get done, so you need to be able to work through those postings.

Alternatively, skip this problem and come back later. There are only a handful of array problems like this in Project Euler so there's plenty of other stuff to do (though I'll admit the array problems are probably my favorites.)
 
StoneTemplePython said:
(though I'll admit the array problems are probably my favorites.)
Why you like them so much :p
StoneTemplePython said:
That is not what a spreadsheet should look like -- everything needs to be in it's own cell.
Puff..I watched some videos and they import data from the .txt or etc. Okay well I ll try to do like that and find a way for that.

I don't much like to skip but maybe I do.
 
  • #10
Maybe I just do like this; since I managed to get rid of the spaces, I just copy and paste the data to the script and write a code to create a matrix maybe, it seems simpler. I ll try that. Another problem is their (the poeople who ask on stackoverflow) data is already in the separated form. They don't have to do any other work. So I ll try this
 
  • #11
Nvm I manage to do it. I just copied the data to .txt and then I opened excel and make import data :)
 
  • Like
Likes StoneTemplePython
  • #12
I couldn't import the data. It gives me file error or it doesn't work. Can someone give me an example code ?

Edit:Someone can think that I didnt tried well I watched nearly dozens of videos and searched at least 2 hours.
 
Last edited:
  • #13
Solving the problem with only the Python text editor seems impossible, I can't get the replace function to find newlines.

The excel method should work. You should use the Text to colums function and specify Delimited and choose a space as delimiter.
Finally save the document as .csv

Even easier is to copy and paste the numbers from the web page to a new text file and read that in.
Both the csv and the numpy methods work for the original text file
For numpy you must assign the result of loadtxt in a variable and you might want to use dtype = int to get integers and not floating point numbers.
 
  • #14
StoneTemplePython said:
You don't want a string here -- you want a matrix
Absolutely. And since the goal is to find the largest product of numbers, the numbers should be in a two-dimensional 20x20 array.

For the purpose of a program (presumably in Python, since that's the language you were using in a previous thread), it doesn't matter if the numbers in your array are the same as those in the Project Euler problem. So focussing on putting the numbers in a spreadsheet and importing them into a program is really wasted effort.The important thing is to get an algorithm that you can implement as code in some language (such as Python). Regarding the importance of starting with a proper algorithm, one of the first things that was presented in a programming class I took years ago was this:
The sooner you sit down to the computer keyboard, the longer it will take to write your program.
The idea here is that if you just start typing program code without having a good idea of what the code is supposed to do, it will take a lot longer to produce a working program.

It might be helpful to start with a smaller array of numbers, say 9x9, and see if you can come up with a way to find the largest product of three number in a row, down a column, or along any diagonal. Some parts of the problem description you can ignore, such as the "left and right" and "up and down" parts. If you find the largest product in one row going left to right, it will be the same as if you had gone right to left. Same with top to bottom versus bottom to top.

To start your algorithm, think about how you would find the largest product in any row. See if you can flesh out your algorithm to take care of all of the other things you need to check.

Edit: the thread was marked Solved, but since the OP is a long way from even a start, the problem doesn't appear solved to me. I reset things to Unsolved.
 
Last edited:
  • #15
Mark44 said:
Absolutely. And since the goal is to find the largest product of numbers, the numbers should be in a two-dimensional 20x20 array.

For the purpose of a program (presumably in Python, since that's the language you were using in a previous thread), it doesn't matter if the numbers in your array are the same as those in the Project Euler problem. So focussing on putting the numbers in a spreadsheet and importing them into a program is really wasted effort.The important thing is to get an algorithm that you can implement as code in some language (such as Python). Regarding the importance of starting with a proper algorithm, one of the first things that was presented in a programming class I took years ago was this:

The idea here is that if you just start typing program code without having a good idea of what the code is supposed to do, it will take a lot longer to produce a working program.

It might be helpful to start with a smaller array of numbers, say 9x9, and see if you can come up with a way to find the largest product of three number in a row, down a column, or along any diagonal. Some parts of the problem description you can ignore, such as the "left and right" and "up and down" parts. If you find the largest product in one row going left to right, it will be the same as if you had gone right to left. Same with top to bottom versus bottom to top.

To start your algorithm, think about how you would find the largest product in any row. See if you can flesh out your algorithm to take care of all of the other things you need to check.

Edit: the thread was marked Solved, but since the OP is a long way from even a start, the problem doesn't appear solved to me. I reset things to Unsolved.
I solved it
 
  • #16
At first, I may not be import the data but İt doesn't mean that I can't write codes to solve the problems. I imported the data and solved the problem.
Mark44 said:
the thread was marked Solved, but since the OP is a long way from even a start, the problem doesn't appear solved to me. I reset things to Unsolved.
So no need for any help and don't judge me, please. It's a helping site. Not a judging site. If I marked as solved it means that I solved it. But thanks for your concern.
 
  • #17
Mark's just trying to be helpful -- no need to feel like you're being judged.

As I saw it, you had two main subproblems here (a) figuring out the importing of the array into a matrix in python and (b) the algorithm for completing the challenge.

Both of these are good programming practice though they are developing different skills. It seems like you put in a lot of time and work on this, and solved it -- great, that's how you get up the learning curve in programming.

Care to share how you finally got the data table / array from the web page, into Python?

(I have my fingers crossed that this served as a positive introduction to numpy. You may also enjoy using Pandas module. )
 
  • #18
StoneTemplePython said:
As I saw it, you had two main subproblems here (a) figuring out the importing of the array into a matrix in python and (b) the algorithm for completing the challenge.

Both of these are good programming practice though they are developing different skills. It seems like you put in a lot of time and work on this, and solved it -- great, that's how you get up the learning curve in programming.
Yes that is why it hurts when somebody says "I am marking it as unsolved"

First I tried panda and numpy to import the data as you said. Then I changed my approach and by using excal I just arranged the terms and put them into the matrix form.

but here that code:

Python:
import pandas as pd
df=pd.read_excel(r'C:\Users\Arman\Desktop\as.xlsx', sheetname='sheet1')
import numpy as np
M=np.array(df)

I was using anaconda and using just normal python seens better
 
Last edited:
  • #19
Arman777 said:
Yes that's why it hurts when somebody says "I am marking it as unsolved"

I really wouldn't take it that way. Threads get mis-categorized as solved or unsolved from time to time and mentors need to make an educated guess on what the status is.

Btw, when people try to give you pointers on solving problems, they are genuinely curious as to how you ultimately solved the problem...

Sharing part of your solution is another way to head off any confusion over whether the problem is solved and perhaps satisfy the curiosity of someone who did this problem a few years ago...
- - - - -
a lot of programming is spending time on Google and working your way through technical snags, btw. It's something I was oddly resistant toward doing before programming and now I kind of automatically do it.
 
  • #20
Arman777 said:
Yes that is why it hurts when somebody says "I am marking it as unsolved"
Since you posted almost no code for this problem, other than what is needed to import from an Excel spreadsheet, I interpreted the "Solved" check as possibly being a mistake, in the way that @StoneTemplePython described.

Since you have solved the problem, we would like to see your solution.
 
  • #21
Python:
M=[[8,2,22,97,38,15,0,40,0,75,4,5,7,78,52,12,50,77,91,8],[49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,4,56,62,0],[81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,3,49,13,36,65],[52,70,95,23,4,60,11,42,69,24,68,56,1,32,56,71,37,2,36,91],[22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80],[24,47,32,60,99,3,45,2,44,75,33,53,78,36,84,20,35,17,12,50],[32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70],[67,26,20,68,2,62,12,20,95,63,94,39,63,8,40,91,66,49,94,21],[24,55,58,5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72],[21,36,23,9,75,0,76,44,20,45,35,14,0,61,33,97,34,31,33,95],[78,17,53,28,22,75,31,67,15,94,3,80,4,62,16,14,9,53,56,92],[16,39,5,42,96,35,31,47,55,58,88,24,0,17,54,24,36,29,85,57],[86,56,0,48,35,71,89,7,5,44,44,37,44,60,21,58,51,54,17,58],[19,80,81,68,5,94,47,69,28,73,92,13,86,52,17,77,4,89,55,40],[4,52,8,83,97,35,99,16,7,97,57,32,16,26,26,79,33,27,98,66],[88,36,68,87,57,62,20,72,3,46,33,67,46,55,12,32,63,93,53,69],[4,42,16,73,38,25,39,11,24,94,72,18,8,46,29,32,40,62,76,36],[20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,4,36,16],[20,73,35,29,78,31,90,1,74,31,49,71,48,86,81,16,23,57,5,54],[1,70,54,71,83,51,54,69,16,92,33,48,61,43,52,1,89,19,67,48]]
A=[]
y=0
t=4
q=1
for i in range(20):
    while True:
        for j in range(y,t):
            q=M[i][j]*q
        A.append(q)
        q=1
        y=y+1
        if t==20:
            y=0
            t=4
            q=1
            break
        else:
            t=t+1
for i in range(20):
    while True:
        for j in range(y,t):
            q=M[j][i]*q
        A.append(q)
        q=1
        y=y+1
        if t==20:
            y=0
            t=4
            q=1
            break
        else:
            t=t+1
u=0
l=17
b=1
for u in range(17):
    for y in range(l):
        for j in range(u,u+4):
            b=M[j+y][j]*b
        A.append(b)
        b=1
    l=l-1
u=0
q=1
l=17
for u in range(17):
    for y in range(l):
        for j in range(u,u+4):
            q=M[j][j+y]*q
        A.append(q)
        q=1
    l=l-1
V=[[8,91,77,50,12,52,78,7,5,4,75,0,40,0,15,38,97,22,2,8],[0,62,56,4,48,69,43,98,40,17,87,60,57,18,81,17,40,99,49,49],[65,36,13,49,3,30,88,53,67,40,71,93,29,14,79,55,73,31,49,81],[91,36,2,37,71,56,32,1,56,68,24,69,42,11,60,4,23,95,70,52]
,[80,13,33,66,28,40,40,22,54,36,92,41,89,63,67,51,71,16,31,22],[50,12,17,35,20,84,36,78,53,33,75,44,2,45,3,99,60,32,47,24]
,[70,64,38,18,66,70,54,59,67,40,38,26,10,67,23,64,28,81,98,32],[21,94,49,66,91,40,8,63,39,94,63,95,20,12,62,2,68,20,26,67]
,[72,63,89,34,88,14,83,96,78,78,17,97,26,99,73,66,5,58,55,24],[95,33,31,34,97,33,61,0,14,35,45,20,44,76,0,75,9,23,36,21]
,[92,56,53,9,14,16,62,4,80,3,94,15,67,31,75,22,28,53,17,78],[57,85,29,36,24,54,17,0,24,88,58,55,47,31,35,96,42,5,39,16]
,[58,17,54,51,58,21,60,44,37,44,44,5,7,89,71,35,48,0,56,86],[40,55,89,4,77,17,52,86,13,92,73,28,69,47,94,5,68,81,80,19]
,[66,98,27,33,79,26,26,16,32,57,97,7,16,99,35,97,83,8,52,4],[69,53,93,63,32,12,55,46,67,33,46,3,72,20,62,57,87,68,36,88]
,[36,76,62,40,32,29,46,8,18,72,94,24,11,39,25,38,73,16,42,4],[16,36,4,74,85,59,67,82,69,99,62,34,88,23,30,72,41,36,69,20]
,[54,5,57,23,16,81,86,48,71,49,31,74,1,90,31,78,29,35,73,20],[48,67,19,89,1,52,43,61,48,33,92,16,69,54,51,83,71,54,70,1]]
u=0
l=17
b=1
for u in range(17):
    for y in range(l):
        for j in range(u,u+4):
            b=V[j+y][j]*b
        A.append(b)
        b=1
    l=l-1
u=0
q=1
l=17
for u in range(17):
    for y in range(l):
        for j in range(u,u+4):
            q=V[j][j+y]*q
        A.append(q)
        q=1
    l=l-1
b=sorted(A)
print(b)
print(len(b))

The structure is a bit messy but it works. I don't need to optimize it anyways since it's not a homework but if someone can point an easier way to merge these "for" loops in one piece that can be nice.
V just the same matrix with a different kind order. When you run, the last element will give the result.

Also, it counts the (00,11,22,33) (11,22,33,44) elements twice (due to the code structure) but it doesn't affect the result so I leave it in that way.

As you may notice there's mainly 6 "for" loops first two calculates the up-down and left-right multiplication. As you may notice the only difference is the order of the matrix.

Next two calculates the right diagonal multiplication, again the only difference is matrix notation one is ##M[j+y][j]## and the other is ##M[j][j+y]## since the first one calculates lower triangular part of the matrix and the other one the upper triangular one (which that's why I have 2 times 11,22,33,44 etc.)

Last two is left diagonal multiplication again same codes since that's why I have another matrix of V. Or I have the same code because I changed the matrix a bit.

Without an inserting another matrix V the problem is solvable but doing with V is easier for me.
 
Last edited:
  • #22
Those two arrays M and V are rather ugly, as are all those assignment statements u=0, l=17, b=1 etc.

Once you have a correct solution, you can always look at the forum at project Euler.
Looking that over, I think the solution of Victor winberg/29-3-2018 on the last page is the most elegant (in python.

It iterates over the whole matrix only once, and then checks if for each cell, if there's room for a column of 4, below it, a row of 4 to the right or both.
Once you're certain there is room you can get four numbers with [M[y+i][x] for i in range(4)] (this is for a column)
Also note the trick of using ''' or """ to create a multiline string constant, so the numbers from the webpage can be pasted in the python editor without manipulating in excel.

An even shorter one by xjcl uses numpy. You can get a 4x4 submatrix with M[x:x+3][y:y+3] and use np.diag, np.fliplr to get at the diagonals.
Starting with numpy 1.9 these only create views of the matrix, an do not copy elements or create new matrix objects.
 
  • Like
Likes Arman777
  • #23
willem2 said:
Those two arrays M and V are rather ugly, as are all those assignment statements u=0, l=17, b=1 etc.
Yes I agree, my code structure is a mess , I didnt much care about it but I guess I should try to put in a nice form.

willem2 said:
It iterates over the whole matrix only once, and then checks if for each cell, if there's room for a column of 4, below it, a row of 4 to the right or both.
Once you're certain there is room you can get four numbers with [M[y+i][x] for i in range(4)] (this is for a column)
Also note the trick of using ''' or """ to create a multiline string constant, so the numbers from the webpage can be pasted in the python editor without manipulating in excel.

Thats really a good idea actually. I never thought that..Even I do I can't write codes like him.
I also liked couple codes. In the same page concaptain or siyavash
 
  • #24
Python:
# coding: utf-8

# In[43]:import pandas as pd
import numpy as np
df=pd.read_excel(r'C:\Users\Arman\Desktop\M.xlsx', sheet_name='Sheet1')
M=np.array(df)
dl=pd.read_excel(r'C:\Users\Arman\Desktop\V.xlsx', sheet_name='Sheet1')
V=np.array(dl)
A=[]
u=y=0
t=4
c=w=b=q=1
l=17
for i in range(20):
    while True:
        for j in range(y,t):
            q=M[i][j]*q
            b=M[j][i]*b
        A.append(q)
        A.append(b)
        q=1
        b=1
        y=y+1
        if t==20:
            y=0
            t=4
            q=1
            break
        else:
            t=t+1
for u in range(17):
    for y in range(l):
        for j in range(u,u+4):
            b=M[j+y][j]*b
            q=M[j][j+y]*q
            w=V[j+y][j]*w
            c=V[j][j+y]*c
        A.append(q)
        A.append(b)
        A.append(w)
        A.append(c)
        b=1
        q=1
        w=1
        c=1
    l=l-1
b=sorted(A)
print("The largest product is", b[len(b)-1])

Much better :) Only problem is none of you can run it since you don't have the matrix.

Edit: I made it a bit shorter.
 
Last edited:
  • #25
For future things like this: you can also often solve simple text manipulation problems like this in seconds just using the capabilities of a decent text editor. For example, Emacs has a feature called "keyboard macros" that let's you record sequences of keystrokes (e.g., move to beginning of line, insert a '[', move to the end of the line, insert "],", move to the next line) and repeat them as many times as you like. Using a keyboard macro and the search-and-replace feature to replace every space with ", ", I got this in a few moments:
Code:
[08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00],
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65],
[52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91],
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
[24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
[67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 08, 40, 91, 66, 49, 94, 21],
[24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
[21, 36, 23, 09, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95],
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 09, 53, 56, 92],
[16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57],
[86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
[19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40],
[04, 52, 08, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
[88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
[04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 08, 46, 29, 32, 40, 62, 76, 36],
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16],
[20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54],
[01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48],
Then you would only need to delete the last comma and add the [ and ] at the beginning and end to get the correct Python syntax.

If you work a lot with text, using a good text editor and knowing about these kinds of capabilities can often save you the trouble of fishing around for a library or having to use Perl/Awk/etc. to do the work.
 
  • Like
Likes Arman777
  • #26
wle said:
For future things like this: you can also often solve simple text manipulation problems like this in seconds just using the capabilities of a decent text editor. For example, Emacs has a feature called "keyboard macros" that let's you record sequences of keystrokes (e.g., move to beginning of line, insert a '[', move to the end of the line, insert "],", move to the next line) and repeat them as many times as you like. Using a keyboard macro and the search-and-replace feature to replace every space with ", ", I got this in a few moments:


I ll look in detail, thanks a lot. Creating M matrix was easy but I cannot say the same thing for V. So I am sure it will be useful.
 
  • #27
wle said:
For future things like this: you can also often solve simple text manipulation problems like this in seconds just using the capabilities of a decent text editor. For example, Emacs has a feature called "keyboard macros" that let's you record sequences of keystrokes (e.g., move to beginning of line, insert a '[', move to the end of the line, insert "],", move to the next line) and repeat them as many times as you like. Using a keyboard macro and the search-and-replace feature to replace every space with ", ", I got this in a few moments:
Code:
[08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00],
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65],
[52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91],
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
[24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
[67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 08, 40, 91, 66, 49, 94, 21],
[24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
[21, 36, 23, 09, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95],
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 09, 53, 56, 92],
[16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57],
[86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
[19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40],
[04, 52, 08, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
[88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
[04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 08, 46, 29, 32, 40, 62, 76, 36],
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16],
[20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54],
[01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48],
Then you would only need to delete the last comma and add the [ and ] at the beginning and end to get the correct Python syntax.

If you work a lot with text, using a good text editor and knowing about these kinds of capabilities can often save you the trouble of fishing around for a library or having to use Perl/Awk/etc. to do the work.


I tried but I couldn't do it..(for other problem)
 
  • #28
Arman777 said:
I tried but I couldn't do it..(for other problem)

OK, but what do you want me to do? You haven't even said what "it" is.

I should maybe clarify something about my previous post. You had a simple text manipulation problem (you had an array of numbers copied and pasted from a website that, one way or another, you wanted to get into Python). There are various tools that you can use to deal with such problems (in general):
  • Text manipulation capabilities of a good text editor, like Emacs or Vim.
  • Scripting languages specialised for text processing, like Awk and Perl.
  • Whatever text manipulation features and libraries happen to be available for the language you actually want to use (in your case, Python).
My point was that if you're expecting to have to deal with a lot of text manipulation problems like this in the future then it would be worth learning such tools, and a good text editor (Emacs is not the only one) is such a tool. The point was not "You should immediately start using Emacs for all such problems". Emacs, like other editors with similar capabilities, takes some time to learn before you can use it effectively.

You also always have the option of just doing the text processing in Python, if you find that simpler. For your original problem, if you copied and saved the numbers into a text file "numbers.txt" then reading it into Python takes about 2 lines of code:
Python:
with open("numbers.txt") as f:
    M = [ [int(w) for w in line.split()] for line in f.readlines() ]

# Now do whatever you want with M.
This uses the following Python features:

There may be libraries to do such things, but it is also worth learning to do it by yourself.
 
Last edited:

Similar threads

  • · Replies 8 ·
Replies
8
Views
928
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 47 ·
2
Replies
47
Views
8K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 30 ·
2
Replies
30
Views
7K
  • · Replies 80 ·
3
Replies
80
Views
10K