1
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?

CodeWars オススメ問題 #8

Last updated at Posted at 2024-10-24

はじめに

個人的に好きなアルゴリズム学習サイト「CodeWars」の問題をシェア。

週1くらいのペースで、全10回を目指す予定。

CodeWarsはいいぞ!の紹介はこちら

CodeWarsの始め方はこちら

今回は、「知識があれば簡単だけど、知らないとわからない」 内容です。

オススメ問題

問題

数字が与えられるため、ASCIIコードで文字データに変換する

65 --> 'A'
97 --> 'a'
48 --> '0'

難易度

分類は 8kyu です。
最も簡単なレベルですが、知識が必要とされます。

オススメの回答

「評価が高い回答」の中から、学びの多い回答をピックアップしてご紹介。

模範解答
export function getChar(c: number): string {
  return String.fromCharCode(c);
}

fromCharCodeは、ASCIIコード変換を行うメソッドです。

「ASCIIコード」「fromCharCode」この2つの知識があれば簡単な問題ですね。

天才の解答
export const getChar = String.fromCharCode;

引数を明示しないことにより、大幅に短くなった解答です。

getChar(引数)は、そのままString.fromCharCode(引数)を表します。

「追加の処理をしていない」ことが、引数を省略できるポイントですね。
「可読性の向上」にもつながるといえるでしょう。

「変数」と「引数」に関しては別記事にも書きました。

おわりに

以上、CodeWarsオススメ問題でした。

オススメ問題・解法など、ぜひコメントお待ちしております!

1
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
1
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?