Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Python Pandas - ดึงวันในสัปดาห์จาก DateTimeIndex ด้วย dayofweek

    คำอธิบาย ใช้ property DateTimeIndex.dayofweek ดึงวันในสัปดาห์ (0=จันทร์, 6=อาทิตย์) ของแต่ละ timestamp ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-10-20 02:30:50, periods=6, tz=Australia/Sydney, freq=3D) print(DateTimeIndex...\n, datetimeindex) print(DateTimeIndex frequen

  2. โปรแกรมหาอัตราการขุดหินน้อยที่สุดใน K ชั่วโมงด้วย Python

    คำอธิบายปัญหา กำหนดลิสต์ piles (จำนวนหินแต่ละกอง) และ k ชั่วโมง ทุกชั่วโมงเลือกกองหนึ่งขุด r หิน (ถ้าน้อยกว่า r ขุดหมดก็ใช้ 1 ชั่วโมง) หา **ค่า r น้อยที่สุด** ที่ขุดหมดทุกกองภายใน ≤ k ชั่วโมง ตัวอย่าง piles = [3, 6, 4], k = 5 → Output: 3 r=3: กอง 6 ใช้ 2 ชม., กอง 4 ใช้ 2 ชม., กอง 3 ใช้ 1 ชม. รวม 5

  3. Python Pandas - ดึงไตรมาสของวันที่จาก DateTimeIndex ด้วย quarter property

    คำอธิบาย ใช้ property DateTimeIndex.quarter เพื่อดึงหมายเลขไตรมาส (1-4) ของแต่ละวันที่ใน DateTimeIndex ไตรมาสของปี Q1: 1 ม.ค. - 31 มี.ค. Q2: 1 เม.ย. - 30 มิ.ย. Q3: 1 ก.ค. - 30 ก.ย. Q4: 1 ต.ค. - 31 ธ.ค. ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-10-20 02:30:50, periods=

  4. Python Pandas - ดึงเขตเวลาจาก DateTimeIndex ด้วย tz property

    คำอธิบาย ใช้ property DateTimeIndex.tz เพื่อดึงข้อมูลเขตเวลา (timezone) ของ DateTimeIndex ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-10-20 02:30:50, periods=6, tz=Australia/Sydney, freq=D) print(DateTimeIndex...\n, datetimeindex) print(\nGet the timezone..\n, datetimeind

  5. โปรแกรมหาจำนวนเกาะที่หลุดจากขอบเมทริกซ์ไม่ได้ใน Python

    คำอธิบายปัญหา กำหนดเมทริกซ์ไบนารี: 1=ที่ดิน, 0=น้ำ จากที่ดินเดินได้ 4 ทิศทาง หาจำนวนเซลล์ที่ดินที่ **ไม่สามารถเดินออกนอกเมทริกซ์ได้** (ถูกล้อมโดยน้ำ/ขอบ) ตัวอย่าง 0 0 0 1 0 1 1 0 0 1 1 0 0 0 0 1 Output: 4 (บล็อก 2x2 กลาง อยู่ตำแหน่ง (1,1),(1,2),(2,1),(2,2) หลุดไม่ได้) ขั้นตอนการแก้ปัญหา (BFS จา

  6. Python Pandas - ดึงความถี่จาก DateTimeIndex ด้วย freq property

    คำอธิบาย ใช้ property DateTimeIndex.freq เพื่อดึงอ็อบเจกต์ความถี่ (DateOffset) ของ DateTimeIndex ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-10-20 02:30:50, periods=6, tz=Australia/Adelaide, freq=D) print(DateTimeIndex...\n, datetimeindex) print(DateTimeIndex frequency...

  7. Python Pandas - ดึงความถี่เป็นสตริงจาก DateTimeIndex ด้วย freqstr

    คำอธิบาย ใช้ property DateTimeIndex.freqstr เพื่อดึงความถี่ (frequency) ของ DateTimeIndex ในรูปแบบสตริง ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-10-20 02:30:50, periods=6, tz=Australia/Adelaide, freq=D) print(DateTimeIndex...\n, datetimeindex) print(DateTimeIndex frequ

  8. โปรแกรมหาจำนวน element ที่ลบได้เพื่อให้ผลรวมดัชนีคู่-คี่เท่ากันใน Python

    คำอธิบายปัญหา กำหนดลิสต์ nums ฟังก์ชัน f(i) ลบ element ตำแหน่ง i แล้วตรวจสอบว่าผลรวมดัชนีคู่ = ผลรวมดัชนีคี่ หรือไม่ หาจำนวน i ที่ทำให้ f(i) = True ตัวอย่าง nums = [6, 8, 5, 2, 3] → Output: 2 ลบ 8 (index 1): [6,5,2,3] → even: 6+2=8, odd: 5+3=8 ✓ ลบ 2 (index 3): [6,8,5,3] → even: 6+5=11, odd: 8+3=

  9. Python Pandas - ตรวจสอบวันที่ใน DateTimeIndex เป็นวันแรกของเดือนหรือไม่

    คำอธิบาย ใช้ property DateTimeIndex.is_month_start ตรวจสอบว่าแต่ละวันที่เป็นวันที่ 1 ของเดือนหรือไม่ ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-9-21 02:30:50, periods=6, tz=Australia/Adelaide, freq=5D) print(DateTimeIndex...\n, datetimeindex) print(\nCheck whether the da

  10. โปรแกรมหาจำนวนการดำเนินการน้อยสุดทำให้คู่หน้า-หลังผลรวมเท่ากันใน Python

    คำอธิบายปัญหา กำหนดลิสต์ nums ความยาวคู่ อนุญาตเปลี่ยนค่า element ใดก็ได้เป็นค่าใน [1, max(nums)] หาจำนวนการเปลี่ยนน้อยที่สุด เพื่อให้ nums[i] + nums[n-1-i] มีค่าเท่ากันทุก i ตัวอย่าง nums = [8,6,2,5,9,2] → Output: 2 เปลี่ยน nums[2]=2→5, nums[4]=9→4 ได้ [8,6,5,5,4,2] คู่: (8+2)=(6+4)=(5+5)=10 ขั้

  11. Python Pandas - ตรวจสอบวันที่ใน DateTimeIndex เป็นวันสุดท้ายของเดือนหรือไม่

    คำอธิบาย ใช้ property DateTimeIndex.is_month_end ตรวจสอบว่าแต่ละวันที่เป็นวันสุดท้ายของเดือนหรือไม่ ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-9-15 06:40:35, periods=6, tz=Australia/Adelaide, freq=15D) print(DateTimeIndex...\n, datetimeindex) print(\nCheck whether the da

  12. Python Pandas - ตรวจสอบวันที่ใน DateTimeIndex เป็นวันแรกของไตรมาสหรือไม่

    คำอธิบาย ใช้ property DateTimeIndex.is_quarter_start ตรวจสอบว่าแต่ละวันที่เป็นวันแรกของไตรมาสหรือไม่ ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-10-1 02:30:50, periods=6, tz=Australia/Adelaide, freq=30D) print(DateTimeIndex...\n, datetimeindex) print(\nCheck whether the d

  13. Python Pandas - ตรวจสอบวันที่ใน DateTimeIndex เป็นวันสุดท้ายของไตรมาสหรือไม่

    คำอธิบาย ใช้ property DateTimeIndex.is_quarter_end ตรวจสอบว่าแต่ละวันที่เป็นวันสุดท้ายของไตรมาสหรือไม่ ไตรมาสของปี Q1: 1 ม.ค. - 31 มี.ค. (สิ้นสุด 31 มี.ค.) Q2: 1 เม.ย. - 30 มิ.ย. (สิ้นสุด 30 มิ.ย.) Q3: 1 ก.ค. - 30 ก.ย. (สิ้นสุด 30 ก.ย.) Q4: 1 ต.ค. - 31 ธ.ค. (สิ้นสุด 31 ธ.ค.) ตัวอย่างโค้ด import

  14. โปรแกรมหาจำนวนควอดรูเพิลที่ผลคูณคู่แรกและคู่หลังเท่ากันใน Python

    คำอธิบายปัญหา กำหนดลิสต์ nums ตัวเลขบวกไม่ซ้ำกัน หาจำนวน quadruple (a,b,c,d) ที่ต่างกันทั้งหมด และ a*b = c*d เรียงลำดับสำคัญ (a,b) และ (c,d) สลับได้ ตัวอย่าง nums = [3, 6, 4, 8] → Output: 8 คู่ที่ให้ผลคูณ 24: (3,8) และ (4,6) → 8 แบบจัดเรียง ขั้นตอนการแก้ปัญหา นับความถี่ของผลคูณทุกคู่ c[product]

  15. Python Pandas - ตรวจสอบวันที่ใน DateTimeIndex เป็นวันแรกของปีหรือไม่

    คำอธิบาย ใช้ property DateTimeIndex.is_year_start ตรวจสอบว่าแต่ละวันที่เป็นวันที่ 1 มกราคม (วันแรกของปี) หรือไม่ ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-12-30 02:30:50, periods=6, tz=Australia/Adelaide, freq=1D) print(DateTimeIndex...\n, datetimeindex) print(\nCheck w

  16. Python Pandas - ตรวจสอบวันที่ใน DateTimeIndex เป็นวันสุดท้ายของปีหรือไม่

    คำอธิบาย ใช้ property DateTimeIndex.is_year_end ตรวจสอบว่าแต่ละวันที่เป็นวันที่ 31 ธันวาคม (วันสุดท้ายของปี) หรือไม่ ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-12-25 02:30:50, periods=6, tz=Australia/Adelaide, freq=2D) print(DateTimeIndex...\n, datetimeindex) print(\nChe

  17. จำนวนการเคลื่อนที่น้อยที่สุดเพื่อหนีเขาวงกตเมทริกซ์ใน Python

    คำอธิบายปัญหา กำหนดเมทริกซ์ไบนารี: 0 = ทางเดิน, 1 = กำแพง เริ่มที่ (0,0) หา **จำนวนเซลล์น้อยที่สุด** (รวมจุดเริ่มต้น) เพื่อไปถึง (R-1, C-1) หาไม่ได้คืน -1 ตัวอย่าง Matrix: 0 0 0 1 0 0 0 1 1 0 0 0 0 1 1 1 1 0 0 0 Output: 8 (เส้นทาง: (0,0)→(1,0)→(2,0)→(2,1)→(2,2)→(3,2)→(3,3)→(3,4) = 8 เซลล์) ขั้น

  18. Python Pandas - ตรวจสอบวันที่ใน DateTimeIndex เป็นปีอธิกสุรทินหรือไม่

    คำอธิบาย ใช้ property DateTimeIndex.is_leap_year ตรวจสอบว่าแต่ละวันที่ใน DateTimeIndex อยู่ในปีอธิกสุรทิน (leap year) หรือไม่ คืนค่า Boolean array ตัวอย่างโค้ด import pandas as pd datetimeindex = pd.date_range(2021-12-30 02:30:50, periods=6, tz=Australia/Adelaide, freq=3Y) print(DateTimeIndex...

  19. Python Pandas - ตรวจจับความถี่ของ DateTimeIndex ด้วย inferred_freq

    คำอธิบาย ใช้ property DateTimeIndex.inferred_freq เพื่ออนุมานความถี่ (frequency) ของ DateTimeIndex โดยอัตโนมัติ ตัวอย่างโค้ด import pandas as pd # DateTimeIndex 5 รอบ ความถี่ 3 ปี (YearEnd December) datetimeindex = pd.date_range(2021-10-30 02:30:50, periods=5, tz=Australia/Adelaide, freq=3Y) pri

  20. โปรแกรมตรวจสอบทุก sublist มีสมาชิกเฉพาะตัวอย่างน้อยหนึ่งตัวใน Python

    คำอธิบายปัญหา กำหนดลิสต์ nums ตรวจสอบว่า **ทุก sublist** มีองค์ประกอบอย่างน้อย 1 ตัวที่เกิดขึ้นเพียงครั้งเดียวใน sublist นั้น (unique element) ต้องแก้ใน O(n) time ตัวอย่าง nums = [5, 10, 20, 10, 0] → Output: True ทุก sublist 15 ชุด มี element ที่ frequency = 1 อย่างน้อย 1 ตัว ขั้นตอนการแก้ปัญหา (

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:393/451  20-คอมพิวเตอร์/Page Goto:1 387 388 389 390 391 392 393 394 395 396 397 398 399