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

沒有留言:

張貼留言

WOKWI ESP32 LED Control , Node-Red MQTT SQLITE  

WOKWI ESP32 LED Control ,  Node-Red  MQTT SQLITE   const char broker[] = "test.mosquitto.org" ; //const char broker[] = "br...