Why do I get "None" as output for a function? Also, how to make a function run for both strings and numbers?

huangapple go评论67阅读模式
英文:

Why do I get "None" as output for a function? Also, how to make a function run for both strings and numbers?

问题

问题是:

> 编写一个名为merge的函数,该函数接受两个已排序的可能长度不同的列表,并将它们合并成一个单独的已排序列表,而不使用内置的排序函数

我尝试过:

list1=(input("Enter the elements of list :")).split()
list2=(input("Enter the elements of list :")).split()

merged_list,sorted_list=(list1+list2),[]

#不使用sort()方法

def merge(list1,list2):

    global merged_list,sorted_list

    if len(merged_list)==0 : return sorted_list

    sorted_list.append(min(merged_list))

    merged_list.remove(min(merged_list))

    merge(list1,list2)

print(merge(list1,list2))

这会输出 "None",我不知道为什么。而且,它适用于字母字符串,但不适用于数字。例如,如果你输入 [02,1,0123],它会返回 [0123,02,1]。我应该做哪些改变才能使它适用于字符串和数字?

英文:

The question is :

> Write a function called merge that takes two already sorted lists of possibly different lengths, and merges them into a single sorted list, without using the inbuilt sort function

I tried :

list1=(input("Enter the elements of list :")).split()
list2=(input("Enter the elements of list :")).split()

merged_list,sorted_list=(list1+list2),[]

#Without using sort()method 

def merge(list1,list2):

    global merged_list,sorted_list

	if len(merged_list)==0 : return sorted_list

	sorted_list.append(min(merged_list))

   	merged_list.remove(min(merged_list))

    merge(list1,list2)

print(merge(list1,list2))

This gives output as "None", I don't know why. Also, it works well for alphabetic strings, but doesn't work for numerics, For example, if you give [02,1,0123] ,it returns [0123,02,1]. What should I change to make it work for both strings and numbers?

答案1

得分: 0

这不完全是你要求的,但我会将其作为示例添加,包括:

  1. 从递归调用返回
  2. 避免不必要的全局变量

def merge(list1, list2):
    # 基本情况:一个或两个列表为空
    if len(list1) == 0 or len(list2) == 0:
        return list1 + list2

    if list1[0] < list2[0]:
        return [list1[0]] + merge(list1[1:], list2)
    else:
        return [list2[0]] + merge(list1, list2[1:])

list1 = [1, 4, 6, 8, 14, 22]
list2 = [1, 2, 5, 9, 10, 15, 18, 20]

merge(list1, list2)
# [1, 1, 2, 4, 5, 6, 8, 9, 10, 14, 15, 18, 20, 22]
英文:

This isn't exactly what you are asking, but I'll add this as an example of:

  1. returning from the recursive calls
  2. avoiding the unnecessary global variable

:

def merge(list1,list2):
    # base case: one or both are empty lists
    if len(list1) == 0 or len(list2) == 0:
        return list1 + list2
    
    if list1[0] &lt; list2[0]:
        return [list1[0]] + merge(list1[1:], list2)
    else:
        return [list2[0]] + merge(list1, list2[1:])

    
list1 = [1, 4, 6, 8, 14, 22]
list2 = [1, 2, 5, 9, 10, 15, 18, 20]

merge(list1,list2)
# [1, 1, 2, 4, 5, 6, 8, 9, 10, 14, 15, 18, 20, 22]

huangapple
  • 本文由 发表于 2023年6月26日 10:43:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定