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

沒有留言:

張貼留言

Node-Red & ModeBus FC=1

Node-Red & ModeBus FC=1 write address=0..7   8bits read address=16..23 8bits  [{"id":"cf7c1253d4d997dd","type&q...