[Python] A program that calculates the difference between the total numbers on the diagonal line.

[Python] A program that calculates the difference between the total numbers on the diagonal line.

This is a memo for myself.

▼ Question

--A table of n x n is given. --Calculate the absolute value of the total value from the upper left to the lower right and the difference from the upper right to the lower left.

URL

▼sample input

matrix


11 2 4
4 5 6
10 8 -12

python


arr = [[11,2,4],[4,5,6],[10,8,-12]]

▼sample output

python


15

▼my answer

python


def diagonalDifference(arr):
    n = len(arr[0])-1
    xrr=[]
    yrr=[]

    i=0
    for ar in arr:
        xrr.append(ar[i])
        yrr.append(ar[n-i])
        i += 1
    ans = abs(sum(xrr)-sum(yrr))
    return ans

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    n = int(input().strip())
    arr = []
    for _ in range(n):
        arr.append(list(map(int, input().rstrip().split())))
    result = diagonalDifference(arr)
    fptr.write(str(result) + '\n')
    fptr.close()

** ・ abs () ** Returns the absolute value

** ・ List definitions cannot be summarized ** List cannot be defined as if i = x = 0 by default. ☓ xrr = yrr = [] * Refers to the same object. ◯xrr=[] yrr=[]

Recommended Posts

[Python] A program that calculates the difference between the total numbers on the diagonal line.
[Python] A program that rounds the score
[Python] I tried to make a simple program that works on the command line using argparse.
[Python] A program that calculates the number of chocolate segments that meet the conditions
[Python] A program that calculates the number of socks to be paired
[Python] A program that calculates the number of updates of the highest and lowest records
[Python] A program that compares the positions of kangaroos.
A note that runs an external program in Python and parses the resulting line
[Python] A program that finds the most common bird types
[Golang] A program that determines the turn with random numbers
I made a program that solves the spot the difference in seconds
[Ev3dev] Create a program that captures the LCD (screen) using python
Python standard module that can be used on the command line
I made a program that automatically calculates the zodiac with tkinter
[Python] A program that rotates the contents of the list to the left
[Python] A program that creates stairs with #
[Introduction to Python] What is the difference between a list and a tuple?
[Python] I made a bot that tells me the current temperature when I enter a place name on LINE
The eval () function that calculates a string as an expression in python
A program that plays rock-paper-scissors using Python
[Python] A progress bar on the terminal
[Python] A program that finds the minimum and maximum values without using methods
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
Periodically run a python program on AWS Lambda
A program that removes duplicate statements in Python
Publish a Python module that calculates meteorological factors
About the difference between "==" and "is" in python
A program that searches for the same image
A shell program that displays the Fibonacci sequence
A story that got stuck when trying to upgrade the Python version on GCE
A note on the library implementation that explores hyperparameters using Bayesian optimization in Python
[Python] A program that finds the shortest number of steps in a game that crosses clouds
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
[Python] A notebook that translates and downloads the ipynb file on GitHub into Japanese.
There is a pattern that the program did not stop when using Python threading
Think about how to program Python on the iPad
[Python] Generate random numbers that follow the Rayleigh distribution
Make a breakpoint on the c layer with python
Python program that looks for the same file name
Write a log-scale histogram on the x-axis in python
A memo that I touched the Datastore with python
A Python program that converts ical data into text
A program that automatically resizes the iOS app icon to the required image size in Python
Summary of points to keep in mind when writing a program that runs on Python 2.5
I want to absorb the difference between the for statement on the Python + numpy matrix and the Julia for statement
[Python] A program to find the number of apples and oranges that can be harvested
What is the difference between a symbolic link and a hard link?
A Python program in "A book that gently teaches difficult programming"
A general-purpose program that formats Linux command strings in python
A program that removes specific characters from the entered text
[Python] Create a linebot that draws any date on a photo
Write a python program to find the editing distance [python] [Levenshtein distance]
I tried "a program that removes duplicate statements in Python"
[Python] A program that creates a two-dimensional array by combining integers
[GCP] A memorandum when running a Python program on Cloud Functions
Is there a bias in the numbers that appear in the Fibonacci numbers?
A Python program that aggregates time usage from icalendar data
A Python script that compares the contents of two directories
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
Understand the probabilities and statistics that can be used for progress management with a python program
I tried to find out the difference between A + = B and A = A + B in Python, so make a note