如何通过跳数查找数组来分离奇数和偶数范围
How to Separate a Range of Odd and Even Numbers Through Searching the Array While Skipping Numbers
我想挑战自己并发展我的编程技能。我想创建一个程序,要求用户输入一个数字范围,其中奇数和偶数应该分开(最好通过搜索),并由指定的跳跃因子分开。
还应该允许用户选择他们是否愿意继续。如果是这样,他们可以重复进入新范围的过程。
例如,当程序是 运行 时,样本输入将是:
"Please enter the first number in the range": 11
"Please enter the last number in the range": 20
"Please enter the amount you want to jump by": 3
程序会输出:
"Your odd Numbers are": 11,17
"Your even Numbers are": 14,20
"Would you like to enter more numbers(Y/N)":
到目前为止,我所拥有的代码是这样的,但我无法将其组合在一起,希望能得到一些帮助。
import sys
print("Hello. Please Proceed to Enter a Range of Numbers")
first = int(input("please enter the first number in the range: "))
last = int(input("please enter the last number in the range: "))
jump = int(input("please enter the amount you want to jump by: "))
def mylist(first,last):
print("your first number is: ",first,"your last number is: ",last,"your jump factor is: ",jump)
def binarySearch (target, mylist):
startIndex = 0
endIndex = len(mylist) – 1
found = False
targetIndex = -1
while (not found and startIndex <= endIndex):
midpoint = (startIndex + endIndex) // 2
if (mylist[midpoint] == target):
found = True
targetIndex = midpoint
else:
if(target<mylist[midpoint]):
endIndex=midpoint-1
else:
startIndex=midpoint+1
return targetIndex
print("your odd Numbers are: ")
print("your even Numbers are: ")
input("Would you like to enter more numbers (Y/N)?")
N = sys.exit()
Y = first = int(input("please enter the first number in the range"))
last = int(input("please enter the last number in the range"))
jump = int(input("please enter the amount you want to jump by: "))
也许我遗漏了一些东西,但你不能用以下内容替换你的二进制搜索吗?
>>> list(filter(lambda x: x%2 == 1, range(11, 20 + 1, 3)))
[11, 17]
>>> list(filter(lambda x: x%2 == 0, range(11, 20 + 1, 3)))
[14, 20]
问题 - "So far what I have for code is this but am having trouble putting it together and would appreciate some help."
回答 -
首先,将输入和输出分组到函数中似乎是个好主意!然后把它放在一起就是小菜一碟!
def get_inputs():
bar = input('foo')
def process_inputs():
bar = bar + 1
def print_outputs():
print(bar)
if '__name__' == '__main__':
get_inputs()
process_inputs()
print_outputs()
你甚至可以在 while
循环中加入类似 if input('more? (Y/N):') == 'Y':
的东西。
我想挑战自己并发展我的编程技能。我想创建一个程序,要求用户输入一个数字范围,其中奇数和偶数应该分开(最好通过搜索),并由指定的跳跃因子分开。
还应该允许用户选择他们是否愿意继续。如果是这样,他们可以重复进入新范围的过程。
例如,当程序是 运行 时,样本输入将是:
"Please enter the first number in the range": 11
"Please enter the last number in the range": 20
"Please enter the amount you want to jump by": 3
程序会输出:
"Your odd Numbers are": 11,17
"Your even Numbers are": 14,20
"Would you like to enter more numbers(Y/N)":
到目前为止,我所拥有的代码是这样的,但我无法将其组合在一起,希望能得到一些帮助。
import sys
print("Hello. Please Proceed to Enter a Range of Numbers")
first = int(input("please enter the first number in the range: "))
last = int(input("please enter the last number in the range: "))
jump = int(input("please enter the amount you want to jump by: "))
def mylist(first,last):
print("your first number is: ",first,"your last number is: ",last,"your jump factor is: ",jump)
def binarySearch (target, mylist):
startIndex = 0
endIndex = len(mylist) – 1
found = False
targetIndex = -1
while (not found and startIndex <= endIndex):
midpoint = (startIndex + endIndex) // 2
if (mylist[midpoint] == target):
found = True
targetIndex = midpoint
else:
if(target<mylist[midpoint]):
endIndex=midpoint-1
else:
startIndex=midpoint+1
return targetIndex
print("your odd Numbers are: ")
print("your even Numbers are: ")
input("Would you like to enter more numbers (Y/N)?")
N = sys.exit()
Y = first = int(input("please enter the first number in the range"))
last = int(input("please enter the last number in the range"))
jump = int(input("please enter the amount you want to jump by: "))
也许我遗漏了一些东西,但你不能用以下内容替换你的二进制搜索吗?
>>> list(filter(lambda x: x%2 == 1, range(11, 20 + 1, 3)))
[11, 17]
>>> list(filter(lambda x: x%2 == 0, range(11, 20 + 1, 3)))
[14, 20]
问题 - "So far what I have for code is this but am having trouble putting it together and would appreciate some help."
回答 - 首先,将输入和输出分组到函数中似乎是个好主意!然后把它放在一起就是小菜一碟!
def get_inputs():
bar = input('foo')
def process_inputs():
bar = bar + 1
def print_outputs():
print(bar)
if '__name__' == '__main__':
get_inputs()
process_inputs()
print_outputs()
你甚至可以在 while
循环中加入类似 if input('more? (Y/N):') == 'Y':
的东西。