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. 如何使用git创建项目,创建分支

    git config -global user.name "Your name" git config -global user.email "you@example.c ...

  2. B树叶子节点split

    一.B-Tree索引的分裂 1. 创建测试表 SQL> create table split_tab (id number, name varchar2(100)); 表已创建. SQL> ...

  3. FROM CSDN TO CNBLOGS

    做出了一个愉快的决定,以后会将博客从CSDN迁移到CNBLOGS 旧地址:http://blog.csdn.net/fifa0329,文章并不多 原因如下: 我再次出现了该博客违反了网站规则被关闭的问 ...

  4. (greedy)Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. 7 个改变世界的 Java 项目

    英文链接:http://radar.oreilly.com/2011/07/7-java-projects.html 文章转载自:开源中国社区 http://www.oschina.net/news/ ...

  6. Appium 点击Android屏幕

    用driver.tap(1, 10, 10, 800); 点击屏幕,经常提示:An unknown server-side error occurred while processing the co ...

  7. (转)iOS Wow体验 - 第二章 - iOS用户体验解析(2)

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第二章译文精选的第二部分,其余章节将陆续放出.上一 ...

  8. (转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML

    In my recent post on How To Choose the Best XML Parser for Your iPhone Project, Saliom from the comm ...

  9. Mac OS X 下修改网卡地址和抵御 ARP 攻击

    用 Mac 系统有一段时间了,这里记录一下自己遇到的需要终端命令解决的问题. 网络环境绑定了原先机器的 MAC 地址,由于特殊原因,先把新机器的网卡地址改成原先那台. 在终端输入sudo ifconf ...

  10. ashx实现文件下载以及文件MD5码测试

    cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System ...