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
>>> 

沒有留言:

張貼留言

MQTT Explorer 與 Node-Red 介面的實驗

 MQTT Explorer 與 Node-Red 介面的實驗 MQTT EXplorer 與 Node-Red介面的設定 (1) 設定  MQTT EXplorer Client    (2)        Node-Red LED ON  --> MQTT Explor...