แนะนำ repository
ใกล้จะถึงตอนสุดท้ายแล้ว! แบบฝึกหัดนี้จะฝึกการใช้ set difference และนำไปประยุกต์ใช้ในการแนะนำ repository จากผู้ใช้คนที่สองให้กับผู้ใช้คนแรก
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์เครือข่ายระดับกลางใน Python
คำแนะนำการฝึกหัด
- เขียนฟังก์ชันชื่อ
recommend_repositories()ที่รับ argument 3 ตัว ได้แก่G,from_userและto_userแล้วคืนค่า repository ที่from_userมีการเชื่อมต่อ แต่to_userไม่มีการเชื่อมต่อ- หาชุด repository ที่
from_userเคยมีส่วนร่วม แล้วเก็บไว้เป็นfrom_reposโดยเริ่มจากการหา neighbors ของfrom_userจากนั้นใช้ฟังก์ชันset()กับผลลัพธ์ที่ได้ - หาชุด repository ที่
to_userเคยมีส่วนร่วม แล้วเก็บไว้เป็นto_repos - ใช้เมธอด
.difference()เพื่อคืนค่า repository ที่from_userมีการเชื่อมต่อ แต่to_userไม่มีการเชื่อมต่อ
- หาชุด repository ที่
- พิมพ์ repository ที่จะแนะนำ จาก
'u7909'ไปยัง'u2148'
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
def ____:
# Get the set of repositories that from_user has contributed to
from_repos = ____
# Get the set of repositories that to_user has contributed to
to_repos = ____
# Identify repositories that the from_user is connected to that the to_user is not connected to
return ____.____(____)
# Print the repositories to be recommended
print(____)