Week 3: Structured Types 5. Tuples and Lists Exercise: odd tuples

Posted 爱学英语的程序媛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Week 3: Structured Types 5. Tuples and Lists Exercise: odd tuples相关的知识,希望对你有一定的参考价值。

Exercise: odd tuples

5/5 points (graded)

ESTIMATED TIME TO COMPLETE: 5 minutes

Write a procedure called oddTuples, which takes a tuple as input, and returns a new tuple as output, where every other element of the input tuple is copied, starting with the first one. So if test is the tuple (‘I‘, ‘am‘, ‘a‘, ‘test‘, ‘tuple‘), then evaluating oddTuples on this input would return the tuple (‘I‘, ‘a‘, ‘tuple‘)

def oddTuples(aTup):
  ‘‘‘
  aTup: a tuple

  returns: tuple, every other element of aTup.
  ‘‘‘
  # Your Code Here
  return aTup[::2]

一开始写错成return oddTuples[::2] 这样子报错:TypeError: ‘function‘ object is not subscriptable。 注意,我们要分隔的是aTup这个元祖,不是oddTuples这个函数。

答案二:

 


def oddTuples(aTup):
    ‘‘‘
    aTup: a tuple
    
    returns: tuple, every other element of aTup. 
    ‘‘‘
    # a placeholder to gather our response
    rTup = ()
    index = 0

    # Idea: Iterate over the elements in aTup, counting by 2
    #  (every other element) and adding that element to 
    #  the result
    while index < len(aTup):
        rTup += (aTup[index],)
        index += 2

    return rTup

以上是关于Week 3: Structured Types 5. Tuples and Lists Exercise: odd tuples的主要内容,如果未能解决你的问题,请参考以下文章

Spark Structured Streaming - 1

神经结构化学习 3 使用合成图进行训练 Neural Structured Learning - Part 3: Training with synthesized graphs

Spark Structured Streaming 2.3.0 中的水印

大数据Spark Structured Streaming

3,Structured Streaming使用checkpoint进行故障恢复

大数据Spark Structured Streaming集成 Kafka