From 7c22a7d5e11fc5970d03f6503119b785dc9661ee Mon Sep 17 00:00:00 2001 From: ankitabmungalpara Date: Thu, 27 Feb 2025 16:47:50 -0500 Subject: [PATCH 1/2] Create article-views-i.py --- article-views-i.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 article-views-i.py diff --git a/article-views-i.py b/article-views-i.py new file mode 100644 index 0000000..874d6a0 --- /dev/null +++ b/article-views-i.py @@ -0,0 +1,62 @@ +""" + +Table: Views + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| article_id | int | +| author_id | int | +| viewer_id | int | +| view_date | date | ++---------------+---------+ +There is no primary key (column with unique values) for this table, the table may have duplicate rows. +Each row of this table indicates that some viewer viewed an article (written by some author) on some date. +Note that equal author_id and viewer_id indicate the same person. + + +Write a solution to find all the authors that viewed at least one of their own articles. + +Return the result table sorted by id in ascending order. + +The result format is in the following example. + + +Example 1: + +Input: +Views table: ++------------+-----------+-----------+------------+ +| article_id | author_id | viewer_id | view_date | ++------------+-----------+-----------+------------+ +| 1 | 3 | 5 | 2019-08-01 | +| 1 | 3 | 6 | 2019-08-02 | +| 2 | 7 | 7 | 2019-08-01 | +| 2 | 7 | 6 | 2019-08-02 | +| 4 | 7 | 1 | 2019-07-22 | +| 3 | 4 | 4 | 2019-07-21 | +| 3 | 4 | 4 | 2019-07-21 | ++------------+-----------+-----------+------------+ +Output: ++------+ +| id | ++------+ +| 4 | +| 7 | ++------+ + +""" + +# The function filters the DataFrame to include only rows where the author_id matches the viewer_id, +# meaning the author viewed their own article. It then removes duplicate author_id entries to ensure +# each author appears only once and sorts the results before returning the final DataFrame. + +import pandas as pd + +def article_views(views: pd.DataFrame) -> pd.DataFrame: + + df = views[views['author_id'] == views['viewer_id']] + df.drop_duplicates(subset=['author_id'], inplace=True) + df.sort_values(by=['author_id'], inplace=True) + + return df[['author_id']].rename(columns={'author_id': 'id'}) From e8edb53eb3f05e91415ffb6abb34ebafdd071a3f Mon Sep 17 00:00:00 2001 From: ankitabmungalpara Date: Thu, 27 Feb 2025 16:50:15 -0500 Subject: [PATCH 2/2] Create invalid-tweets.py --- invalid-tweets.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 invalid-tweets.py diff --git a/invalid-tweets.py b/invalid-tweets.py new file mode 100644 index 0000000..b600713 --- /dev/null +++ b/invalid-tweets.py @@ -0,0 +1,56 @@ +""" + +Table: Tweets + ++----------------+---------+ +| Column Name | Type | ++----------------+---------+ +| tweet_id | int | +| content | varchar | ++----------------+---------+ +tweet_id is the primary key (column with unique values) for this table. +content consists of characters on an American Keyboard, and no other special characters. +This table contains all the tweets in a social media app. + + +Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15. + +Return the result table in any order. + +The result format is in the following example. + + +Example 1: + +Input: +Tweets table: ++----------+-----------------------------------+ +| tweet_id | content | ++----------+-----------------------------------+ +| 1 | Let us Code | +| 2 | More than fifteen chars are here! | ++----------+-----------------------------------+ + +Output: ++----------+ +| tweet_id | ++----------+ +| 2 | ++----------+ + +Explanation: +Tweet 1 has length = 11. It is a valid tweet. +Tweet 2 has length = 33. It is an invalid tweet. + +""" + +# The function filters out tweets where the 'content' length is 15 characters or less. +# It creates a boolean mask to identify valid tweets and selects only those that meet the condition. +# Finally, it returns a DataFrame containing only the 'tweet_id' column of the valid tweets. + +import pandas as pd + +def invalid_tweets(tweets: pd.DataFrame) -> pd.DataFrame: + isValid = tweets['content'].str.len() > 15 + df = tweets[isValid] + return df[['tweet_id']]