使用索引进行循环-hard_real-time_computing_systems
9.4使用索引进行循环前一节我用for循环来编写函数,因为我只需要处理字符串中的字符;我不必用索引做任对于下面的is_abecedarian,我们必须比较邻接的字符,用for循环来写的话有点棘手。� def is_abecedarian(word): previous = word [0] for c in word: if c < previous: return False previous = c return True �一种替代方法是使用递归:� def is_abecedarian(word): if len(word) word [1]: return False return is_abecedarian(word [1:]) �另一中方法是使用while循环: 2reduction to a previously solved problem
2.71MB
文件大小:
评论区