2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

プログラマの考えが・・身につく本より(Python):条件付きの検索(最大値)

Last updated at Posted at 2016-04-27

前回に引き続き、検索問題を読んでいます。
ここでは、他の値との関係性のなかから値を検索し最大値を求めています。

条件付きの検索(最大値)

C++の解答コードがこちら
const int ARRAY_SIZE = 10;
int intArray[ARRAY_SIZE] = {4,5,9,12,-4,0,-57,30987,-287,1};
int highestValue = intArray[0];
for (int i = 1; i < ARRAY_SIZE; i++) {
    if (intArray[i] > highestValue) highestValue = intArray[i];

自分の解答

Python3
#!/usr/bin/env python
#coding:utf-8

def MaxSearch():
    ARRAY_NUMBER = [4,5,9,12,-4,0,-57,30987,-287,1]
    maxValue = ARRAY_NUMBER[0]
    for x in range(len(ARRAY_NUMBER)):
        if ARRAY_NUMBER[x] > maxValue:
            maxValue = ARRAY_NUMBER[x]

    print(maxValue)

・・・(ターミナル実行)
>>> from test31 import MaxSearch
>>> MaxSearch()
30987
>>> 

前回コメントを参考に
自分なりに短くしたつもりですが、最大値は求められました!

前回コメントより参考に
PythonのメソッドがないかPython標準ライブラリこちらで調べてみましたが、最大値を求めるメソッドらしきものはみあたらなかったので最大値は順に見ていく方がいいんでしょうかね?

2
2
9

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?