Reduce algorithm code length even if increase algorithm runtime complexity #4

Closed
opened 2023-05-16 01:43:51 +02:00 by Benjamin_Loison · 0 comments

The following diff (compared to 6dd2f42eb9) change the worst case runtime complexity to strictly linear (no coefficient different than one for the linearity) in the number of comments with at least one answer.

diff --git a/search_in_youtube_video_comments.py b/search_in_youtube_video_comments.py
index b4c3c29..52514d3 100755
--- a/search_in_youtube_video_comments.py
+++ b/search_in_youtube_video_comments.py
@@ -33,7 +33,7 @@ while True:
         snippet = item['snippet']
         treatComment(snippet['topLevelComment'])
         totalReplyCount = snippet['totalReplyCount']
-        if totalReplyCount > 5:
+        if totalReplyCount > 0:
             parentId = item['id']
             commentsNextPageToken = ''
             while True:
@@ -43,10 +43,6 @@ while True:
                 if not 'nextPageToken' in commentsData:
                     break
                 commentsNextPageToken = commentsData['nextPageToken']
-        else:
-            if totalReplyCount > 0:
-                for comment in item['replies']['comments']:
-                    treatComment(comment)
     if not 'nextPageToken' in data:
         break
     nextPageToken = data['nextPageToken']

Could also remove:

        totalReplyCount = snippet['totalReplyCount']
        if totalReplyCount > 0:

but then the complexity at runtime would become in the worst case strictly linear in the number of comments.

The following diff (compared to 6dd2f42eb9b19e17cf14638e45560c4b55d96337) change the worst case runtime complexity to strictly linear (no coefficient different than one for the linearity) in the number of comments with at least one answer. ```diff diff --git a/search_in_youtube_video_comments.py b/search_in_youtube_video_comments.py index b4c3c29..52514d3 100755 --- a/search_in_youtube_video_comments.py +++ b/search_in_youtube_video_comments.py @@ -33,7 +33,7 @@ while True: snippet = item['snippet'] treatComment(snippet['topLevelComment']) totalReplyCount = snippet['totalReplyCount'] - if totalReplyCount > 5: + if totalReplyCount > 0: parentId = item['id'] commentsNextPageToken = '' while True: @@ -43,10 +43,6 @@ while True: if not 'nextPageToken' in commentsData: break commentsNextPageToken = commentsData['nextPageToken'] - else: - if totalReplyCount > 0: - for comment in item['replies']['comments']: - treatComment(comment) if not 'nextPageToken' in data: break nextPageToken = data['nextPageToken'] ``` Could also remove: ```py totalReplyCount = snippet['totalReplyCount'] if totalReplyCount > 0: ``` but then the complexity at runtime would become in the worst case strictly linear in the number of comments.
This repo is archived. You cannot comment on issues.
No Label
1 Participants
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Benjamin_Loison/Search_in_YouTube_video_comments#4
No description provided.