Đây là một chương trình Python để kiểm tra các chữ cái phổ biến trong hai chuỗi đầu vào. Mô tả vấn đề Chương trình lấy hai chuỗi và kiểm tra các chữ cái chung trong cả hai chuỗi. Giải pháp vấn đề 1. Nhập hai chuỗi đầu vào và lưu trữ nó trong các biến riêng biệt. 2. Chuyển đổi cả hai chuỗi thành các bộ và tìm các chữ cái chung giữa cả hai bộ. 3. Lưu trữ các chữ cái phổ biến trong một danh sách. 4. Sử dụng một vòng lặp để in các chữ cái của danh sách. 5. Thoát. Chương trình/mã nguồn Dưới đây là mã nguồn của chương trình Python để kiểm tra các chữ cái phổ biến trong hai chuỗi đầu vào. Đầu ra chương trình cũng được hiển thị dưới đây.
2. Convert both of the strings into sets and find the common letters between both the
sets.
3. Store the common letters in a list.
4. Use a for loop to print the letters of the list.
5. Exit.s1=raw_input["Enter first string:"]
s2=raw_input["Enter second string:"]
a=list[set[s1]&set[s2]]
print["The common letters are:"]
for i in a:
print[i]
Giải thích chương trình
1. Người dùng phải nhập hai chuỗi đầu vào và lưu trữ nó trong các biến riêng biệt. 2. Cả hai chuỗi được chuyển đổi thành các bộ và các chữ cái chung giữa cả hai bộ được tìm thấy bằng toán tử ‘&. 3. Những chữ cái phổ biến này được lưu trữ trong một danh sách. 4. A For Loop được sử dụng để in các chữ cái của danh sách.
2. Both of the strings are converted into sets and the common letters between both the sets
are found using the ‘&’ operator.
3. These common letters are stored in a list.
4. A for loop is used to print the letters of the list.
Trường hợp kiểm tra thời gian chạy
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n
Sê -ri Giáo dục & Học tập toàn cầu Sanfoundry - Chương trình Python.
Để thực hành tất cả các chương trình Python, đây là bộ hoàn thành hơn 150 vấn đề và giải pháp Python.
Bước tiếp theo:
- Nhận Giấy chứng nhận miễn phí trong chương trình Python
- Tham gia cuộc thi chứng nhận lập trình Python
- Trở thành một người xếp hạng hàng đầu trong chương trình Python
- Thực hiện các bài kiểm tra lập trình Python
- Các bài kiểm tra thực hành theo chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10
- Các bài kiểm tra giả chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,
Manish Bhojasia, một cựu chiến binh công nghệ với hơn 20 năm @ Cisco & Wipro, là người sáng lập và CTO tại Sanfoundry. Ông sống ở Bangalore, và tập trung vào sự phát triển của nhân Linux, Công nghệ San, Cvanced C, Cấu trúc dữ liệu & Alogrithms. Giữ kết nối với anh ta tại LinkedIn.Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Đăng ký các lớp học chính miễn phí của mình tại YouTube & Thảo luận kỹ thuật tại Telegram SanfoundryClasses.
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e[2 times], g[1 time], k[1 time] and s[1 time]. Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh
Bàn luận
- Đưa ra hai chuỗi, in tất cả các ký tự phổ biến theo thứ tự từ vựng. Nếu không có chữ cái chung, in -1. Tất cả các chữ cái là chữ thường. & Nbsp;Counter[str] method, which contains characters of string as key and their frequencies as value.
- Vấn đề này có giải pháp hiện tại Vui lòng giới thiệu các ký tự phổ biến của hai chuỗi trong liên kết thứ tự bảng chữ cái. Chúng tôi sẽ giải quyết vấn đề này trong Python bằng cách sử dụng mô -đun thuộc tính và bộ sưu tập giao nhau. Cách tiếp cận là đơn giản,intersection [ a&b ] property.
- Chuyển đổi cả hai chuỗi thành kiểu dữ liệu từ điển bằng phương thức bộ đếm [STR], chứa các ký tự của chuỗi là khóa và tần số của chúng là giá trị.
- Bây giờ tìm các phần tử phổ biến giữa hai chuỗi sử dụng thuộc tính giao lộ [A & B].elements[] method of counter dictionary to expand list of keys by their frequency number of times.
- Kết quả cũng sẽ là một từ điển truy cập có các yếu tố phổ biến là các khóa và tần số phổ biến của chúng là giá trị.
Implementation:
Python3
from
collections
import
Counter
def
common[str1,str2]:
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n0
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n1
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n3
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n0
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n5
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n7
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n0
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n9
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e[2 times], g[1 time], k[1 time] and s[1 time]. Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh1
____10
Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e[2 times], g[1 time], k[1 time] and s[1 time]. Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh3
Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e[2 times], g[1 time], k[1 time] and s[1 time]. Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh4
Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e[2 times], g[1 time], k[1 time] and s[1 time]. Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh5
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e[2 times], g[1 time], k[1 time] and s[1 time]. Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh8
Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e[2 times], g[1 time], k[1 time] and s[1 time]. Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh9
eegks0
eegks1
eegks2
eegks3
eegks4
eegks5
eegks0
eegks7
____10
eegks9
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
from
1from
2____10
eegks9
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
from
6from
7Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n0
eegks1
collections
0Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e[2 times], g[1 time], k[1 time] and s[1 time]. Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh3
collections
2Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
collections
5Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e[2 times], g[1 time], k[1 time] and s[1 time]. Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh9
Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n0
collections
8Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
import
0Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n0
import
2Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n2
import
4Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n0
import
6Output:
eegks
Độ phức tạp về thời gian: O [n] & nbsp; không gian phụ trợ: O [n]
Auxiliary Space : O[n]