Saturday, November 23, 2024

LeetCode 567 Permutation in String

コミュカレでイントロ Java の授業を取っていてだされた宿題に LeetCode 567 Permutation in String に近いものがあったのでトライしてみた。

11.2.1: LAB: All permutations of names

Write a program that lists all ways people can line up for a photo (all permutations of a list of Strings). The program will read a list of one word names into ArrayList nameList (until -1), and use a recursive method to create and output all possible orderings of those names separated by a comma, one ordering per line.

import java.util.Scanner;
import java.util.ArrayList;

public class PhotoLineups {

   // Method to create and output all permutations of the list of names
   public static void printAllPermutations(ArrayList<String> permList, ArrayList<String> nameList) {
      // Base case: when nameList is empty, print the current permutation
      if (nameList.size() == 0) {
         System.out.println(String.join(", ", permList));
         return;
      }

      // Iterate through the list and recursively generate all permutations
      for (int i = 0; i < nameList.size(); i++) {
         // Select the current name
         String currentName = nameList.get(i);

         // Create a new list of remaining names by removing the current name
         ArrayList<String> remainingNames = new ArrayList<>(nameList);
         remainingNames.remove(i);

         // Add the current name to the permutation list
         permList.add(currentName);

         // Recurse with the remaining names and current permutation list
         printAllPermutations(permList, remainingNames);

         // Backtrack: remove the last added name to try the next permutation
         permList.remove(permList.size() - 1);
      }
   }

   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      ArrayList<String> nameList = new ArrayList<>();
      ArrayList<String> permList = new ArrayList<>();
      String name;

      // Read names from the user until "-1" is entered
      while (true) {
         name = scnr.next().trim();
         if (name.equals("-1")) {
            break;  // Stop reading names when "-1" is encountered
         }
         nameList.add(name);  // Add name to the list
      }

      // Generate and print all permutations of the list of names
      printAllPermutations(permList, nameList);
   }
}

Tuesday, October 22, 2024

replit

 replit というブラウザ・ベースの IDE を使う機会があったので備忘録として。

元々、スマフォでもコーディングしたいということでスタートしたらしいですが、AI もインテグレートされていて便利ですな。

新規で Repl を作成:

環境を選択:

普通に使えます。Git 環境もあり:


別に自分の PC のローカル開発環境でも十分ですが、何も構築しないでブラウザーですぐに始められるというのはGood:

自分の リモートGithubを指定:

勝手に repo 作ってくれます:

Git add、commit、push もコマンドでなくても可能:


でも、サブスクライブ月々 $25 だって。。。

自分のローカルPC環境で十分です。。。

Sunday, May 19, 2024

Django + venv on Amazon Linux 2023 でアプリを作ってみた

 

sudo yum update

sudo yum install python3-pip

sudo pip3 install virtualenv


mkdir ~/shiroproject

cd ~/shiroproject

python3 -m venv myvenv

source myvenv/bin/activate             止めるときは deactivate だけ


pip install django


django-admin startproject my_first_matplot ~/shiroproject

settings.py にALLOWED_HOSTS = ['35.175.86.54']


~/shiroproject/manage.py runserver 0.0.0.0:8000 << 立ち上がるがmigrateしろと怒られる


ここからチュートリアルと同じ

python manage.py startapp polls


Thursday, May 9, 2024

Djangoにmatplotlibで円グラフ表示

Amazon Linux 2023 で再度トライ

sudo yum update

sudo yum install python3-pip

pip install django

python3 -m django --version

pip3 install matplotlib

django-admin startproject mymatplotlibsite

settings.py の ALLOWED_HOSTS = ['34.229.238.162']を編集

python3 manage.py runserver 0.0.0.0:8000 疎通確認

python3 manage.py startapp myfirstplot 

この後、views.py に下記をかいて表示させて確認(チュートリアル1参考)

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

今回はグラフを表示させたいだけなのでモデルは必要ないと思うがmodels.pyをチュートリアルと同じように作成(使わない前提)

アプリケーションをプロジェクトに含める(settings.py)

INSTALLED_APPS = [
    'myfirstplot.apps.MyfirstplotConfig',

python3 manage.py makemigrations myfirstplot
python3 manage.py sqlmigrate myfirstplot 0001
python3 manage.py migrate

モデルクラスにクラスメソッドを追加

adminサイトにログインできるユーザーを作成

python3 manage.py createsuperuser

myfirstplot/admin.py を編集

Wednesday, May 8, 2024

djangoproject.com のチュートリアル

djangoproject.com のチュートリアル(日本語が選べれるのが素晴らしい)

先日作ったEC2インスタンス上でvenv をもう一度(混乱したため。。。

mkdir ~/myprojectdir2

cd ~/myprojectdir2

python3 -m venv myprojectenv

source myprojectenv/bin/activate

pip install django gunicorn psycopg2-binary

django-admin startproject mysite ~/myprojectdir2

settings.py にALLOWED_HOSTS = ['35.175.86.54']

ここからチュートリアル

~/myprojectdir2/manage.py runserver 0.0.0.0:8000 << 立ち上がるがmigrateしろと怒られる

python manage.py startapp polls

下記のファイルにチュートリアルに従って編集・追加
polls/views.py
polls/urls.py
mysite/urls.py

python manage.py migrate

mysite/settings.py編集

python manage.py makemigrations polls

python manage.py sqlmigrate polls 0001

python manage.py migrate

python manage.py createsuperuser

polls/admin.py 編集





Part2 まで完了。EC2上でアプリが動いてます!

次の日 part 3 完了。下記のリンクにテンプレートのことが詳しく:
https://docs.djangoproject.com/ja/5.0/topics/templates/

Monday, May 6, 2024

Django + venv on Amazon Linux 2023

 Digital Ocean のチュートリアルがうまく動かないので Amazon Linux 2023 で再度トライ


sudo yum update

sudo yum install python3-pip

sudo pip3 install virtualenv

virtualenv your_project_name

source your_project_name/bin/activate

deactivate

ここからDitalOceanの続きをしようとpip install django gunicorn psycopg2-binaryしたがすでにある?

DitalOceanの"myprojectdir"が"myproject"

Step4から

django-admin startproject mysurvey ~/myproject

DitalOceanの"myproject"が"mysurvey"

~/myproject/manage.py makemigrations

~/myproject/manage.py migrate


~/myproject/manage.py createsuperuser

~/myproject/manage.py collectstatic

sudo ufw allow 8000 << 通らなかったが今回は無視

~/myproject/manage.py runserver 0.0.0.0:8000

インスタンスに穴をあける



開通!



Monday, April 22, 2024

EC2上にDockerをインストール(続き)

 先日の続き【Docker講座5】と【Docker講座6】の途中までだった。。。(

https://blog.shiro.net/2024/02/ec2docker.html

CloudTech というYouTubeチャンネルのDocker講座【Docker講座8】から

https://www.youtube.com/@cloudtech9882

こちらの方のチュートリアルから

git clone https://github.com/AWSCLOUDTECH/tutorial.git


■コマンドメモ docker run -it --rm myimage1



Test stage FROM alpine:3.13.5 AS test LABEL application=todobackend Install basic utilities RUN apk add --no-cache bash git ■コマンドメモ sudo systemctl status docker.service sudo systemctl start docker.service docker build -t myimage1 . << Docker イメージを作成 docker images << イメージの確認 docker history myimage1 RUN apk add --no-cache curl docker build -t myimage2 . docker inspect xxxxxx docker rmi -f xxxxxx docker images -q docker rmi -f `docker images -q` Create app user RUN addgroup -g 1000 app && \ adduser -u 1000 -G app -D app Copy and install application source and pre-built dependencies COPY --from=test --chown=app:app /build /build COPY --from=test --chown=app:app /app /app RUN pip3 install -r /build/requirements.txt -f /build --no-index --no-cache-dir RUN rm -rf /build Set working directory and application user WORKDIR /app USER app ■ビルド docker build -t todobackend-release . ■アプリケーション実行 docker run -it --rm -p 8000:8000 todobackend-release uwsgi --http=0.0.0.0:8000 --module=todobackend.wsgi --master


一応、Dockerイメージが立ち上がって、そこにインストールされているAppが動いた。が、、、いまいち理解していない。。。