翻译1.22&&1.23Infinite Dictionaries

原题连接:Infinite Dictionaries - C语言网

A dictionary is a set of key-value pairs, for example:

 

{'color':'red', 'price':2, 7:'test', 100:-100}

 //一个字典是一组关键对,例如{'color':'red', 'price':2, 7:'test', 100:-100}

As you can see, keys and values can be strings or integers. What’s more, values can also be dictionaries or variable references. Here is the formal definition of terms that will be used soon:

//你可以看到,关键词和价值可以是字符串或者是数字。另外,价值也可以是字典或者变化的指代。下面是将要用到的东西的正式定义:

 

key   ::=      INTEGER | STRING

value ::=      INTEGER | STRING | dict

pair  ::=      key ':' value

dict  ::=      '{' [pair (',' pair)*] '}'

var   ::=      'a'|'b'|'c'|...|'z'

slot  ::=      var('[' key ']')*

lvar  ::=      slot

rvar  ::=      slot | value

 

Here ('[' key ']')* means zero or more subscripts, [pair (',' pair)*] means zero or more key-value pairs.

//('[' key ']')*表示零或者更多的subscript[pair (',' pair)*]表示零或者更多的关键价值对。

 

Strings are always enclosed by single quotes ('') and consists of up to 10 lower-case letters. Integers always have absolute values of no more than 1000.

//字符串总被单引号括起来并且由至多10个小写字母组成。整数的绝对值不大于1000.

You can insert spaces anywhere, except inside strings or integers. For example, { 'a':-1} and {'a' : -1   } are the same, but {'a b':1} and {'a':- 1} are both illegal.

 //你可以在任何地方加空格,除了字符串和整数之间。

例如{ 'a':-1} and {'a' : -1   }是一样的,但是 {'a b':1} and {'a':- 1}都不合法。

Your task is to execute a series of commands and print the results. There are 3 kinds of commands:

 //你的任务是计算一些指令并且输出结果。一共有三种指令:

1. Assignment: <lvar> = <rval>

 //Assignment:

After assigning a slot to a slot (rather than a value), the left-hand slot will be holding a reference to the right-hand.

//在把一个slot赋值给另一个slot(而不是一个value)之后,左手边的slot将会指向右手边的slot

For example, After executing the following commands, b[1][0] is 1, rather than 0:

//例如,在进行以下指令之后,b【i】【0】是1而不是0.

// 

a = {0:0}

b = {}

b[1] = a

a[0] = 1

 

Slots must be assigned before it is read or subscripted, and integers and strings cannot be subscripted. Consider the following comammd list:

//slot在读或带下标之前一定已经被赋值过,并且数字和字符串不能加下标志。思考以下命令表:

 

c = {}

c[0] = 3

c[1] = c[0]

d[0] = 'i'

c = d

d = c[1]['a']

c[2][2] = 2

 

The first three commands are legal, but the next two are both illegal because slot d must be assigned before it is read or subscripted.

//前三个指令是合法的,第四、五个指令都是不合法的因为slot d必须在读或加下标之前被赋过值。

The last three are also illegal.

 //最后两个也是不合法的。

2. Length: length(<slot>)

 

Output the number of key-value pairs in the slot. Note that nested pairs are not counted. For example:

 //输出在slot里面关键价值组的数量。注意嵌套组不计。例如:

a = {0: {0:0, 1:1}}

length(a)

 

will output 1, not 3. In this command, it is guaranteed that <slot> is storing a dictionary, not a string or an integer.

 //会输出1而不是3.在这条指令里面,保证slot是一个dictionary,而不是一个字符串或者一个数字。

3. Infinity test: test(<slot>)

 

If the slot can be subscripted indefinitely, output 1. Otherwise, output 0. For example, after executing the following command list:

 //如果slo可以被无线下标,输出1.不然,输出0.例如处理以下指令:

d = {}

d[0] = d

 

Then d is infinite, since d[0][0][0][0][0][0]... is always d. In this command, it is guaranteed that <slot> is storing a dictionary, not a string or an integer.

 //d就是无线,因为。。。。总是d。在这条指令里面,保证slot存载一个dictionary,而不是

字符串或整数

输入

 The input contains at most 10000 lines of commands, each line will be non-empty and will contain no more than 300 characters.

//输入至多10000条指令,每一行都不是空的并且包括不超过300个单词。

All the commands are legal.

//所有指令合法。

输出

 Print the output (one line for each length/test command).

//输出一行代表每个指令的长度

样例输入复制

c = {}

d = {'color': 'red', 'price': 2, 7: 'test', 100: -100} length(d)

d[7] = {'this': 'is', 'a': 'book'} length(d)

d[8] = {'this' : 'is', 'another' : {'a' : 'book', 'b': 'book2'} } length(d)

c[7] = c test(c) test(d) length(c) d[0] = c length(d) test(d[0])

样例输出复制
4 4 5 1 0 1 6 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值