Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Nächste Überarbeitung
Vorhergehende Überarbeitung
modul:m323:learningunits:lu02:loesungen:pure1 [2024/03/28 14:07] – angelegt - Externe Bearbeitung 127.0.0.1modul:m323:learningunits:lu02:loesungen:pure1 [2024/08/28 13:31] (aktuell) kmaurizi
Zeile 9: Zeile 9:
     print('The sum of the numbers is:', sum_of_numbers(numbers))     print('The sum of the numbers is:', sum_of_numbers(numbers))
 </code> </code>
 +
 +oder 
 +
 +<code python>
 +def sum_of_numbers(numbers):
 +    """
 +    Recursively calculates the sum of a list of numbers.
 +
 +    :param numbers: A list of integers.
 +    :type numbers: list
 +    :return: The sum of the numbers in the list.
 +    :rtype: int
 +
 +    The function operates recursively, summing the first element of the list 
 +    with the sum of the remaining elements. If the list is empty, the function 
 +    returns 0 as the base case.
 +    """
 +    if not numbers:
 +        return 0
 +    return numbers[0] + sum_of_numbers(numbers[1:])
 +    
 +if __name__ == '__main__':
 +    numbers_list = [1, 2, 3, 4, 5]
 +    result = sum_of_numbers(numbers_list)
 +    print(f'The sum of the numbers is: {result}'   
 +</code>
 +
  
 ---- ----
 [[https://creativecommons.org/licenses/by-nc-sa/4.0/ch/|{{https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png}}]] (c) Kevin Maurizi  [[https://creativecommons.org/licenses/by-nc-sa/4.0/ch/|{{https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png}}]] (c) Kevin Maurizi 
  • modul/m323/learningunits/lu02/loesungen/pure1.1711631267.txt.gz
  • Zuletzt geändert: 2024/03/28 14:07
  • von 127.0.0.1