Differences between revisions 2 and 3
Revision 2 as of 2019-03-28 15:21:03
Size: 587
Comment:
Revision 3 as of 2019-03-28 15:22:54
Size: 4411
Comment:
Deletions are marked like this. Additions are marked like this.
Line 27: Line 27:
=== 解答例1 () === === 解答例1 (exam_04_02a_sum_and_average_with_exception.py) ===
Line 30: Line 30:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ==============================================================================
# * Copyright (c) 2018 IIJIMA, Tadashi
# * (IIJIMA Laboratory, Dept. of Science and Technology, Keio University).
# ==============================================================================
# ソフトウェア工学 期末試験 (追試) exam_04_02a_sum_and_average_with_exception.py
# Exam 04-01: Ctrl-Cが入力されるまで,整数を読み込み,総和と平均,分散を計算する
# 2019-02-06 飯島 正 (iijima@ae.keio.ac.jp)
# ==============================================================================
import statistics
# ==============================================================================
# ===== 【関数定義】 Ctrl+Cが入力されるまで,整数値をリストに読み込む =====
def input_sequence_of_int():
    result = list()
    while True :
        try:
            data = input( "整数を入力してください(終了は\"Ctrl-C\")>>> " )
        except KeyboardInterrupt:
            break
        else:
            try:
                int_data = int( float( data ) ) # 小数点を含む数の文字列への対応,一旦floatにしておく
            except ValueError:
                print( "整数値ではないので読み飛ばします" )
            else:
                result.append( int_data )
    return( result )
# ==============================================================================
def sum( int_list ):
    result = 0
    for data in int_list:
        result += data
    return( result )
# ==============================================================================
# ===== 【関数定義】 整数リストの平均値を返す =====
def average( int_list ):
    result = 0.0
    try:
        result = sum( int_list ) / len( int_list )
    except ZeroDivisionError:
        print("エラー: データ数がゼロです")
        raise ZeroDivisionError("エラー: ゼロでの割り算があり平均が求められません")
    return( result )
# ==============================================================================
# ===== 【関数定義】 整数リストの分散(母分散)を返す =====
def variance( int_list ):
    return( sum_of_square_error( int_list ) / ( len( int_list ) ) )
# ==============================================================================
# ===== 【関数定義】 整数リストの偏差平方和を返す =====
def sum_of_square_error( int_list ):
    means = average( int_list )
    result = float()
    for x in int_list:
        result += ( ( x -means ) ** 2 )
    return( result )
# ==============================================================================
# ===== 【メイン・プログラム】 =====
# ----- オープニングメッセージ -----
print( "Ctrl-Cが入力されるまで,整数値をリストに読み込む: " )
print( " ※整数以外は無視する" )
print()
Line 31: Line 93:
# ----- Xが入力されるまで,整数値をリストに読み込む -----
data_list = input_sequence_of_int()
print()

# ----- 読み込んだ結果を表示する -----
print( " 読み込んだ結果: ", data_list )
print()


# ----- 総和を表示する -----
print( " 総和: ", sum( data_list ) )


# ----- 平均を表示する -----
try:
    print( " 平均: ", average( data_list ) )
except ZeroDivisionError as e:
    print(e.args[0])

# ----- 分散を表示する -----
print( " 分散: ", variance( data_list ) )
print( "(比較:組込関数) 母分散:", statistics.pvariance( data_list ) )
# ==============================================================================


授業-ソフトウェア工学 > 2018年度 > 試験 > 期末試験 (追試) > 期末試験 (追試) 問題02


授業-2018年度-ソフトウェア工学-期末試験 (追試) 問題02

問題02:

*

(公開)



問題 02:

[ edit]


問題

[ edit]



解答例1 (exam_04_02a_sum_and_average_with_exception.py)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ==============================================================================
# * Copyright (c) 2018 IIJIMA, Tadashi
# *       (IIJIMA Laboratory, Dept. of Science and Technology, Keio University).
# ==============================================================================
# ソフトウェア工学 期末試験 (追試) exam_04_02a_sum_and_average_with_exception.py
# Exam 04-01: Ctrl-Cが入力されるまで,整数を読み込み,総和と平均,分散を計算する
#        2019-02-06 飯島 正 (iijima@ae.keio.ac.jp)
# ==============================================================================
import statistics
# ==============================================================================
# ===== 【関数定義】 Ctrl+Cが入力されるまで,整数値をリストに読み込む =====
def input_sequence_of_int():
    result = list()
    while True :
        try:
            data = input( "整数を入力してください(終了は\"Ctrl-C\")>>> " )
        except KeyboardInterrupt:
            break
        else:
            try:
                int_data = int( float( data ) ) # 小数点を含む数の文字列への対応,一旦floatにしておく
            except ValueError:
                print( "整数値ではないので読み飛ばします" )
            else:
                result.append( int_data )
    return( result )
# ==============================================================================
def sum( int_list ):
    result = 0
    for data in int_list:
        result += data
    return( result )
# ==============================================================================
# ===== 【関数定義】 整数リストの平均値を返す =====
def average( int_list ):
    result = 0.0
    try:
        result = sum( int_list ) / len( int_list )
    except ZeroDivisionError:
        print("エラー: データ数がゼロです")
        raise ZeroDivisionError("エラー: ゼロでの割り算があり平均が求められません") 
    return( result )
# ==============================================================================
# ===== 【関数定義】 整数リストの分散(母分散)を返す =====
def variance( int_list ):
    return( sum_of_square_error( int_list ) / ( len( int_list ) ) )
# ==============================================================================
# ===== 【関数定義】 整数リストの偏差平方和を返す =====
def sum_of_square_error( int_list ):
    means = average( int_list )
    result = float()
    for x in int_list:
        result += ( ( x -means ) ** 2 )
    return( result  )
# ==============================================================================
# ===== 【メイン・プログラム】 =====
# ----- オープニングメッセージ -----
print( "Ctrl-Cが入力されるまで,整数値をリストに読み込む: " )
print( "  ※整数以外は無視する" )
print()

# ----- Xが入力されるまで,整数値をリストに読み込む -----
data_list = input_sequence_of_int()
print()

# ----- 読み込んだ結果を表示する -----
print( "  読み込んだ結果: ", data_list )
print()


# ----- 総和を表示する -----
print( "  総和: ", sum( data_list ) )


# ----- 平均を表示する -----
try:
    print( "  平均: ", average( data_list ) )
except ZeroDivisionError as e:
    print(e.args[0])

# ----- 分散を表示する -----
print( "  分散: ", variance( data_list ) )
print( "(比較:組込関数)  母分散:", statistics.pvariance( data_list ) )
# ==============================================================================


SoftEng: 2018/Exam/04/02/IndexPage (last edited 2019-03-28 15:22:54 by TadashiIijima)