0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ChatGPTで学校のレポートをどれだけ楽できるか

Last updated at Posted at 2024-04-03

はじめに

この記事は大阪工業大学 Advent Calendar 2022の18日目の記事です。
みなさんC演習Iは難しかったですか?
そこで一番難しかったであろう画像圧縮のレポートを退治していきたいと思います
今回用いるのはChat GPTです
Chat GPTは色々説明でてるので今はググってください あとで付け加えます

runLength圧縮とは

ランレングス圧縮とは、最も基本的な圧縮アルゴリズムの一つで、連続して現れる符号を、繰り返しの回数を表す値に置き換える方式。圧縮によって内容を損なわない可逆圧縮を行う[1]。

ChatGPTに聞いてみた

ChatGPTとは、AIと対話ができるものである。これを使って実際にレポートを手抜きしてみた

質問1

For example, when compressing the string "AAAABBBBCCCC", since "A", "B", and "C" are repeated four times in succession, each letter and its number of repetitions can be combined to represent the string as "4A4B4C". Such a method of representation is called run-length compression

回答1

Run-length encoding is a data compression method that is commonly used for compressing data that contains a lot of repeated values. In the example you gave, the string "AAAABBBBCCCC" is compressed by representing each letter and the number of times it is repeated in succession. For instance, the sequence of four "A"s is represented as "4A", and the sequence of four "B"s is represented as "4B". This allows the original string to be represented using a shorter string, which can be useful for reducing the amount of storage space needed for the data or for transmitting the data more efficiently.

質問2

Now please write an implementation in Clang.

回答2

Here is an implementation of run-length encoding in C++ using the Clang compiler:

#include <iostream>
#include <string>

std::string run_length_encode(const std::string& input) {
  std::string output;
  for (int i = 0; i < input.size(); ++i) {
    int count = 1;
    while (i + 1 < input.size() && input[i] == input[i + 1]) {
      ++i;
      ++count;
    }
    output += std::to_string(count) + input[i];
  }
  return output;
}

int main() {
  std::string input = "AAAABBBBCCCC";
  std::string encoded = run_length_encode(input);
  std::cout << "Encoded: " << encoded << std::endl;
  return 0;
}

This implementation works by iterating over the input string and counting the number of times that each value is repeated. When it encounters a new value, it adds the count and the value to the output string. For example, when it encounters the first "A" in the input string, it will add "1A" to the output string. When it encounters the next "A", it will increment the count and continue. When it encounters a "B", it will add "3A" to the output string (since there were three "A"s) and then start counting the number of "B"s.

質問3

Please show decoding of run-length compression in Clang.

回答3

Here is an implementation of run-length decoding in C++ using the Clang compiler:

#include <iostream>
#include <string>

std::string run_length_decode(const std::string& input) {
  std::string output;
  for (int i = 0; i < input.size(); ++i) {
    int count = 0;
    while (i < input.size() && isdigit(input[i])) {
      count *= 10;
      count += input[i] - '0';
      ++i;
    }
    for (int j = 0; j < count; ++j) {
      output += input[i];
    }
  }
  return output;
}

int main() {
  std::string input = "4A4B4C";
  std::string decoded = run_length_decode(input);
  std::cout << "Decoded: " << decoded << std::endl;
  return 0;
}

This implementation works by iterating over the input string and extracting the count and value for each encoded sequence. It does this by reading the digits of the count until it encounters a non-digit character, which is assumed to be the value. It then adds the value to the output string the specified number of times. For example, when it encounters "4A" in the input string, it will add "A" to the output string four times. When it encounters "4B", it will add "B" to the output string four times.

できているか確認

image.png

結果

今回はchatGPTにrunLength圧縮の例を最初に与えた上で紹介したが、これを行わなかった場合、c++での実装例を見せてもらうときに文法がおかしくなってしまったり途中で書くのを止めてしまうことがあった。
実装例を書いてもらうときにc言語で書いてくださいと指示したつもりがc++で書かれてしまった。
clangのところをcだけにしたらc言語で書いてくれるがなぜかtry againしても毎回network errorが起きてしまった。

参考文献

[1] IT用語辞典 ランレングス圧縮
https://e-words.jp/w/%E3%83%A9%E3%83%B3%E3%83%AC%E3%83%B3%E3%82%B0%E3%82%B9%E5%9C%A7%E7%B8%AE.html

ChatGPT
https://chat.openai.com/chat

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?