6-01: Model Solution: What if the largest number is smaller than 0?

オープン 初心者向け
#52 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
65/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
python
領域
content

調査の方向性

演習 6-01 のモデル解答を調べ、負の数だけを含むリストでその動作を再現してください。0 ではなく入力中の最大の数を返すように解答を更新し、その後も正の数と正負が混在する数を含む入力を処理できることを確認してください。

索引モデルが issue の本文から書いたものです。

説明

The model solution assigns 0 as the biggest number but the question didn't specify that the question will always have at least one non-negative number.

What if the list only have negative numbers?
In that case instead of returning the largest number, it will return 0.

def largest():
    with open("numbers.txt") as file:
        start = True
        biggest = 0
        for number in file:
            if start or int(number) > biggest:
                biggest = int(number)
                start = False
        return biggest

A simple solution would be to store the first number as the largest number instead. I am adding my solution but I believe this could be shortened:

def largest():
    with open("numbers.txt") as new_file:

        flag = True

        for line in new_file:
            line = int(line.replace("\n",""))

            #store the first value then skip all else
            if flag:
                largest_number = line
            flag = False

            if largest_number<line:
                largest_number = line
    return largest_number

if __name__ == "__main__":
    largest()
主要言語
JavaScript
スター
77
フォーク
30
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rage/programming-26 のほかの issue

rage/programming-26 の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。