Read in variable length integer matrix - fortran

I'm trying to read in the file
10 5 6 78 5 120 5 6 84 9 5 1
1 3 2 4 5 2 3 4 1 2 1 3
1 4 7 8 12 13
With a variable length of the rows.
I attempt to count the number of elements in a column first by reading in one number at a time, but it seems as if every call to read moves me to the next line. Is there an easy way to count the number of elements in an individual line in Fortran?

Check if this helps -
program count_words_text
implicit none
integer, parameter :: nlen=1000
character (len=nlen) :: text
integer :: nwords, pos, i
text = "foo boo 1 2 goo"
pos = 1
nwords = 0
loop: do
i = verify(text(pos:), ' ') !-- Find next non-blank.
if (i == 0) exit loop !-- No word found.
nwords = nwords + 1 !-- Found something.
pos = pos + i - 1 !-- Move to start of the word.
i = scan(text(pos:), ' ') !-- Find next blank.
if (i == 0) exit loop !-- No blank found.
pos = pos + i - 1 !-- Move to the blank.
end do loop
print*,nwords ! gives 5
end program count_words_text

Related

What will be the output of the following pseudo code for input 7?

Please help me to understand the following code and what will be the possiable output.
What will be the output of the following pseudo code for input 7?
1.Input n
2.Set m = 1, T = 0
3.if (m > n)
Go to step 9
5.else
T = T + m
m = m + 1
8.Go to step 3
9.Print T
0
n is less than n so go to step 9 which is print T which is equal to 0 as set in step 2.
T should be 28. It will loop till m>7 (since n=7) and in each iteration T adds m to itself, since T is 0 initially it is only summing up m after incrementing it by 1 in each iteration.So if you add 1+2+3.....+7 you get 28 and that is when the loop breaks since m is now equal to 8.
for m = 1 2 3 4 5 6 7 and for 8 m>n will be true and it will go to step 9
T=(T+M)= 1 3 6 10 15 21 28 basically T is a series where next is added as 2,3,4,5,6,7 to prev number 2 3 4 5 6 7 if one look from other angle

Python 2.7, restart loop

i am trying to loop though a series of numbers and assign a constant index to them with restarting a loop at a certain number.
input numbers
0
3
6
9
12
15
18
21
0
3
6
9
12
15
18
21
......
the expected output should be
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
my code looks like this. I works but never stops.
How could i iterate over the input data?
count = 1
for line in in_file:
should_restart = True
while should_restart:
should_restart = False
for i in xrange(0,22,3):
print count
if i == 21:
should_restart = True
count+=1
break
Thanks!
Supposing you have an in_file like this (that's just an example yours is different but I wanted to do it with your given input):
in_file = "0 3 6 9 12 15 18 21 0 3 6 9 12 15 18 21"
in_file = in_file.split(" ")
you setup an end-condition for your counter and a starting count:
end_condition = 21
count = 1
then you iterate through your list:
for line in in_file:
line = int(line) # This is only needed if you have string-data
within the loop print the current count:
print(count, end=' ')
and later increment the counter if you reach the final value:
if line == end_condition:
count += 1

Magic Square in Python Debugging

Problem originally is in this link. I wrote a Python code but I got 64 points (total points is 100) and this indicates that my code has some missing points. I passed 11 of 16 test cases but 5 test cases have problematic for me. Could you say where my code has some missing points and how can I fix it?
import math
m = int(raw_input())
liste = []
y_liste = []
md = 0
ad = 0
sum = 0
sum2 = 0
for k in range(m):
temp = str(raw_input())
liste.append(temp)
liste[k] = liste[k].split(" ")
liste[k] = [int(i) for i in liste[k]]
for k in range(m):
md += liste[k][k]
ad += liste[k][m-k-1]
if md == ad:
print 0
else:
for k in range(m):
for l in range(m):
sum2 += liste[l][k]
sum += liste[k][l]
if sum2 != md and -(k+1) is not y_liste:
y_liste.append(-(k+1))
if sum != md and (k+1) is not y_liste:
y_liste.append(k+1)
sum2 = 0
sum = 0
if md != ad:
y_liste.append(0)
print len(y_liste)
y_liste.sort()
for i in y_liste:
print i
Problem Statement
Magic Square
Johnny designed a magic square (square of numbers with the same sum for all rows, columns and diagonals i.e. both the main diagonal - meaning the diagonal that leads from the top-left corner towards bottom-right corner - and the antidiagonal - meaning the diagonal that leads from top-right corner towards bottom-left corner). Write a program to test it.
Task
Write a program that will check if the given square is magic (i.e. has the same sum for all rows, columns and diagonals).
Input
First line: N , the size of the square (1 <= N <= 600).
Next N lines: The square, N space separated integers pre line, representing the entries per each row of the square.
Output
First line: M , the number of lines that do not sum up to the sum of the main diagonal (i.e. the one that contains the first element of the square). If the Square is magic, the program should output 0.
Next M lines: A sorted (in incremental order ) list of the lines that do not sum up to the sum of the main diagonal. The rows are numbered 1,2,…,N; the columns are numbered -1,-2,…,-N; and the antidiagonal is numbered zero.
Note: There is a newline character at the end of the last line of the output.
Sample Input 1
3
8 1 6
3 5 7
4 9 2
Sample Output 1
0
Sample Input 2
4
16 3 2 13
5 10 11 8
6 9 7 12
4 15 14 1
Sample Output 2
3
-2
-1
0
Explanation of Sample Output 2
The input square looks as follows: http://i.stack.imgur.com/JyMgc.png
(Sorry for link but I cannot add image due to reputation)
The square has 4 rows (labeled from 1 to 4 in orange) and 4 columns (labeled from -1 to -4 in green) as depicted in the image above. The main diagonal and antidiagonal of the square are highlighted in red and blue respectively.
The main diagonal has sum = 16 + 10 + 7 +1 = 34.
The antidiagonal has sum = 13 + 11 + 9 + 4 = 37. This is different to the sum of the main diagonal so value 0 corresponding to the antidiagonal should be reported.
Row 1 has sum = 16 + 3 + 2 + 13 = 34.
Row 2 has sum = 5 + 10 + 11 + 8 = 34.
Row 3 has sum = 6 + 9 + 7 + 12 = 34.
Row 4 has sum = 4 + 15 + 14 + 1 = 34.
Column -1 has sum = 16 + 5 + 6 + 4 = 31. This is different to the sum of the main diagonal so value -1 should be reported.
Column -2 has sum = 3 + 10 + 9 + 15 = 37. This is different to the sum of the main diagonal so value -2 should be reported.
Column -3 has sum = 2 + 11 + 7 + 14 = 34.
Column -4 has sum = 13 + 8 + 12 + 1 = 34.
Based on the above, there are 3 lines that do not sum up to the sum of the elements of the main diagonal. Since they should be sorted in incremental order, the output should be:
3
-2
-1
0
Your explanation doesn't discuss this clause which is a potential source of error:
if md == ad:
print 0
else:
It says that if the main diagonal and antidiagonal add up to the same value, print just a 0 (no bad lines) indicating the magic square is valid (distinct from reporting a 0 in the list of bad lines). Consider this valid magic square:
9 6 3 16
4 15 10 5
14 1 8 11
7 12 13 2
If I swap 13 and 11, the diagonals still equal each other but the square is invalid. So the above code doesn't appear to be correct. In the else clause for the above if statement, you test:
if md != ad:
y_liste.append(0)
a fact you already know to be true from the previous/outer test so your code seems to be out of agreement with itself.

How to read gnuplot's file input format to array in Fortran?

#x #y #z
1 1 1
1 2 4
1 3 9
1 4 16
2 1 1
2 2 4
2 3 9
2 4 16
3 1 1
3 2 4
3 3 9
3 4 16
...
How to read this file to array x, y, z?
where x from first column, y from first column, z from first column of file
I'd tried something already but i had some problem with blank line.
Your description is quite incomplete, but if I get your intention right, you can first read the line into a variable and test if it is empty or not
character(200) : line
i = 1
do
read(unit, '(a)') line
if (len_trim(line)>0) then
read(line,*) x(i), y(i), z(i)
i = i + 1
end do
end do

Creating a non-right Pascal's triangle (centered) in python

I need to write a code that inputs a non-right Pascal's triangle given the nth level as an input where the first row is the 0th level. Apart from that, at the end of each row the level must be indicated.
Here's what I've made so far:
level = input('Please input nth level: ')
x = -1
y = 1
while x < level:
x = x+1
d = str(11**x)
while y < level:
y = y+1
print " ",
for m,n in enumerate(d):
print str(n) + " ",
while y < level:
y = y+1
print " ",
print x
When I input 3, it outputs:
1 0
1 1 1
1 2 1 2
1 3 3 1 3
My desired output is:
   1 0
1 1 1
1 2 1 2
1 3 3 1 3
You could use str.format to center the string for you:
level = int(raw_input('Please input nth level: '))
N = level*2 + 5
for x in range(level+1):
d = ' '.join(str(11**x))
print('{d:^{N}} {x:>}'.format(N=N, d=d, x=x))
Please input nth level: 4
1 0
1 1 1
1 2 1 2
1 3 3 1 3
1 4 6 4 1 4
Note that if d = '1331', then you can add a space between each digit using ' '.join(d):
In [29]: d = '1331'
In [30]: ' '.join(d)
Out[30]: '1 3 3 1'
Note that using d = str(11**x) is a problematic way of computing the numbers in Pascal's triangle since it does not give you the correct digits for x >= 5. For example,
Please input nth level: 5
1 0
1 1 1
1 2 1 2
1 3 3 1 3
1 4 6 4 1 4
1 6 1 0 5 1 5 <-- Should be 1 5 10 10 5 1 !
You'll probably want to compute the digits in Pascal's triangle a different way.