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

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...