现在,我有一个基础10到基本2转换器工作,但它总是打印没有在每次转换结束。base_two=0
def binary_recursion(base_ten):
global base_two
if base_ten==0:
print(base_two)
return
c=0
while base_ten//2**c>1:
c+=1
base_two+=10**c
if c==0:
print(base_two)
return
binary_recursion(base_ten-2**c)我尝试返回base_two,而不是打印它,但这不返回一个数字,它也只是返回零。有人能帮我找出我的错误吗?
发布于 2017-07-19 18:20:15
def node(document_info, next_node):
return {'data': document_info, 'next': next_node}
def insert(head, new_document_info):
#insert new document into the linked list head
#returns the head of the modified list
if head is None:
return node(new_document_info, None)
if new_document_info[1] <= head['data'][1]:
return node(new_document_info, head)
head['next'] = insert(head['next'], new_document_info)
return head 这是一种略为修改的插入排序方法,从我的回答到最后一个问题。您将从head = None开始,然后每次添加打印作业时执行head = insert(head, document_info)。或者在收集了你所有的打印工作之后,做一些类似的事情
head = None
for document_info in list_queue:
head = insert(head, document_info)发布于 2017-07-19 16:56:40
您不会向队列中添加新元素。假设list_queue是一个队列。
Queue有put函数来向其中添加一个新元素。
def make_job():
temp_list=[]
list_queue = Queue()
for line in print_list:
if line[:6]=='submit': #If the command is submit, then the file must be placed in its
a=line.split() #correct spot in the linked list
del(a[0])
list_queue.put(a)#Contains file id, and the time required to print
temp_list.append(list_queue)
organize(list_queue) #Places the file in its correct spot in the linked list
else:
break发布于 2017-07-19 17:31:53
Python模块有一个名为queue的类,它可以完成您想要的任务。对于您的情况,使用它看起来如下所示:
class Job(object):
def __init__(self, name, print_time):
self.name = name
self.print_time = print_time
def __lt__(self, other):
return self.print_time < other.print_time
import queue as _queue # Need to rename the module so it doesn't conflict with your 'queue' function
my_queue = _queue.PriorityQueue()
def make_job():
for line in print_list:
if line[:6]=='submit':
a=line.split()
del(a[0])
new_job=queue(a) # queue(a) now returns a Job, e.g. Job('101', 40), instead of a 2-element list
my_queue.put(new_job)
else:
break一旦构建了my_queue,那么对my_queue.get()的重复调用将返回print_time命令的Job。
如果您希望能够在不删除元素的情况下检查队列的内容(get删除它返回的元素),可以将Job附加到列表中,并在每次插入后调用list_queue.sort()。如果这是出于性能考虑,您可以自己在列表中找到合适的位置,并调用list_queue.insert(i, a)。但是,推迟使用Python的list.sort有一些优点,即它是稳定。
最后,如果您不想定义一个新的类,可以使用sorted或list.sort与自定义排序函数。这取代了我为__lt__定义的Job成员。
new_job=queue(a) # queue(a) is your original function, which returns 2-element lists like ['101', 40]
list_queue.append(new_job)
list_queue.sort(key=lambda a,b: return a[1]<b[1])https://stackoverflow.com/questions/45196207
复制相似问题