Digit Stack

In computer science, a stack is a particular kind of data type or collection in which the principal operations in the collection are the addition of an entity to the collection (also known as push) and the removal of an entity (also known as pop). The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure. In a LIFO data structure, the last element added to the structure must be the first one to be removed. Often a peek, or top operation is also implemented, returning the value of the top element without removing it.

We will emulate the stack process with Python. You are given a sequence of commands:
- "PUSH X" -- add X in the stack, where X is a digit.
- "POP" -- look and remove the top position. If the stack is empty, then it returns 0 (zero) and does nothing.
- "PEEK" -- look at the top position. If the stack is empty, then it returns 0 (zero).
The stack can only contain digits.

You should process all commands and sum all digits which were taken from the stack ("PEEK" or "POP"). Initial value of the sum is 0 (zero).

Let's look at an example, here’s the sequence of commands:
["PUSH 3", "POP", "POP", "PUSH 4", "PEEK", "PUSH 9", "PUSH 0", "PEEK", "POP", "PUSH 1", "PEEK"]

Input: A sequence of commands as a list of strings.

Output: The sum of the taken digits as an integer.

题目大义: 使用Python模拟一个栈, 有PUSH, POP, PEEK命令, PEEK命令返回栈顶元素, POP命令返回并删除栈顶元素, PUSH命令将数字压入栈中

 def digit_stack(commands):
sim_stack = []
total = 0
for each in commands:
if each[1] == 'U': #push
pos = each.index(' ')
sim_stack.append(int(each[pos + 1:]))
else:
if sim_stack:
total += sim_stack[-1] if each[1] == 'O': #pop
sim_stack.pop() return total

因为只有三个命令, 且命令的第二个字母不同, 所以以其区分命令

观摩cdthurman的代码

 def digit_stack(commands):
stack, sum = [],0
for cmd in commands:
if 'PUSH' in cmd.upper():
stack.append(int(cmd.strip()[-1]))
elif 'POP' in cmd.upper() and len(stack):
sum += stack.pop()
elif len(stack):
sum+=stack[-1]
return sum

使用了in, 无他

Digit Stack的更多相关文章

  1. [Swift]LeetCode402. 移掉K位数字 | Remove K Digits

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  2. JS数据结构及算法(一) 堆栈

    最近在看<学习JavaScript数据结构与算法>这本书,感觉自己又涨知识了 哈哈... 现在将自己看的做个总结,也是巩固理解. 栈:先进后出,新添加和待删除的元素都保存在栈顶.可以用数组 ...

  3. vs中“Stack around the variable was corrupted”的解决方案

    把 project->配置属性->c/c++->代码生成->基本运行时检查 为 默认值 就不会报本异常.具体原因正在研究中... 如果改为其他就有exception. exce ...

  4. Deep Learning 8_深度学习UFLDL教程:Stacked Autocoders and Implement deep networks for digit classification_Exercise(斯坦福大学深度学习教程)

    前言 1.理论知识:UFLDL教程.Deep learning:十六(deep networks) 2.实验环境:win7, matlab2015b,16G内存,2T硬盘 3.实验内容:Exercis ...

  5. 用matlab训练数字分类的深度神经网络Training a Deep Neural Network for Digit Classification

    This example shows how to use Neural Network Toolbox™ to train a deep neural network to classify ima ...

  6. Kaggle—Digit Recognizer竞赛

    Digit Recognizer 手写体数字识别  MNIST数据集 本赛 train 42000样例 test 28000样例,原始MNIST是 train 60000 test 10000 我分别 ...

  7. 【DeepLearning】Exercise: Implement deep networks for digit classification

    Exercise: Implement deep networks for digit classification 习题链接:Exercise: Implement deep networks fo ...

  8. Digit Division

    Digit Division Time limit: 1 s Memory limit: 512 MiB We are given a sequence of n decimal digits. Th ...

  9. [USACO09OPEN]牛的数字游戏Cow Digit Game 博弈

    题目描述 Bessie is playing a number game against Farmer John, and she wants you to help her achieve vict ...

随机推荐

  1. cf492A Vanya and Cubes

    A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. css备忘录(关于relative、absolute)

    父级用:position: relative; 子级才能用:position: absolute; relative里面用margin调位置: absolute里面用top.left.right.bo ...

  3. python3-day3(内置函数)

    1.内置函数 1>print(bytearray('王',encoding='utf8')) 2>print(bytes('王',encoding='utf8')) 3>bool(' ...

  4. JSF标签大全详解

    1. JSF入门 藉由以下的几个主题,可以大致了解JSF的轮廓与特性,我们来看看网页设计人员与应用程序设计人员各负责什么. 1.1简介JSF Web应用程序的开发与传统的单机程序开发在本质上存在着太多 ...

  5. Cuts the cake_hdu_2134.java

    Cuts the cake Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  6. JMeter基础概念

    JMeter 介绍:一个非常优秀的开源的性能测试工具. 优点:你用着用着就会发现它的重多优点,当然不足点也会呈现出来. 从性能工具的原理划分: Jmeter工具和其他性能工具在原理上完全一致,工具包含 ...

  7. IOS Layer的使用

    CALayer(层)是屏幕上的一个矩形区域,在每一个UIView中都包含一个根CALayer,在UIView上的所有视觉效果都是在这个Layer上进行的. CALayer外形特征主要包括: 1.层的大 ...

  8. Windows CMD命令之tasklist及taskkill

    Tasklist介绍 Tasklist"是 winxp/win2003/vista/win7/win8下的命令,用来显示运行在本地或远程计算机上的所有进程,带有多个执行参数. 使用格式 ta ...

  9. CocoaPods的一些理解

    在这片博客中,我将分享我从cocopods中学到的东西. 如果你使用Cocoapods,你的.gitignore文件中会有什么. 这个问题在debate on SO中被提及,但是我建议只追踪Podfi ...

  10. C#遍历Object各个属性含List泛型嵌套。

    同事遇到一个问题:在做手机app接口时,返回JSON格式,json里面的数据属性均是string类型,但不能出现NULL(手机端那边说处理很麻烦,哎).Model已经创建好了,而且model的每个属性 ...