[PYTHON] Memorandum / memo about programming learning / competitive programming site

Introduction

Recently, the following programming learning / competitive programming sites have become popular.

That's why I tried it a little, so I'll make a memorandum and memo of what I researched and studied.

Processing to store standard input numeric character strings in a multidimensional array

Processing to store each line of input in an array as shown below.

123 => arr[0][0]、arr[0][1]、arr[0][2]
456 => arr[1][0]、arr[1][1]、arr[1][2]
789 => arr[2][0]、arr[2][1]、arr[2][2]

If you can do this, you can do various things using this array.

C language program

#include <stdio.h>
#include <string.h>

#define MAX_ROW 1000
#define MAX_COL 1000

static int g_map[MAX_ROW][MAX_COL];

int main(int argc, char** argv){
  int count1, count2, len;
  char input_string[MAX_ROW];
  
  /*First standard input*/
  scanf("%s", input_string);
  len = strlen(input_string);
  
  for(count1 = 0; count1 < len; count1++){
    /*From the second time onward, perform standard input here*/
    if(count1 > 0){
      scanf("%s", input_string);
    }
    for(count2 = 0; count2 < len; count2++){
      /*From ASCII letters"0"(0x30)You can get the value by subtracting.*/
      g_map[count1][count2] = input_string[count2] - '0';
    }
  }
  
  /*display*/
  for(count1 = 0; count1 < len; count1++){
    for(count2 = 0; count2 < len; count2++){
      printf("%d", g_map[count1][count2]);
    }
    printf("\n");
  }
  
  return 0;
}

When using a dynamic array Is this one more versatile?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INPUT_STRING_LEN 1000

int **g_map;

int main(int argc, char** argv){
  int count1, count2, len;
  char input_string[INPUT_STRING_LEN];

  /*First standard input*/
  scanf("%s", input_string);
  len = strlen(input_string);

  /*Dynamic array allocation*/
  g_map = malloc(sizeof(int *) * len);
  for (int i = 0; i < len; i++) {
    g_map[i] = malloc(sizeof(int) * len);
  }
  
  for(count1 = 0; count1 < len; count1++){
    /*From the second time onward, perform standard input here*/
    if(count1 > 0){
      scanf("%s", input_string);
    }
    for(count2 = 0; count2 < len; count2++){
      /*From ASCII letters"0"(0x30)You can get the value by subtracting.*/
      g_map[count1][count2] = input_string[count2] - '0';
    }
  }

  /*display*/
  for(count1 = 0; count1 < len; count1++){
    for(count2 = 0; count2 < len; count2++){
      printf("%d", g_map[count1][count2]);
    }
    printf("\n");
  }
  
  /*Opening process*/
  for (int i = 0; i < len; i++) {
    free(g_map[i]);
  }
  free(g_map);

  return 0;
}

C ++ program

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(int argc, char** argv){
    string      input_string;
    const char* c_string;
    int         length, count1, count2;
    vector< vector<int> > arr;
    
    cin >> input_string;
    length = input_string.size();
    
    /*Dynamically resize using the resize function*/
    arr.resize(length);
    for(count1 = 0; count1 < length; count1++){
        arr[count1].resize(length);
    }
    
    for(count1 = 0; count1 < length; count1++){
        if(count1 > 0){
            cin >> input_string;
        }
        for(count2 = 0; count2 < length; count2++){
            c_string = input_string.c_str();
            arr[count1][count2] = c_string[count2] - '0';
        }
    }
    
    for(count1 = 0; count1 < length; count1++){
        for(count2 = 0; count2 < length; count2++){
            cout << arr[count1][count2];
        }
        cout << endl;
    }

    //It is unnecessary because the delete process is done in the destructor.

    return 0;
}

Python3 program

if __name__=="__main__":
    input_string = input()
    length = len(input_string)
    
    map_data = [[0 for i in range(length)] for j in range(length)]
    
    for count1 in range(0, length):
        if count1 > 0:
            input_string = input()
        for count2 in range(0, length):
            map_data[count1][count2] = int(input_string[count2])
    
    #display
    for count1 in range(0, length):
        for count2 in range(0, length):
            print(map_data[count1][count2], end='')
        print("")

Common Lisp program

(defun main ()
  (let (count1
        count2
        input-string
        str-length
        map-data)
    (setf input-string (read-line))
    (setf str-length (length input-string))
    (setf map-data (make-array `(,str-length ,str-length)))
    (dotimes (count1 str-length)
      (setf count2 0)
      (if (> count1 0)
          (setf input-string (read-line)))
      (loop :for char :across input-string
            :do (setf (aref map-data count1 count2) (digit-char-p char))
                (incf count2)))
    ;;display
    (dotimes (count1 str-length)
      (dotimes (count2 str-length)
        (format t "~d" (aref map-data count1 count2)))
      (format t "~%"))))

Java program

import java.util.*;

class Main{
    /*Constant declaration*/
    public static final int MAX_ROW = 1000;
    public static final int MAX_COL = 1000;
    
    /*MAIN function*/
    public static void main(String[]args){
        int question[][] = new int[MAX_ROW][MAX_COL];
        int count1, count2, len;
        String input_string;
        
        Scanner cin = new Scanner(System.in);
	
        input_string = cin.nextLine();
        len = input_string.length();
        
        for(count1 = 0; count1 < len; count1++){
            if(count1 > 0){
                input_string = cin.nextLine();
            }
            for(count2 = 0; count2 < len; count2++){
                question[count1][count2] = input_string.charAt(count2) - '0';
            }
        }
        
        /*display*/
        for(count1 = 0; count1 < len; count1++){
            for(count2 = 0; count2 < len; count2++){
                System.out.print(question[count1][count2]);
            }
            System.out.println("");
        }
    }
}

C # program

using System.IO;
using System;

class Program
{
    /*Constant definition*/
    public const int MAX_ROW = 1000;
    public const int MAX_COL = 1000;
    
    /*MAIN function*/
    static void Main()
    {
        int[,] question = new int[MAX_ROW, MAX_COL];
        int count1, count2, len;
        String input_string;
	
        input_string = Console.ReadLine();
        len = input_string.Length;
        
        for(count1 = 0; count1 < len; count1++){
            if(count1 > 0){
                input_string = Console.ReadLine();
            }
            for(count2 = 0; count2 < len; count2++){
                question[count1, count2] = input_string[count2] - '0';
            }
        }
        
        /*display*/
        for(count1 = 0; count1 < len; count1++){
            for(count2 = 0; count2 < len; count2++){
                Console.Write(question[count1, count2]);
            }
            Console.WriteLine("");
        }
    }
}

Ruby program

if __FILE__ == $0
    input_string = gets.chomp
    length = input_string.length - 1
    
    map_data = Array.new
    
    (0..length).each do |count1|
        if count1 > 0 then
            input_string = gets.chomp
        end
        map_data << input_string.split("").map(&:to_i)
    end
    
    #display
    (0..length).each do |count1|
        (0..length).each do |count2|
            print map_data[count1][count2]
        end
        puts ""
    end
end

Finally

I wrote a program in 5 different languages (C, Python3, Common Lisp, Java, C #). (I think there is a better way to write it ...) This is a preparatory program to prepare the data before writing the algorithm. Actually, we will solve the problem using the data stored in the array from here. This time it was an array, but there are various other data structures such as lists, queues, stacks, hashes, etc., so it would be nice if you could select it according to the problem.

Recommended Posts

Memorandum / memo about programming learning / competitive programming site
Python Competitive Programming Site Summary
LPIC201 learning memo
Learn about programming
Django Learning Memo
Deep Learning Memorandum
Python learning site
[Memo] Machine learning
Memorandum about validation
Python class (Python learning memo ⑦)
Python module (Python learning memo ④)
I tried competitive programming
About machine learning overfitting
Programming study site summary
A memorandum about matplotlib
[Python] Memo about functions
Competitive programming diary python 20201213
A memorandum about Nan.
Programming learning record day 2
[Python] Memo about errors
Machine learning course memo
Competitive programming diary python 20201220
Competitive programming with python
Flask site construction memorandum
Competitive programming diary python