Leetcode- 1/15
Table: Employee
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| salary | int |
| managerId | int |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table indicates the ID of an employee, their name, salary, and the ID of their manager.
```
import pandas as pd
def find_employees(employee: pd.DataFrame) -> pd.DataFrame:
df=pd.DataFrame(employee)
df.rename(columns={'name': 'Employee'}, inplace=True)
result = pd.merge(df, df, left_on='managerId', right_on='id', suffixes=('', '_manager'))
result = result[result['salary'] > result['salary_manager']]
result = result[['Employee']]
return result
```
**left_on="managerId":** This parameter specifies the column in the left DataFrame (data) that will be used as the key for merging. In this case, it's the column named "managerId".
**right_on="id":** This parameter specifies the column in the right DataFrame (data) that will be used as the key for merging. In this case, it's the column named "id".
**suffixes=("", "_manager"):** This parameter specifies a tuple of suffixes to be added to the column names in case of overlapping column names between the left and right DataFrames. In your example, an empty string is used for the left DataFrame, and "_manager" is used for the right DataFrame. This helps to distinguish between columns with the same name in the merged result.
## Duplicate Emails
Table: Person
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| email | varchar |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table contains an email. The emails will not contain uppercase letters.
```
import pandas as pd
def duplicate_emails(person: pd.DataFrame) -> pd.DataFrame:
return person.loc[person[person['email'].duplicated()].index][["email"]].drop_duplicates()
```
##
> In this example, the file located at file_path is opened in read **mode ('r')**, and its contents are read into the file_content variable.
Other common file modes include:
> **'w':** Write mode. Opens the file for writing. If the file already exists, it truncates the file to zero length. If the file does not exist, it creates a new file for writing.
> **'a':** Append mode. Opens the file for writing, but if the file already exists, it appends the new content to the end of the file.
> **'b':** Binary mode. Treats the file as a binary file instead of a text file. For example, use 'rb' for reading a binary file.
### Check the existed file in current directioary
```
###To check is the specific file is in the current working dirctonary
import os
file_name = 'file.txt'
if os.path.exists(file_name):
print(f'The file {file_name} exists in the current working directory.')
else:
print(f'The file {file_name} does not exist in the current working directory.')
```
## 193. Valid Phone Numbers
Given a text file file.txt that contains a list of phone numbers (one per line), write a one-liner bash script to print all valid phone numbers.
You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
o assume each line in the text file must not contain leading or trailing white spaces.
**Example:**
Assume that file.txt has the following content:
987-123-4567
123 456 7890
(123) 456-7890
Your script should output the following valid phone numbers:
987-123-4567
(123) 456-7890
```
import re
# Read the content of the file
file_path = 'file.txt'
with open(file_path, 'r') as file:
lines = file.readlines()
# Define the regular expression pattern
phone_number_pattern = re.compile(r'^(\d{3}-\d{3}-\d{4}|\(\d{3}\) \d{3}-\d{4})$')
# Print valid phone numbers
for line in lines:
if phone_number_pattern.match(line.strip()):
print(line.strip())
```
>**re.compile():**
> This function compiles the regular expression pattern into a regular expression object. This object can be used for matching operations in Python. Compiling the pattern can improve performance if you plan to use it multiple times.
```
import pandas as pd
def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame:
weather.sort_values(by='recordDate', inplace=True)
return weather.loc[(weather['temperature'].diff() > 0) & (weather['recordDate'].diff().dt.days == 1)][['id']]
```