python中的“〜"是什么意思? [英] what does the '~' mean in python?

查看:137
本文介绍了python中的“〜"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'〜'在python中是什么意思?

what does the '~' mean in python?

我不久前在python中找到了这个BF解释器.

i found this BF interpreter in python a while ago.

import sys

#c,i,r,p=0,0,[0]*255,raw_input()

c=0   
i=0
p=raw_input()    
r=[0]*255 

while c<len(p):
    m,n,u=p[c],0,r[i]
    if m==">":i+=1
    if m=="<":i-=1
    if m=="+":r[i]+=1
    if m=="-":r[i]-=1
    if m==".":sys.stdout.write(chr(u))  
    if m=="[":
        if ~u:
            while 1:
                m=p[c]
                if m=="]":n-=1
                if m=="[":n+=1
                if ~n:break
                c+=1
    if m=="]":
        if u:
            while 1:
                m=p[c]
                if m=="]":n-=1
                if m=="[":n+=1
                if ~n:break
                c-=1    
    c+=1

我想知道它的作用,因为我想在ti 84(和PF上)上做一个

and i want to know what it does because i want to make one on my ti 84 (and a PF one)

BF是 http://en.wikipedia.org/wiki/Brainfuck 和PF是相似的

BF is http://en.wikipedia.org/wiki/Brainfuck and PF is something similar

推荐答案

在此特定情况下,只需将'〜'替换为'not'.

In this particular context, just replace '~' with 'not'.

PS.好吧,我猜我将不得不解释-开始被-1打耳光,可能是在我不知道逻辑取反和按位取反之间的区别的前提下.

PS. ok i guess i will have to explain - started getting slapped with -1's, probably on the premise i don't know the difference between logical and bitwise negation.

问题是,问题中的代码已损坏.其中有一个错误.如果您检查Brainfuck的工作方式,则在当前存储单元为!= 0时,它会在 [] 括号内循环(在输入 [和从] 返回之前进行优化).

The thing is, the code in the question is broken. There is a bug in it. If you check how Brainfuck should work, it loops within [ ] braces while the current memory cell is !=0 (this is checked as pre-condition when entering [ and as optimization before returning from ]).

但是,与其争论不休,也许更容易通过代码示例显示出来.让我们来看一个简单的程序'[+]'.尝试对此进行调整应该退出(因为当前单元格为0,所以它甚至都不会进入循环).相反,如果您在此解释器中运行它,则会陷入无限循环.

But instead of arguing, perhaps is easier to show with examples of the code not working. Let's take the simple program '[+]'. Trying to tun this should just exit (because current cell is 0, it won even enter the loop). Instead if you run it in this interpreter, it goes into infinite loop.

因此,如果现在我的澄清是有意义的,我将请您还原-1票;-)

So i'll kindly ask you to revert your -1 votes if my clarification makes sense now ;-)

这是解释器稍微美化了,并修复了~错误,我还添加了缺少的,输入:

Here is the interpreter slightly beautified, with fixed ~ bug and i also added the missing , input:

from sys import stdin, stdout

bfHelloWorld = '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.'

# http://esoteric.sange.fi/brainfuck/bf-source/prog/yapi.b
bfPiDigits = '''>  +++++ (5 digits)
[<+>>>>>>>>++++++++++<<<<<<<-]>+++++[<+++++++++>-]+>>>>>>+[<<+++[>>[-<]<[>]<-]>>
[>+>]<[<]>]>[[->>>>+<<<<]>>>+++>-]<[<<<<]<<<<<<<<+[->>>>>>>>>>>>[<+[->>>>+<<<<]>
>>>>]<<<<[>>>>>[<<<<+>>>>-]<<<<<-[<<++++++++++>>-]>>>[<<[<+<<+>>>-]<[>+<-]<++<<+
>>>>>>-]<<[-]<<-<[->>+<-[>>>]>[[<+>-]>+>>]<<<<<]>[-]>+<<<-[>>+<<-]<]<<<<+>>>>>>>
>[-]>[<<<+>>>-]<<++++++++++<[->>+<-[>>>]>[[<+>-]>+>>]<<<<<]>[-]>+>[<<+<+>>>-]<<<
<+<+>>[-[-[-[-[-[-[-[-[-<->[-<+<->>]]]]]]]]]]<[+++++[<<<++++++++<++++++++>>>>-]<
<<<+<->>>>[>+<<<+++++++++<->>>-]<<<<<[>>+<<-]+<[->-<]>[>>.<<<<[+.[-]]>>-]>[>>.<<
-]>[-]>[-]>>>[>>[<<<<<<<<+>>>>>>>>-]<<-]]>>[-]<<<[-]<<<<<<<<]++++++++++.
'''

code = bfPiDigits   # the code
data = [0] * 255    # data memory
cp = 0              # code pointer
dp = 0              # data pointer

while cp < len(code):
    cmd = code[cp]
    if   cmd == '>': dp += 1
    elif cmd == '<': dp -= 1
    elif cmd == '+': data[dp] += 1 
    elif cmd == '-': data[dp] -= 1 
    elif cmd == '.': stdout.write(chr(data[dp]))
    elif cmd == ',': data[dp] = ord(stdin.read(1))
    elif cmd == '[' and not data[dp]: # skip loop if ==0
        n = 0
        while True:
            cmd = code[cp]
            if   cmd == '[': n += 1
            elif cmd == ']': n -= 1
            if not n: break
            cp += 1
    elif cmd == ']' and data[dp]:  # loop back if !=0
        n = 0
        while True:
            cmd = code[cp]
            if   cmd == '[': n+=1
            elif cmd == ']': n-=1
            if not n: break
            cp -= 1
    cp += 1

这篇关于python中的“〜"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆