首页 > 其他分享 >Introduction to Computer Science #Homework 06

Introduction to Computer Science #Homework 06

时间:2022-11-26 17:36:35浏览次数:42  
标签:do mysum return 8.0 Introduction Science len Computer 全局变量

Introduction to Computer Science

Homework 06

1. 程序是如何执行的

3.2.1

  1. 给寄存器R赋值20
  2. CPU将寄存器R中的值存回a所在的地址

3.2.2

将主存中1200地址处的值读取到寄存器R2中

3.2.3

shiftl R3,R1,04h

3.2.4

R1*12 = R1*22 + R123

shiftl R2,R1,02h

shiftl R3,R1,03h

add R3,R2,R3

3.2.5

load R1,(600)
load R2,(604)
sub R1,R1,R2
store (600),R1

3.2.6

算数右移:负数的表示为原码的反码+1,算数右移让所有新位元填1,是为了让右移后既有正数逻辑右移的性质,同时保持符号不改变,同样表示负数。

2. 局部变量与全局变量

  • 输出:8

  • 局部变量:d

3. 四则运算例子

分析:

  1. 全局变量a赋值3

  2. 全局变量b赋值2

  3. 全局变量c赋值1

  4. ab作为参数传给do_add()

  5. 【在do_add()中】

    1. 将全局变量c的值修改为a+b #5

    2. c1作为参数传给do_sub()

      • 【在do_sub()中】

        1. 局部变量c的值修改为a-b #4

        2. cc作为参数传给do_mul()

          • 【在do_mul()中】
            1. 将全局变量c的值修改为a*b #c*c=16
            2. 打印c:输出16
            3. 返回c
        3. do_mul()返回16,将16赋值到局部变量c

        4. c2作为参数传给do_div()

          • 【在do_div()中】
            1. 将局部变量c的值修改为a/b #8.0
            2. 打印c:输出8.0
            3. 返回c
        5. do_mul()返回8.0,将8.0赋值到局部变量c

        6. 打印c:输出8.0

        7. 返回c

    3. do_sub()返回8.0,将8.0赋值到全局变量c

    4. 打印c:输出8.0

  6. 此时全局变量c的值为8.0。打印c:输出8.0

综上,输出结果为:

16
8.0
8.0
8.0
8.0

4. 四则运算例子/改

分析:

  1. 全局变量a赋值3

  2. 全局变量b赋值2

  3. 全局变量c赋值1

  4. ab作为参数传给do_add()

  5. 【在do_add()中】

    1. 将局部变量c的值修改为a+b #5全局变量c=1不变

    2. c1作为参数传给do_sub()

      • 【在do_sub()中】

        1. 局部变量c的值修改为a-b #4

        2. cc作为参数传给do_mul()

          • 【在do_mul()中】
            1. 将全局变量c的值修改为a*b #c*c=16
            2. 打印c:输出16
            3. 返回c
        3. do_mul()返回16,将16赋值到局部变量c

        4. c2作为参数传给do_div()

          • 【在do_div()中】
            1. 将局部变量c的值修改为a/b #8.0
            2. 打印c:输出8.0
            3. 返回c
        5. do_mul()返回8.0,将8.0赋值到局部变量c

        6. 打印c:输出8.0

        7. 返回c

    3. do_sub()返回8.0,将8.0赋值到全局变量c

    4. 打印c:输出8.0

  6. 此时全局变量c的值为16。打印c:输出16

综上,输出结果为:

16
8.0
8.0
8.0
16

5. 嵌套函数局部与全局变量练习

a = 1; b = 2
def fun(x):
    def F():
        global a
        a = x+y+b
        #全局变量:a/b;局部变量:x/y
        return a
    y = 12  #局部变量
    x = x + 2  #局部变量
    a = F()  #局部变量
fun(b)  #全局变量
print("Finally,a is:%d and b is:%d" % (a,b))
———————————————————————————————————————
#Programming Result:
Finally,a is:18 and b is:2

6. Py. 删除字符串元素方法比较

import time

def remove(s,x):
    for i in range(len(s)):
        if s[i]==x:
            return(s[:i]+s[i+1:])
    return (s)

def removeall_1(s,x):
    i = 0
    while x in s:
        s = remove(s,x)
    return s

def removeall_2(s,x):
    i = 0
    while i < len(s):
        if s[i] == x:
            s = remove(s,x)
        else:
            i += 1
    return s
    
def removeall_3(s,x):
    s1 = ""
    for e in s:
        if e != x:
            s1 += e
    return s1

s="1"*1000000+"0"*1000

start1 = time.perf_counter()
a=removeall_1(s,"0")
elapsed1 = time.perf_counter()-start1
print("Method1's time",elapsed1)

start2 = time.perf_counter()
b=removeall_2(s,"0")
elapsed2 = time.perf_counter()-start2
print("Method2's time",elapsed2)

start3 = time.perf_counter()
c=removeall_3(s,"0")
elapsed3 = time.perf_counter()-start3
print("Method3's time",elapsed3)

———————————————————————————————————————
#Programming Result:
Method1's time 38.998721100000694
Method2's time 37.862995300001785
Method3's time 0.1028602000005776

7. Py. 参数传递问题练习

def Sum(L):
    mysum = 0; i = len(L) - 1
    while i >= 0:
        if L[i] % 2 == 0:
            mysum += L.pop(i)
        i = i - 1
    return mysum

L = [2, 2, 3, 4, 5]
mysum = Sum(L)
print(L,mysum)
———————————————————————————————————————
#Programming Result:
[3, 5] 8

原列表L被修改,变为[3,5]

#Method 1
def Sum(L):
    L1 = L.copy()
    mysum = 0; i = len(L1) - 1
    while i >= 0:
        if L[i] % 2 == 0:
            mysum += L1.pop(i)
        i = i - 1
    return mysum

L = [2, 2, 3, 4, 5]
mysum = Sum(L)
print(L,mysum)
———————————————————————————————————————
#Programming Result:
[2, 2, 3, 4, 5] 8
#Method 2
def Sum(L):
    L1 = L[:]
    mysum = 0; i = len(L1) - 1
    while i >= 0:
        if L[i] % 2 == 0:
            mysum += L1.pop(i)
        i = i - 1
    return mysum

L = [2, 2, 3, 4, 5]
mysum = Sum(L)
print(L,mysum)
———————————————————————————————————————
#Programming Result:
[2, 2, 3, 4, 5] 8
#Method 3
def Sum(L):
    mysum = 0; i = len(L) - 1
    while i >= 0:
        if L[i] % 2 == 0:
            mysum += L[i]
        i = i - 1
    return mysum

L = [2, 2, 3, 4, 5]
mysum = Sum(L)
print(L,mysum)
———————————————————————————————————————
#Programming Result:
[2, 2, 3, 4, 5] 8

8. Py. 列表推演表达式

L = ["%d * %d = %d" % (x, y, x*y) for x in range (1,10) for y in range (1,10)]

print(L)
———————————————————————————————————————
#Programming Result:
['1 * 1 = 1', '1 * 2 = 2', '1 * 3 = 3', '1 * 4 = 4', '1 * 5 = 5', '1 * 6 = 6', '1 * 7 = 7', '1 * 8 = 8', '1 * 9 = 9', '2 * 1 = 2', '2 * 2 = 4', '2 * 3 = 6', '2 * 4 = 8', '2 * 5 = 10', '2 * 6 = 12', '2 * 7 = 14', '2 * 8 = 16', '2 * 9 = 18', '3 * 1 = 3', '3 * 2 = 6', '3 * 3 = 9', '3 * 4 = 12', '3 * 5 = 15', '3 * 6 = 18', '3 * 7 = 21', '3 * 8 = 24', '3 * 9 = 27', '4 * 1 = 4', '4 * 2 = 8', '4 * 3 = 12', '4 * 4 = 16', '4 * 5 = 20', '4 * 6 = 24', '4 * 7 = 28', '4 * 8 = 32', '4 * 9 = 36', '5 * 1 = 5', '5 * 2 = 10', '5 * 3 = 15', '5 * 4 = 20', '5 * 5 = 25', '5 * 6 = 30', '5 * 7 = 35', '5 * 8 = 40', '5 * 9 = 45', '6 * 1 = 6', '6 * 2 = 12', '6 * 3 = 18', '6 * 4 = 24', '6 * 5 = 30', '6 * 6 = 36', '6 * 7 = 42', '6 * 8 = 48', '6 * 9 = 54', '7 * 1 = 7', '7 * 2 = 14', '7 * 3 = 21', '7 * 4 = 28', '7 * 5 = 35', '7 * 6 = 42', '7 * 7 = 49', '7 * 8 = 56', '7 * 9 = 63', '8 * 1 = 8', '8 * 2 = 16', '8 * 3 = 24', '8 * 4 = 32', '8 * 5 = 40', '8 * 6 = 48', '8 * 7 = 56', '8 * 8 = 64', '8 * 9 = 72', '9 * 1 = 9', '9 * 2 = 18', '9 * 3 = 27', '9 * 4 = 36', '9 * 5 = 45', '9 * 6 = 54', '9 * 7 = 63', '9 * 8 = 72', '9 * 9 = 81']

9. Py. 猜数字游戏

from random import randint

# 产生所有不重复的候选数字
def generateALL(length):
    L = []
    for i in range(10**length):
        t1 = str(i)
        len_t1 = len(t1)
        for j in range(length - len_t1):
            t1 = "0" + t1
        if not duplicate(t1):
            L.append(t1)
    return L


# 检查A中是否有重复的数字
def duplicate(A):
    for i in range(0,len(A)):
        for j in range(i+1,len(A)):
            if A[i] == A[j]:
                return True
    return False


# 在候选数字集合中随机猜一个数字
def guss_number_rand(L):
    if len(L) == 1:
        return L[0]
    i = randint(0,len(L)-1)
    return L[i]


# 在没有重复数字的情况下判断几A几B
def verifyAB(SL, GL):
    if len(SL) != len(GL):
        print("ERROR in verifyAB")
        return []
    a = 0
    b = 0
    for i in range(len(GL)):
        if GL[i] == SL[i]:
            a += 1
        elif GL[i] in SL:
            b += 1
    return [a, b]


# 筛选候选数字集合
def prune(Legal, G, Ans):
    L = []
    for e in Legal:
        a = verifyAB(e, G)
        if a == Ans:
            L.append(e)
    return L


# 游戏main函数
def main():
    print("猜数字游戏。A-数字与位置均正确;B-数字正确,位置不正确")
    for i in range(20):
        while True:
            S = input("猜的数字有几位:\n")
            SL = input("你的秘密数字:\n")
            if S.isdigit() and SL.isdigit():
                digit_num = int(S)
                if digit_num < 10 and len(SL) == digit_num:
                    break
        Legal_list = generateALL(digit_num)
        print("开始有 %d 个可能数" % (len(Legal_list)))
        Answer = [0, 0]
        i = 0
        while Answer[0] < digit_num:
            G1 = guss_number_rand(Legal_list)
            Answer = verifyAB(SL, G1)
            Legal_list = prune(Legal_list, G1, Answer)
            i += 1
            print("猜测第 %d 次:%s,答案:%dA%dB. 还有 %d 个可能数" % (i, G1, Answer[0], Answer[1], len(Legal_list)))
    return


main()
———————————————————————————————————————
#Programming Result:
猜数字游戏。A-数字与位置均正确;B-数字正确,位置不正确
猜的数字有几位:
3  [enter]
你的秘密数字:
986  [enter]
开始有 720 个可能数
猜测第 1 次:375,答案:0A0B. 还有 210 个可能数
猜测第 2 次:910,答案:1A0B. 还有 36 个可能数
猜测第 3 次:280,答案:1A0B. 还有 6 个可能数
猜测第 4 次:216,答案:1A0B. 还有 1 个可能数
猜测第 5 次:986,答案:3A0B. 还有 1 个可能数
猜的数字有几位:
4  [enter]
你的秘密数字:
2934  [enter]
开始有 5040 个可能数
猜测第 1 次:6908,答案:1A0B. 还有 480 个可能数
猜测第 2 次:7218,答案:0A1B. 还有 126 个可能数
猜测第 3 次:1953,答案:1A1B. 还有 27 个可能数
猜测第 4 次:4103,答案:0A2B. 还有 6 个可能数
猜测第 5 次:3942,答案:1A3B. 还有 1 个可能数
猜测第 6 次:2934,答案:4A0B. 还有 1 个可能数

标签:do,mysum,return,8.0,Introduction,Science,len,Computer,全局变量
From: https://www.cnblogs.com/LeeHero/p/16927832.html

相关文章