最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

加拿大瑞爾森大學(xué) Python習(xí)題 Lab 4

2023-08-24 15:50 作者:逃跑的叮當(dāng)貓  | 我要投稿

Lab 4?– CPS?106

?

?

This lab helps you practice recursion, string manipulation and numerical approximation.

?

1. ??Assume that L is?a list?with?one?element.?The?element?might?be?a?number?or?it?might?be?a?list?of one?element,?where?that?element?might?be?a?number?or?a?list?of one?element,?and?so?on.?Return the innermost number. You must use recursion. Your solution cannot use a loop.

Hint: type(52) == int is True.

?

Test your program with?the following:

[5]

[[55]]

[[[123]]]

[[[[[[[[[[[[18]]]]]]]]]]]]

?

2. ??Assume?that?T?is?a?tuple.?Return?a?tuple?which?is?the?reverse?of T.?You?must?use?recursion.?Your solution cannot use a loop?or?a?list?or?a built-in?reverse?function.?Hint:?Reverse?the?tuple?starting?at?the?second?element,?and?concatenate?to?the?end?of?that?reversed?tuple?the?first?element.

?

Test your program with?the following:

(1,?2,?3,?4,?5)

('red', 'green',?'blue',?'yellow')

?

3. ??Assume?that ?sub?and?bigstring?are?strings.?Return?True?if?sub?is?part?of?bigstring,?False?otherwise.?You?must?use?recursion.?Your ?solution ?cannot ?use ?a ?string ?funtion ?or ?the ?'in?operation'. Your?solution?cannot use?a loop. Hint:?check?if?sub?is?the?beginning?of??bigstring,?and if?not, consider the string obtained by removing the first letter from?bigstring.

?

4. ??Assume?that?phrase?is?a?string.?Return?the?number?of words?in ?phrase.?Assume?that?words?are?separated?by?one?or?more?blanks.?You?are?not?allowed?to?use?the?built-in?function?split()?For?example,??countwords('The?cat?is?black')?would?return?4.?countwords('The?cat?is?black')?would also return 4. countwords('A whitecat?and a ??black cow.') would also return 7.

?

Test your program with?the following:

'A??white cat and ?a ?black cow'

'a b?c de?f?g?h'

"Orange you glad I didn't say banana"

?

5. ??Return the first value of?n such?that?the?sum

Sum?= 4/1 - 4/3?+ 4/5?-?4/7?+?...?+ 4?*?(-1)**(n+1)/(2n?-?1)

is?within?epsilon?of math.pi?=?3.141592653589793.?For?example,?if epsilon?is?1,?then?return?1,?since 4 is within?1 of?pi. If?epsilon is 0.5 then return?2,?since?4?is not?within?0.5?of?pi but?4?-?4/3?= 2.67, and that is within?0.5?of?pi



代碼:

4-2 tuple

4-3 findsub

4-3-2 findsub


加拿大瑞爾森大學(xué) Python習(xí)題 Lab 4的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
永宁县| 长丰县| 台中县| 扎鲁特旗| 黄浦区| 莱州市| 文登市| 德安县| 慈利县| 永泰县| 定远县| 句容市| 泰顺县| 塔城市| 射洪县| 诸暨市| 射阳县| 梨树县| 大港区| 巴青县| 刚察县| 郓城县| 如皋市| 含山县| 涡阳县| 扬中市| 丹凤县| 麻阳| 万源市| 临海市| 陆川县| 庄浪县| 兴和县| 格尔木市| 阿瓦提县| 同心县| 定日县| 渭源县| 玉林市| 靖宇县| 楚雄市|