未验证 提交 01504306 编写于 作者: J Julia Li 提交者: GitHub

add py_firststep (#592)

Co-authored-by: Nyejuliali <61902434+yejuliali@users.noreply.github.com>
上级 65d0b46c
print("Today is " + "Monday")
print("Today:", "Monday")
print("Today is %s, and the main course of our dinner will be %s." % ("Monday", "Fish"))
\ No newline at end of file
print('Tom asked: "Where is my ball?"')
print("Jack's brother hided a ball behind Jack's body.")
print("Jack told his brother: \"Bob, don't do that. You should return the ball to Tom.\"")
print('Bob cried. Tom\'s sister tried to console him, but didn\'t succeed.')
print('Tom shouted loudly: "Return my ball! Otherwise, I will call you \'The Thief\'all the time!"')
print("Tom's sister said: \"No, he is a little boy, not a thief. Let's play together with the ball.\"")
\ No newline at end of file
print(6-2)
print(7.5/3)
print((3-1.12)*10/4*(2-3.325)+3*2-5/10)
\ No newline at end of file
print("The price of this %s is %d dollars." % ("hotdog", 4.0))
print("The price of this %s is %s dollars." % ("hotdog", 4.0))
print("The price of this %s is %d dollars." % ("meal", 12.25))
print("The price of this %s is %s dollars." % ("meal", 12.25))
\ No newline at end of file
print("Monday Food: " + str(2) + " Apples " + "and " + str(3) + " Carrots")
print("Monday Food:", 2, "Apples", "and", 3, "Carrots")
\ No newline at end of file
a = "1"
a = a + 2
print(a)
\ No newline at end of file
import keyword
print(keyword.kwlist)
\ No newline at end of file
length = 10
width = 5
space = length * width
print("Space is: %s square meters" % space)
\ No newline at end of file
if space > 30:
print("This is a big room.")
else:
print("This is a normal room.")
\ No newline at end of file
if space > 30:
print("This is a big room.")
elif space > 10:
print("This is a normal room.")
else:
print("This is a small room.")
\ No newline at end of file
arr = ["apple", "orange", "watermelon"]
i = 0
while i < len(arr):
print(arr[i])
i = i + 1
print("No more fruit.")
\ No newline at end of file
arr = ["apple", "orange", "watermelon"]
i = 0
while i < len(arr):
print(arr[i])
if arr[i] == "orange":
break;
i = i + 1
if i == len(arr):
print("No more fruit.")
\ No newline at end of file
a = "1"
a = int(a) + 2
print(a)
\ No newline at end of file
arr = [2, 3, "apple", 52, 'c', True]
print(arr)
\ No newline at end of file
arr = [2, 3, "apple", 52, 'c', True]
print(arr[-1])
\ No newline at end of file
arr = [1, 5, 8, 19, 3, 2, 14, 6, 8, 22, 44, 95, 78]
arr_new = arr
arr[0] = -3
print("arr is: ", arr)
print("arr_new is: ",arr_new)
\ No newline at end of file
arr = [1,5,8,19,3,2,14,6,8,22,44,95,78]
arr_new = arr.copy()
arr[0] = -3
print("arr is: ", arr)
print("arr_new is: ",arr_new)
\ No newline at end of file
program Hello;
begin
writeln ('Hello, world.');
end.
\ No newline at end of file
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
\ No newline at end of file
tn = 95
arr = [1, 5, 8, 19, 3, 2, 14, 6, 8, 22, 44, 95, 78]
i = 0
while i < len(arr):
if arr[i] == tn:
print("tn is element", i)
break
else:
i = i + 1
if i == len(arr):
print("failed")
\ No newline at end of file
for n in range(1,11):
print(n)
\ No newline at end of file
n=1
while n < 11:
print(n)
n = n + 1
\ No newline at end of file
for char in 'Python3':
print('char :', char)
\ No newline at end of file
snacks = ['Cake', 'Cookie', 'Ice-cream', 'Pudding']
for snack in snacks:
print('Today snack :', snack)
\ No newline at end of file
for i in range(1, 11):
print(i)
\ No newline at end of file
tn = 95
arr = [1, 5, 8, 19, 3, 2, 14, 6, 8, 22, 44, 95, 78]
for i in range(0, len(arr)):
if arr[i] == tn:
print("tn is element", i)
break
if i == len(arr):
print("failed")
\ No newline at end of file
tn = 165 # 这里可以是任意整数
found = False
for i in range(1,1001):
if i == tn:
print("secrete number is ", i)
found = True
break
if not found:
print("failed")
\ No newline at end of file
arr = [3, 5, 9, 7, 12, 15, 18, 32, 66, 78, 94, 103, 269] # 例表内数值可以随便改,只要保证有序排列即可
tn = 5 # 可以随便改,arr中有没有都可以
low = 0
high = len(arr) - 1
while low <= high:
m = int((high - low)/2) + low
if arr[m] == tn:
print("Succeeded! The target index is: ", m)
break
else:
if arr[m] < tn:
low = m + 1
else:
high = m - 1
if low > high:
print("Search failed.")
\ No newline at end of file
def hello_world():
print("hello world")
return
\ No newline at end of file
def test_list_param(arr):
for i in range(0, len(arr)):
arr[i] = arr[i] * 2
return arr
x_arr = [1, 2, 3, 4, 5]
y_arr = test_list_param(x_arr)
print("xArr is", x_arr)
print("yArr is", y_arr)
\ No newline at end of file
def test_list_param(arr):
for i in range(0, len(arr)):
arr[i] = arr[i] * 2
return
x_arr = [1, 2, 3, 4, 5]
print("Before function:", x_arr)
test_list_param(x_arr)
print("After function:", x_arr)
\ No newline at end of file
def binary_search(arr, tn):
low = 0
high = len(arr) - 1
while low <= high:
m = int((high - low) / 2) + low
if arr[m] == tn:
return m
else:
if arr[m] < tn:
low = m + 1
else:
high = m - 1
if low > high:
return -1
# 下面一行为打印提示语句,不要出现在代码示例里:
print("\n\nFollowing are output of code 10-3\n")
# 下面代码要显示在书中10-3部分
arr = [3, 5, 9, 7, 12, 15, 18, 32, 66, 78, 94, 103, 269]
tn = 5
result = binary_search(arr, tn)
if result >= 0:
print("Succeeded! The target index is: ", result)
else:
print("Search failed.")
# 下面一行为打印提示语句,不要出现在代码示例里:
print("\n\nFollowing are output of code 10-4\n") # TOBE IGNORED
# 下面代码要显示在书中10-4部分
arr = []
for i in range(1, 1001):
arr.append(i)
for tn in range(1, 1001):
result = binary_search(arr, tn)
if result >= 0:
print("Succeeded! The target index is: ", result)
else:
print("Search failed.")
\ No newline at end of file
from SearchAlgorithms import binary_search
\ No newline at end of file
from SearchAlgorithms import binary_search
arr_input = input("input the number sequence, separated by ',':")
arr_strs = arr_input.strip().split(',') # 输入的序列以逗号为分割,切分成一个List的若干元素
arr = list(map(int, arr_strs)) # 将一个元素类型为字符串的序列转换为类型为整型的序列
tn_input = input("input target number:")
tn = int(tn_input.strip())
result = binary_search(arr, tn)
if result >= 0:
print("Succeeded! The target index is: ", result)
else:
print("Search failed.")
\ No newline at end of file
from SearchAlgorithms import binary_search
while 1:
arr_input = input("input the number sequence, separated by ',':")
arr_strs = arr_input.strip().split(',')
arr = list(map(int, arr_strs))
tn_input = input("input target number:")
tn = int(tn_input.strip())
result = binary_search(arr, tn)
if result >= 0:
print("Succeeded! The target index is: ", result)
else:
print("Search failed.")
\ No newline at end of file
from SearchAlgorithms import binary_search
filePath = 'sequences_for_search.txt'
with open(filePath) as fp:
line = fp.readline()
while line:
tmps = line.strip().split('|') # 将读入的一行用“|”分隔为两段
if len(tmps) != 2: # 如果格式不对,则忽略此行
continue
arr_strs = tmps[0].strip().split(',') # 将“|”前的部分再以逗号进行分割
arr = list(map(int, arr_strs)) # 转化为整型List
tn = int(tmps[1]) # 将“|”后的部分读取为整型
result = binary_search(arr, tn)
if result >= 0:
print("Succeeded! The target index is: ", result)
else:
print("Search failed.")
line = fp.readline()
\ No newline at end of file
def test_scalar_param(a):
a = a * 2
return a
x = 3
y = test_scalar_param(x)
print("x is", x)
print("y is", y)
\ No newline at end of file
# 本文件与10-2完全一致
def binary_search(arr, tn):
low = 0
high = len(arr) - 1
while low <= high:
m = int((high - low) / 2) + low
if arr[m] == tn:
return m
else:
if arr[m] < tn:
low = m + 1
else:
high = m - 1
if low > high:
return -1
\ No newline at end of file
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16|5
2,3,7,8,12,14,15,26,37,44,53,61,78,80,94,99,104,106|106
7,9,18,21,33,34,37,39,40,41,49,51,69,107,123,458,699,723,875,1023|34
\ No newline at end of file
arr = list(range(1, 1001)) # 生成一个List,里面按顺序存储了1~1000这1000个元素
tn = 635 # 可以随便改
low = 0
high = len(arr) - 1
while low <= high:
m = int((high - low) / 2) + low
if arr[m] == tn:
# 把打印出目标数所在的位置下标改成直接打印出目标数
print("Succeeded! The target number is: ", arr[m])
break
else:
if arr[m] < tn:
low = m + 1
else:
high = m - 1
if low > high:
print("Search failed.")
\ No newline at end of file
tn = 635 # 可以随便改
low = 1
high = 1000
while low <= high:
m = int((high - low) / 2) + low
if m == tn:
# 打印出目标数
print("Succeeded! The target number is: ", m)
break
else:
if m < tn:
low = m + 1
else:
high = m - 1
if low > high:
print("Search failed.")
\ No newline at end of file
user_input = input("Your input: ")
print(user_input)
\ No newline at end of file
low = 1
high = 1000
while low <= high:
m = int((high - low) / 2) + low
print("My guess is", m)
# user_input是循环条件中被判断的变量,因此需要在循环之前先有一个值,否则循环会出错
user_input = ""
while user_input != '1' and user_input != '2' and user_input != '3':
print("\t\t1) Bingo! %s is the secret number! \n\
2) %s < the secret number.\n\
3) %s > the secret number." % (m, m, m))
user_input = input("Your option:")
user_input = user_input.strip()
if user_input == '1':
print("Succeeded! The secret number is %s." % m )
break
else:
if user_input == '2':
low = m + 1
else:
high = m - 1
if low > high:
print("Failed!")
\ No newline at end of file
low = 1
high = 1000
loop_num = 0 # 记录循环轮数
while low <= high:
m = int((high - low) / 2) + low
print("My guess is", m)
# userInput是循环条件中被判断的变量,因此需要在循环之前先有个值,否则循环会出错
user_input = ""
input_num = 0
while user_input != '1' and user_input != '2' and user_input != '3':
if input_num == 3:
print("\nYou input too many invalid options. Game over!")
exit(0)
print("\t\t1) Bingo! %s is the secret number! \n\
2) %s < the secret number.\n\
3) %s > the secret number." % (m, m, m))
user_input = input("Your option:")
user_input = user_input.strip()
input_num += 1
input_num = 0
loop_num += 1
if user_input == '1':
print("Succeeded! The secret number is %s.\n\
It took %s round to locate the secret number. \n" % (m, loop_num))
break
else:
if user_input == '2':
low = m + 1
else:
high = m - 1
if low > high:
print("Failed!Cannot got your secret number. Make sure it in range of [1, 1000].")
\ No newline at end of file
low = 1
high = 1000
loop_num = 0
while low <= high:
m = int((high - low) / 2) + low
loop_num += 1
print("[Loop %s]: My guess is %s" % (loop_num, m))
user_input = ""
while user_input != '1' and user_input != '2' and user_input != '3' :
print("\t\t1) %s == sn \n\
2) %s < sn.\n\
3) %s > sn." % (m, m, m))
user_input = input("Your option:")
user_input = user_input.strip()
if user_input == '1':
print("Succeeded! SN is: ", m)
break
else:
if user_input == '2':
low = m
else:
high = m
if low > high:
print("Failed!Cannot got your secret number. Make sure it in range of [1, 1000].")
\ No newline at end of file