您的位置:首页 > 其它

jeapedu 19 字符串算术运算习题答案

2018-02-27 15:04 375 查看
链接: https://pan.baidu.com/s/1eUlMJSI 密码: dk43

# 1 print jeapedu000~jeapedu100
# 2 count "c" in "aacacaac" * 100 count and pos
# 3 random.randint(100000,999999)
#   1) if 4 and 7 in it, otherwise drop the num
#   2) when not 4 and 7 in it, whether 6 and 8 in it

# 1 print jeapedu000~jeapedu100
#jeapedu000
#jeapedu001
#jeapedu009
#...
#jeapedu010
#jeapedu011
#jeapedu099
#...
#jeapedu100
# while
i = 0
while i <= 100:
if i < 10:
print("jeapedu00" + str(i))
elif i < 100:
print("jeapedu0" + str(i))
else:
print("jeapedu" + str(i))
i +=1
# str.zfill(width) str method
i = 0
while i <= 100:
s = str(i)
s = s.zfill(3)
print("jeapedu" + s)
#print("jeapedu" + str(i).zfill(3))
i += 1
# num formatting
i = 0
while i <= 100:
s = "%03d" %i
print("jeapedu" + s)
#print("jeapedu" + "%03d" %i)
i += 1
# num format function
i = 0
while i <= 100:
s = "{0:03d}".format(i)
print("jeapedu" + s)
i += 1

# 2 count "c" in "aacacaac" * 100 count and pos
s = "aacacaac" * 100
i = 0
count = 0
while i < len(s):
if s[i] == "c":
print("found", s[i], i)
count += 1
i += 1
print(count)

# 3 random.randint(100000,999999)
#   1) if 4 and 7 in it, otherwise drop the num
#   2) when not 4 and 7 in it, whether 6 and 8 in it
import random
i = 0
while i < 20:
x = random.randint(100000,999999)
s = str(x)
j = 0
if "4" in s or "7" in s:
print(s, end=' ')
while j < len(s):
if s[j] == "4" or s[j] == "7":
print(s[j], j, end=' ')
j += 1
elif "6" in s or "8" in s:
print(s, end=' ')
while j < len(s):
if s[j] == "6" or s[j] == "8":
print(s[j], j, end=' ')
j += 1

print("")
i += 1

import random
x = random.randint(100000, 999999)
print(x + 100, type(x), str(x), type(str(x)))
s = str(x)
i = 1
while i < 20:
x = random.randint(100000, 999999)
s = str(x)
#if "4" in s:
#print(i, "has 4 in", s)
if "4" in s and "7" not in s:
print(i, "has 4 in", s)
else:
print(i, s)
i += 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: