Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions article-views-i.py
Original file line number Diff line number Diff line change
@@ -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'})
56 changes: 56 additions & 0 deletions invalid-tweets.py
Original file line number Diff line number Diff line change
@@ -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']]