2018年1月11日 星期四

Python: Find a distinct pair of numbers whose product is odd from a sequence of integer values

Python: Find a distinct pair of numbers whose product is odd from a sequence of integer values 從一系列整數對中找出一個產品是奇數的

Python Basic: Exercise-150 with Solution

Write a Python function to find a distinct pair of numbers whose product is odd from a sequence of integer values.
Sample Solution :-
def odd_product(nums):
  for i in range(len(nums)):
    for j in range(len(nums)):
      if  i != j:
        product = nums[i] * nums[j]
        if product & 1:
          return True
          return False
          
dt1 = [2, 4, 6, 8]
dt2 = [1, 6, 4, 7, 8]
print(dt1, odd_product(dt1));
print(dt2, odd_product(dt2));

=========== RESTART: F:/Python_APSC/py-ex-basic-150.py ===========
[2, 4, 6, 8] None
[1, 6, 4, 7, 8] True
>>> 

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...