memorandums

日々の生活で問題解決したこと、知ってよかったことなどを自分が思い出すために記録しています。

Competitive Programmer's Core Skillsの最初の練習問題(Inventing Tests)がわからなかった...

暇つぶしにCourseraのCompetitive Programmer's Core Skillsという講座をかじっていました。

Competitive Programmer's Core Skills Offered by Saint Petersburg State University. During the cour www.coursera.org

1週目の動画を飛ばし見して、さあ、テストと思ったら。。。どうやってもできない。そもそも何を求められているのかがわからない。。。

やっとわかった。。。ところでこれを書いています。

こういう勉強系のネタバレはよくないと思うのですが、僕にみたいなおっちょこちょいさんがいたら。。。1問くらいならいいかな。。。と思い書いています。​

お題は以下。

=====引用開始=====

In this quiz, we aren't asking you to solve problems. Instead, you are given some problems and their incorrect solutions, and your task is to find tests where these solutions actually work incorrectly — by returning incorrect answer, working for too long, crashing, taking too much memory — anything that would give a verdict other than "Accepted". Consider that the time limit is 2 seconds, and the memory limit is 256 megabytes for all of the following problems.

This exercise will help you imagine tests for your own code. And it's actually very similar to "challenges" on many competitive programming platforms (e.g. Topcoder, Codeforces) — where after solving a problem you could earn additional score by breaking other's solutions to it. We hope you'll like it!

Next follows the first problem and the incorrect solution to it.

You are given a non-empty list of integers, and you need to find the maximum value among them. The length of the list is not greater than 100, and the absolute value of each element is not greater than 1000.

def solve(a):

 max = 0

 for x in a:

   if x > max:

     max = x

 return max

Implement a function called getTest. It should return a list on which the solve function works incorrectly. Note that the returned list must fit the restrictions in the statement.

The function is to be implemented in Python 3, but if you don't know this language, it's no problem — the sample code should give you the idea of how to do what you need.

def getTest():

 return [1, 2, 3]

=====引用終了=====

問題文によると「solve関数が与えられていて、そのsolve関数が失敗するようなリストを考えなさい」ってことです。

そもそも、答えの記入の仕方がよくわからないってところからスタートでした。getTest関数の入力欄がテスト実行できるようになっているので、solve関数をその中にいれてみて、solve関数を呼び出すように...つまり以下のように書くのかなぁ。。。とかやってみてもダメ。

def solve(a):

 max = 0

 for x in a:

   if x > max:

     max = x

 return max

 

def getTest():

 return solve([1, 2, 3])

ダメっていうのはどういうことかというと、この課題を提出すると「実行結果が正しいからこれは不正解です」っていうエラーが出るのでわかるっていう感じです。

つまり、問題がどうのこうのという以前に、解答方法がよくわからないのでハマりました。

solve関数は一見、正しく動作するように見えてしまうため。。。(←そもそもそう見える時点で冷静さを欠いていたと思います、情けない)。solveは正しいのに間違えるような入力データをどうやって考えればいいんだ。。。と。

冷静になって問題文とsolve関数を見てみれば、solve関数の欠陥がわかりました。

つまり、solve関数はmax変数の初期値を0としているため、0より小さな入力値に対しては最大値を正しく求めることができないわけです。

問題文をよく読めば、リストの各値は「the absolute value of each element is not greater than 1000」であると書かれています。つまり、絶対値は1000を越えることはないということは負の数字を与えてもいいわけです。

結局、答えの一例としては、以下のような感じになります。

def getTest():

 return [-1, -2, -3]

問題文をよく読めってことでした。はい。

Courseraには、こういうハマった学生を支援するためのディスカッション用のフォーラムが用意されています。残念ながら、このレベルで引っかかっている学生はいなかった(1人いたけど回答なしだった)。。。ので、解決できませんでした。

何となく思いますが、こういう、解答形式でつまずく人っているんだろうなぁ。。。と思います。もったいない話で。

どうしてもわからなかったら正解を見る的な選択肢があったらいいなぁ。。。と思います。単位の取得が目的じゃない利用者に対しては。

とりあえず、これで先に進めそうです。

お盆も終わってしまったけど、暇を見つけて進めてみたいと思います。

おしまい。