2018年1月13日 星期六

Python: Function that takes a sequence of numbers and determines if all are different from each other

Python: Function that takes a sequence of numbers and determines if all are different from each other

Python Basic - 1: Exercise-1 with Solution

Write a Python function that takes a sequence of numbers and determines if all the numbers are different from each other.
Sample Solution:
def test_distinct(data):
  count = 0
  for k in data:
    for j in range(1, len(data) - 1):
      if k == j:
        count += 1
        if count == 2:
          return False
          return True
print(test_distinct([2,4,5,7,9]))
print(test_distinct([2,4,5,5,7,9]))

============ RESTART: F:/Python_APSC/py-ex2-basic-1.py ============
None
False
>>> 

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...