当前位置:   article > 正文

Learn the basics of Python 3-Code Challenges:Loops(Advanced)

Learn the basics of Python 3-Code Challenges:Loops(Advanced)

   1.Coding question 1: Larger Sum

Create a function named larger_sum() that takes two lists of numbers as parameters named lst1 and lst2.
The function should return the list whose elements sum to the greater number. If the sum of the elements of each list are equal, return lst1.

  1. def larger_sum(list1,list2):
  2. sum1 = 0
  3. sum2 = 0
  4. for num1 in list1:
  5. sum1 += num1
  6. for num2 in list2:
  7. sum2 += num2
  8. if sum1 >= sum2:
  9. return list1
  10. else:
  11. return list2
  12. print(larger_sum([1, 9, 5], [2, 3, 7]))

 2.Coding question 2: Over 9000

Create a function named over_nine_thousand() that takes a list of numbers named lst as a parameter.
The function should sum the elements of the list until the sum is greater than 9000. When this happens, the function should return the sum. If the sum of all of the elements is never greater than 9000, the function should return total sum of all the elements. If the list is empty, the function should return 0.
For example, if lst was [8000, 900, 120, 5000], then the function should return 9020.

  1. def over_nine_thousand(lst ):
  2. sum = 0
  3. for num in lst:
  4. if sum <= 9000:
  5. sum += num
  6. else:
  7. break
  8. return sum
  9. print(over_nine_thousand([8000, 900, 120, 5000]))

 3.Coding question 3: Max Num

Create a function named max_num() that takes a list of numbers named nums as a parameter.
The function should return the largest number in nums

  1. def max_num(nums):
  2. maximum = nums[0]
  3. for num in nums:
  4. if num > maximum:
  5. maximum = num
  6. return maximum
  7. print(max_num([50, -10, 0, 75, 20]))

4.Coding question 4: Same Values

Write a function named same_values() that takes two lists of numbers of equal size as parameters.
The function should return a list of the indices where the values were equal in lst1 and lst2.
For example, the following code should return [0, 2, 3]
same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5])

  1. def same_values(lst1,lst2):
  2. new_list = []
  3. for index in range(len(lst1)):
  4. if lst1[index] == lst2[index]:
  5. new_list.append(index)
  6. return new_list
  7. print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))

 5.Coding question 5: Reversed List

Create a function named reversed_list() that takes two lists of the same size as parameters named lst1 and lst2.
The function should return True if lst1 is the same as lst2 reversed. The function should return False otherwise.
For example, reversed_list([1, 2, 3], [3, 2, 1]) should return True.

  1. def reversed_list(lst1, lst2):
  2. for index in range(len(lst1 )):
  3. if lst1[index] != lst2[len(lst1) - index - 1]:
  4. return False
  5. else:
  6. continue
  7. return True
  8. print(reversed_list([1, 2, 3], [3, 2, 1]))
  9. print(reversed_list([1, 5, 3], [3, 2, 1]))

Learn the basics of Python 3-Chapter 4:Loops​​​​​​

Learn the basics of Python 3-Code Challenges:Loops

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号