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
Input : test_str = 'GeeksforGeeks', {"G" : 1, "e" : 2, "k" : 5, "f" : 3, "s" : 15, "o" : 4, "r" : 6} Output : 63 Explanation : 2 [G*2] + 8[e*4] + 30[s*2] + 10[k*2] + 4[o] + 6[r] +3[f] = 63.
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 25
Bàn luậnUsing loop
Cho một chuỗi, mỗi ký tự được ánh xạ với trọng lượng [số], tính tổng trọng lượng của chuỗi.
Python3
Phương pháp số 1: Sử dụng vòng lặp
Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp lại cho tất cả các ký tự và tổng hợp tất cả các trọng số được ánh xạ từ từ điển.
Các
The original string is : GeeksforGeeks The weighted sum : 818
The original string is : GeeksforGeeks The weighted sum : 819
The original string is : GeeksforGeeks The weighted sum : 810
The original string is : GeeksforGeeks The weighted sum : 811
The original string is : GeeksforGeeks The weighted sum : 812
The original string is : GeeksforGeeks The weighted sum : 813
The original string is : GeeksforGeeks The weighted sum : 814
=
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 252
The original string is : GeeksforGeeks The weighted sum : 817
=
The original string is : GeeksforGeeks The weighted sum : 819
The original string is : GeeksforGeeks The weighted sum : 810
The original string is : GeeksforGeeks The weighted sum : 811
The original string is : GeeksforGeeks The weighted sum : 812
The original string is : GeeksforGeeks The weighted sum : 813
The original string is : GeeksforGeeks
The weighted sum : 81
4The original string is : GeeksforGeeks
The weighted sum : 81
5 The original string is : GeeksforGeeks
The weighted sum : 81
6
The original string is : GeeksforGeeks The weighted sum : 81
The original string is : GeeksforGeeks The weighted sum : 818The original string is : GeeksforGeeks The weighted sum : 819The original string is : GeeksforGeeks The weighted sum : 819The original string is : GeeksforGeeks The weighted sum : 811The original string is : GeeksforGeeks The weighted sum : 812test_dict
2O[n]Đầu raO[n]
Độ phức tạp về thời gian: O [n]Using sum[]
Không gian phụ trợ: O [n]
Python3
Phương pháp số 1: Sử dụng vòng lặp
Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp lại cho tất cả các ký tự và tổng hợp tất cả các trọng số được ánh xạ từ từ điển.
Các
The original string is : GeeksforGeeks The weighted sum : 818
The original string is : GeeksforGeeks The weighted sum : 819
The original string is : GeeksforGeeks The weighted sum : 810
The original string is : GeeksforGeeks The weighted sum : 811
The original string is : GeeksforGeeks The weighted sum : 812
The original string is : GeeksforGeeks The weighted sum : 813
:
5:
6
The original string is : GeeksforGeeks The weighted sum : 817
=
The original string is : GeeksforGeeks The weighted sum : 819
The original string is : GeeksforGeeks The weighted sum : 810
The original string is : GeeksforGeeks The weighted sum : 811
The original string is : GeeksforGeeks The weighted sum : 812
The original string is : GeeksforGeeks The weighted sum : 813
The original string is : GeeksforGeeks
The weighted sum : 81
4The original string is : GeeksforGeeks
The weighted sum : 81
5 The original string is : GeeksforGeeks
The weighted sum : 81
6
The original string is : GeeksforGeeks The weighted sum : 81
The original string is : GeeksforGeeks The weighted sum : 818The original string is : GeeksforGeeks The weighted sum : 819The original string is : GeeksforGeeks The weighted sum : 819The original string is : GeeksforGeeks The weighted sum : 811The original string is : GeeksforGeeks The weighted sum : 812test_dict
2O[n]Đầu raO[n]
Độ phức tạp về thời gian: O [n]Using list[],set[],count[] methods
Python3
Không gian phụ trợ: O [n]
Phương pháp số 2: Sử dụng Sum []
Đây là một cách nữa trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi sử dụng biểu thức máy phát và sum [] được sử dụng để tính toán tổng trọng lượng riêng lẻ.
The original string is : GeeksforGeeks The weighted sum : 817
=
'tough'
7The original string is : GeeksforGeeks The weighted sum : 819
'tough'
9The original string is : GeeksforGeeks The weighted sum : 819
:
1 :
2:
3:
4Phương thức số 3: Sử dụng Danh sách [], Set [], Count [] Phương thức
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2547
=
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2549
The original string is : GeeksforGeeks The weighted sum : 819
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2551
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2511
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2503
=
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2505
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2557
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2558
The original string is : GeeksforGeeks The weighted sum : 811
=
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2561
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2562
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2563
The original string is : GeeksforGeeks The weighted sum : 818
The original string is : GeeksforGeeks The weighted sum : 819
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2508
The original string is : GeeksforGeeks The weighted sum : 811
The original string is : GeeksforGeeks The weighted sum : 812
Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} Output : 2511
The original string is : GeeksforGeeks
The weighted sum : 81
4The original string is : GeeksforGeeks
The weighted sum : 81
5 The original string is : GeeksforGeeks
The weighted sum : 81
6
The original string is : GeeksforGeeks The weighted sum : 81